diff --git a/.pending/improvements/gaia/4080-add-missing-inv b/.pending/improvements/gaia/4080-add-missing-inv new file mode 100644 index 000000000000..48c8172da0a3 --- /dev/null +++ b/.pending/improvements/gaia/4080-add-missing-inv @@ -0,0 +1 @@ +#4080 add missing invariants during simulations \ No newline at end of file diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index 247cc262a308..7c6a2886de46 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -293,12 +293,7 @@ func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation { } func invariants(app *GaiaApp) []sdk.Invariant { - return []sdk.Invariant{ - simulation.PeriodicInvariant(bank.NonnegativeBalanceInvariant(app.accountKeeper), period, 0), - simulation.PeriodicInvariant(distr.AllInvariants(app.distrKeeper, app.stakingKeeper), period, 0), - simulation.PeriodicInvariant(staking.AllInvariants(app.stakingKeeper, app.feeCollectionKeeper, - app.distrKeeper, app.accountKeeper), period, 0), - } + return simulation.PeriodicInvariants(app.crisisKeeper.Invariants(), period, 0) } // Pass this in as an option to use a dbStoreAdapter instead of an IAVLStore for simulation speed. diff --git a/x/crisis/keeper.go b/x/crisis/keeper.go index 8501b08ee072..95c7cf09c1a0 100644 --- a/x/crisis/keeper.go +++ b/x/crisis/keeper.go @@ -39,3 +39,14 @@ func (k *Keeper) RegisterRoute(moduleName, route string, invar sdk.Invariant) { func (k Keeper) Routes() []InvarRoute { return k.routes } + +// Invariants returns all the registered Crisis keeper invariants. +func (k Keeper) Invariants() []sdk.Invariant { + var invars []sdk.Invariant + for _, route := range k.routes { + invars = append(invars, route.Invar) + } + return invars +} + +// DONTCOVER diff --git a/x/simulation/util.go b/x/simulation/util.go index 3dc75c439044..3badf25a9a90 100644 --- a/x/simulation/util.go +++ b/x/simulation/util.go @@ -75,3 +75,19 @@ func PeriodicInvariant(invariant sdk.Invariant, period int, offset int) sdk.Inva return nil } } + +// PeriodicInvariants returns an array of wrapped Invariants. Where each +// invariant function is only executed periodically defined by period and offset. +func PeriodicInvariants(invariants []sdk.Invariant, period int, offset int) []sdk.Invariant { + var outInvariants []sdk.Invariant + for _, invariant := range invariants { + outInvariant := func(ctx sdk.Context) error { + if int(ctx.BlockHeight())%period == offset { + return invariant(ctx) + } + return nil + } + outInvariants = append(outInvariants, outInvariant) + } + return outInvariants +}