2018-07-18 00:05:48 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
2018-09-09 08:34:09 -07:00
|
|
|
"errors"
|
2018-07-18 00:05:48 -07:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
|
2018-10-20 23:22:48 -07:00
|
|
|
func NonnegativeBalanceInvariant(mapper auth.AccountKeeper) simulation.Invariant {
|
2018-11-29 07:17:10 -08:00
|
|
|
return func(ctx sdk.Context) error {
|
2018-07-18 00:05:48 -07:00
|
|
|
accts := mock.GetAllAccounts(mapper, ctx)
|
|
|
|
for _, acc := range accts {
|
|
|
|
coins := acc.GetCoins()
|
2019-02-06 14:45:15 -08:00
|
|
|
if coins.IsAnyNegative() {
|
2018-09-09 08:34:09 -07:00
|
|
|
return fmt.Errorf("%s has a negative denomination of %s",
|
2018-07-18 00:05:48 -07:00
|
|
|
acc.GetAddress().String(),
|
2018-09-09 08:34:09 -07:00
|
|
|
coins.String())
|
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2018-09-09 08:34:09 -07:00
|
|
|
return nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalCoinsInvariant checks that the sum of the coins across all accounts
|
|
|
|
// is what is expected
|
2018-10-20 23:22:48 -07:00
|
|
|
func TotalCoinsInvariant(mapper auth.AccountKeeper, totalSupplyFn func() sdk.Coins) simulation.Invariant {
|
2018-11-29 07:17:10 -08:00
|
|
|
return func(ctx sdk.Context) error {
|
2018-07-18 00:05:48 -07:00
|
|
|
totalCoins := sdk.Coins{}
|
|
|
|
|
|
|
|
chkAccount := func(acc auth.Account) bool {
|
|
|
|
coins := acc.GetCoins()
|
|
|
|
totalCoins = totalCoins.Plus(coins)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper.IterateAccounts(ctx, chkAccount)
|
2018-09-09 08:34:09 -07:00
|
|
|
if !totalSupplyFn().IsEqual(totalCoins) {
|
|
|
|
return errors.New("total calculated coins doesn't equal expected coins")
|
|
|
|
}
|
|
|
|
return nil
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
}
|