Merge PR #2665: simulation: Remove header from Invariant
This got introduced recently, but wasn't actually needed, hence the reversion
This commit is contained in:
parent
f4d3e65ef0
commit
8e328c8706
|
@ -9,6 +9,7 @@ BREAKING CHANGES
|
|||
* Gaia
|
||||
|
||||
* SDK
|
||||
* [simulation] \#2665 only argument to simulation.Invariant is now app
|
||||
|
||||
* Tendermint
|
||||
|
||||
|
|
|
@ -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{}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
|
||||
|
@ -110,11 +109,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())
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue