2019-08-08 12:51:18 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
// DONTCOVER
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2022-01-21 03:14:00 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
|
2019-06-28 13:11:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterInvariants registers all governance invariants
|
2020-01-30 13:31:16 -08:00
|
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, keeper Keeper, bk types.BankKeeper) {
|
|
|
|
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(keeper, bk))
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AllInvariants runs all invariants of the governance module
|
2020-01-30 13:31:16 -08:00
|
|
|
func AllInvariants(keeper Keeper, bk types.BankKeeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2020-01-30 13:31:16 -08:00
|
|
|
return ModuleAccountInvariant(keeper, bk)(ctx)
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleAccountInvariant checks that the module account coins reflects the sum of
|
|
|
|
// deposit amounts held on store
|
2020-01-30 13:31:16 -08:00
|
|
|
func ModuleAccountInvariant(keeper Keeper, bk types.BankKeeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2019-06-28 13:11:27 -07:00
|
|
|
var expectedDeposits sdk.Coins
|
|
|
|
|
2022-01-21 03:14:00 -08:00
|
|
|
keeper.IterateAllDeposits(ctx, func(deposit v1beta2.Deposit) bool {
|
2020-01-03 12:44:53 -08:00
|
|
|
expectedDeposits = expectedDeposits.Add(deposit.Amount...)
|
2019-06-28 13:11:27 -07:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
macc := keeper.GetGovernanceAccount(ctx)
|
2020-01-30 13:31:16 -08:00
|
|
|
balances := bk.GetAllBalances(ctx, macc.GetAddress())
|
|
|
|
broken := !balances.IsEqual(expectedDeposits)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "deposits",
|
2019-07-15 09:56:38 -07:00
|
|
|
fmt.Sprintf("\tgov ModuleAccount coins: %s\n\tsum of deposit amounts: %s\n",
|
2020-01-30 13:31:16 -08:00
|
|
|
balances, expectedDeposits)), broken
|
2019-06-28 13:11:27 -07:00
|
|
|
}
|
|
|
|
}
|