Skip to content

Commit

Permalink
Run missing invariants during simulations (#4080)
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski authored and alessio committed Apr 10, 2019
1 parent 2cd2289 commit 02a0e39
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .pending/improvements/gaia/4080-add-missing-inv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4080 add missing invariants during simulations
7 changes: 1 addition & 6 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions x/crisis/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions x/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 02a0e39

Please sign in to comment.