Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R Run missing invariants during simulations #4080

Merged
merged 8 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

}

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a unit test for just this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gracefully decline, I really can't see how this is useful whatsoever, I'll add a // DONTCOVER though

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
}