Skip to content

Commit

Permalink
Merge PR cosmos#2665: simulation: Remove header from Invariant
Browse files Browse the repository at this point in the history
This got introduced recently, but wasn't actually needed, hence the reversion
  • Loading branch information
ValarDragon authored and cwgoes committed Nov 2, 2018
1 parent f4d3e65 commit 8e328c8
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 31 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BREAKING CHANGES
* Gaia

* SDK
* [simulation] \#2665 only argument to simulation.Invariant is now app

* Tendermint

Expand Down
4 changes: 2 additions & 2 deletions x/bank/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}

Expand Down
10 changes: 5 additions & 5 deletions x/distribution/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions x/gov/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions x/mock/simulation/random_simulate_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package simulation
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"os"
"os/signal"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -149,15 +149,15 @@ 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")
operations := blockSimulator(r, app, ctx, accs, header, logWriter)
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{})
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions x/mock/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions x/mock/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions x/slashing/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 7 additions & 7 deletions x/stake/simulation/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 8e328c8

Please sign in to comment.