49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package keeper
|
|
|
|
// DONTCOVER
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
|
|
)
|
|
|
|
// RegisterInvariants registers all governance invariants
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, keeper *Keeper, bk types.BankKeeper) {
|
|
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(keeper, bk))
|
|
}
|
|
|
|
// AllInvariants runs all invariants of the governance module
|
|
func AllInvariants(keeper *Keeper, bk types.BankKeeper) sdk.Invariant {
|
|
return func(ctx sdk.Context) (string, bool) {
|
|
return ModuleAccountInvariant(keeper, bk)(ctx)
|
|
}
|
|
}
|
|
|
|
// ModuleAccountInvariant checks that the module account coins reflects the sum of
|
|
// deposit amounts held on store
|
|
func ModuleAccountInvariant(keeper *Keeper, bk types.BankKeeper) sdk.Invariant {
|
|
return func(ctx sdk.Context) (string, bool) {
|
|
var expectedDeposits sdk.Coins
|
|
|
|
keeper.IterateAllDeposits(ctx, func(deposit v1.Deposit) bool {
|
|
expectedDeposits = expectedDeposits.Add(deposit.Amount...)
|
|
return false
|
|
})
|
|
|
|
macc := keeper.GetGovernanceAccount(ctx)
|
|
balances := bk.GetAllBalances(ctx, macc.GetAddress())
|
|
|
|
// Require that the deposit balances are <= than the x/gov module's total
|
|
// balances. We use the <= operator since external funds can be sent to x/gov
|
|
// module's account and so the balance can be larger.
|
|
broken := !balances.IsAllGTE(expectedDeposits)
|
|
|
|
return sdk.FormatInvariant(types.ModuleName, "deposits",
|
|
fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts: %s\n",
|
|
balances, expectedDeposits)), broken
|
|
}
|
|
}
|