From 416f668d166f4f3b3817661ad3808246da02435f Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Thu, 1 Nov 2018 22:58:55 -0700 Subject: [PATCH] simulation: Remove header from Invariant This got introduced recently, but wasn't actually needed, hence the reversion --- PENDING.md | 1 + x/bank/simulation/invariants.go | 4 ++-- x/distribution/simulation/invariants.go | 10 +++++----- x/gov/simulation/invariants.go | 3 +-- x/mock/simulation/random_simulate_blocks.go | 14 +++++++------- x/mock/simulation/types.go | 6 +++--- x/mock/simulation/util.go | 5 ++--- x/slashing/simulation/invariants.go | 3 +-- x/stake/simulation/invariants.go | 14 +++++++------- 9 files changed, 29 insertions(+), 31 deletions(-) diff --git a/PENDING.md b/PENDING.md index 8a16136c0965..f3e4b9db3877 100644 --- a/PENDING.md +++ b/PENDING.md @@ -9,6 +9,7 @@ BREAKING CHANGES * Gaia * SDK + * [simulation] \#2665 only argument to simulation.Invariant is now app * Tendermint diff --git a/x/bank/simulation/invariants.go b/x/bank/simulation/invariants.go index 6574407a4c77..f0cd5ba63ce0 100644 --- a/x/bank/simulation/invariants.go +++ b/x/bank/simulation/invariants.go @@ -14,7 +14,7 @@ import ( // NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances func NonnegativeBalanceInvariant(mapper auth.AccountKeeper) simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { ctx := app.NewContext(false, abci.Header{}) accts := mock.GetAllAccounts(mapper, ctx) for _, acc := range accts { @@ -32,7 +32,7 @@ func NonnegativeBalanceInvariant(mapper auth.AccountKeeper) simulation.Invariant // TotalCoinsInvariant checks that the sum of the coins across all accounts // is what is expected func TotalCoinsInvariant(mapper auth.AccountKeeper, totalSupplyFn func() sdk.Coins) simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { ctx := app.NewContext(false, abci.Header{}) totalCoins := sdk.Coins{} diff --git a/x/distribution/simulation/invariants.go b/x/distribution/simulation/invariants.go index 45c015cf07dd..75863bfdb406 100644 --- a/x/distribution/simulation/invariants.go +++ b/x/distribution/simulation/invariants.go @@ -13,9 +13,8 @@ import ( // AllInvariants runs all invariants of the distribution module // Currently: total supply, positive power func AllInvariants(d distr.Keeper, sk distr.StakeKeeper) simulation.Invariant { - - return func(app *baseapp.BaseApp, header abci.Header) error { - err := ValAccumInvariants(d, sk)(app, header) + return func(app *baseapp.BaseApp) error { + err := ValAccumInvariants(d, sk)(app) if err != nil { return err } @@ -26,8 +25,9 @@ func AllInvariants(d distr.Keeper, sk distr.StakeKeeper) simulation.Invariant { // ValAccumInvariants checks that the fee pool accum == sum all validators' accum func ValAccumInvariants(k distr.Keeper, sk distr.StakeKeeper) simulation.Invariant { - return func(app *baseapp.BaseApp, header abci.Header) error { - ctx := app.NewContext(false, header) + return func(app *baseapp.BaseApp) error { + mockHeader := abci.Header{Height: app.LastBlockHeight() + 1} + ctx := app.NewContext(false, mockHeader) height := ctx.BlockHeight() valAccum := sdk.ZeroDec() diff --git a/x/gov/simulation/invariants.go b/x/gov/simulation/invariants.go index 3135ac80cb43..6d5f41918474 100644 --- a/x/gov/simulation/invariants.go +++ b/x/gov/simulation/invariants.go @@ -3,12 +3,11 @@ package simulation import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/x/mock/simulation" - abci "github.com/tendermint/tendermint/abci/types" ) // AllInvariants tests all governance invariants func AllInvariants() simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { // TODO Add some invariants! // Checking proposal queues, no passed-but-unexecuted proposals, etc. return nil diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index 36c41fdd118f..a4da39523e55 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -3,7 +3,6 @@ package simulation import ( "encoding/json" "fmt" - "math" "math/rand" "os" "os/signal" @@ -49,7 +48,8 @@ func initChain(r *rand.Rand, accounts []Account, setups []RandSetup, app *baseap } func randTimestamp(r *rand.Rand) time.Time { - unixTime := r.Int63n(int64(math.Pow(2, 40))) + // json.Marshal breaks for timestamps greater with year greater than 9999 + unixTime := r.Int63n(253373529600) return time.Unix(unixTime, 0) } @@ -138,7 +138,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, if testingMode { // Make sure invariants hold at beginning of block - assertAllInvariants(t, app, header, invariants, "BeginBlock", displayLogs) + assertAllInvariants(t, app, invariants, "BeginBlock", displayLogs) } ctx := app.NewContext(false, header) @@ -149,7 +149,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, numQueuedTimeOpsRan := runQueuedTimeOperations(timeOperationQueue, header.Time, tb, r, app, ctx, accs, logWriter, displayLogs, event) if testingMode && onOperation { // Make sure invariants hold at end of queued operations - assertAllInvariants(t, app, header, invariants, "QueuedOperations", displayLogs) + assertAllInvariants(t, app, invariants, "QueuedOperations", displayLogs) } logWriter("Standard operations") @@ -157,7 +157,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, opCount += operations + numQueuedOpsRan + numQueuedTimeOpsRan if testingMode { // Make sure invariants hold at end of block - assertAllInvariants(t, app, header, invariants, "StandardOperations", displayLogs) + assertAllInvariants(t, app, invariants, "StandardOperations", displayLogs) } res := app.EndBlock(abci.RequestEndBlock{}) @@ -168,7 +168,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, if testingMode { // Make sure invariants hold at end of block - assertAllInvariants(t, app, header, invariants, "EndBlock", displayLogs) + assertAllInvariants(t, app, invariants, "EndBlock", displayLogs) } if commit { app.Commit() @@ -243,7 +243,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, queueOperations(operationQueue, timeOperationQueue, futureOps) if testingMode { if onOperation { - assertAllInvariants(t, app, header, invariants, fmt.Sprintf("operation: %v", logUpdate), displayLogs) + assertAllInvariants(t, app, invariants, fmt.Sprintf("operation: %v", logUpdate), displayLogs) } if opCount%50 == 0 { fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", header.Height, totalNumBlocks, opCount, blocksize) diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 0a7c989a5c08..e601f2e1f9f5 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -33,7 +33,7 @@ type ( // If the invariant has been broken, it should return an error // containing a descriptive message about what happened. // The simulator will then halt and print the logs. - Invariant func(app *baseapp.BaseApp, header abci.Header) error + Invariant func(app *baseapp.BaseApp) error // Account contains a privkey, pubkey, address tuple // eventually more useful data can be placed in here. @@ -73,9 +73,9 @@ type ( // a given invariant if the mock application's last block modulo the given // period is congruent to the given offset. func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant { - return func(app *baseapp.BaseApp, header abci.Header) error { + return func(app *baseapp.BaseApp) error { if int(app.LastBlockHeight())%period == offset { - return invariant(app, header) + return invariant(app) } return nil } diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 84a4ad2a338c..0911a53cd8ea 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -9,7 +9,6 @@ import ( "testing" "time" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" @@ -103,11 +102,11 @@ func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height } // assertAllInvariants asserts a list of provided invariants against application state -func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, header abci.Header, +func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, invariants []Invariant, where string, displayLogs func()) { for i := 0; i < len(invariants); i++ { - err := invariants[i](app, header) + err := invariants[i](app) if err != nil { fmt.Printf("Invariants broken after %s\n", where) fmt.Println(err.Error()) diff --git a/x/slashing/simulation/invariants.go b/x/slashing/simulation/invariants.go index 5ad2d65ad2f0..0aa0ed1e5c8b 100644 --- a/x/slashing/simulation/invariants.go +++ b/x/slashing/simulation/invariants.go @@ -3,13 +3,12 @@ package simulation import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/x/mock/simulation" - abci "github.com/tendermint/tendermint/abci/types" ) // TODO Any invariants to check here? // AllInvariants tests all slashing invariants func AllInvariants() simulation.Invariant { - return func(_ *baseapp.BaseApp, _ abci.Header) error { + return func(_ *baseapp.BaseApp) error { return nil } } diff --git a/x/stake/simulation/invariants.go b/x/stake/simulation/invariants.go index 3b97bdb72f0e..8e6f18432112 100644 --- a/x/stake/simulation/invariants.go +++ b/x/stake/simulation/invariants.go @@ -19,16 +19,16 @@ func AllInvariants(ck bank.Keeper, k stake.Keeper, f auth.FeeCollectionKeeper, d distribution.Keeper, am auth.AccountKeeper) simulation.Invariant { - return func(app *baseapp.BaseApp, header abci.Header) error { - err := SupplyInvariants(ck, k, f, d, am)(app, header) + return func(app *baseapp.BaseApp) error { + err := SupplyInvariants(ck, k, f, d, am)(app) if err != nil { return err } - err = PositivePowerInvariant(k)(app, header) + err = PositivePowerInvariant(k)(app) if err != nil { return err } - err = ValidatorSetInvariant(k)(app, header) + err = ValidatorSetInvariant(k)(app) return err } } @@ -37,7 +37,7 @@ func AllInvariants(ck bank.Keeper, k stake.Keeper, // nolint: unparam func SupplyInvariants(ck bank.Keeper, k stake.Keeper, f auth.FeeCollectionKeeper, d distribution.Keeper, am auth.AccountKeeper) simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { ctx := app.NewContext(false, abci.Header{}) pool := k.GetPool(ctx) @@ -102,7 +102,7 @@ func SupplyInvariants(ck bank.Keeper, k stake.Keeper, // PositivePowerInvariant checks that all stored validators have > 0 power func PositivePowerInvariant(k stake.Keeper) simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { ctx := app.NewContext(false, abci.Header{}) var err error k.IterateValidatorsBonded(ctx, func(_ int64, validator sdk.Validator) bool { @@ -118,7 +118,7 @@ func PositivePowerInvariant(k stake.Keeper) simulation.Invariant { // ValidatorSetInvariant checks equivalence of Tendermint validator set and SDK validator set func ValidatorSetInvariant(k stake.Keeper) simulation.Invariant { - return func(app *baseapp.BaseApp, _ abci.Header) error { + return func(app *baseapp.BaseApp) error { // TODO return nil }