cosmos-sdk/x/bank/simulation/invariants.go

46 lines
1.2 KiB
Go
Raw Normal View History

2018-07-18 00:05:48 -07:00
package simulation
import (
"errors"
2018-07-18 00:05:48 -07:00
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
func NonnegativeBalanceInvariant(ak auth.AccountKeeper) sdk.Invariant {
return func(ctx sdk.Context) error {
accts := ak.GetAllAccounts(ctx)
2018-07-18 00:05:48 -07:00
for _, acc := range accts {
coins := acc.GetCoins()
if coins.IsAnyNegative() {
return fmt.Errorf("%s has a negative denomination of %s",
2018-07-18 00:05:48 -07:00
acc.GetAddress().String(),
coins.String())
}
2018-07-18 00:05:48 -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
func TotalCoinsInvariant(ak auth.AccountKeeper, totalSupplyFn func() sdk.Coins) sdk.Invariant {
return func(ctx sdk.Context) error {
2019-03-07 16:55:08 -08:00
totalCoins := sdk.NewCoins()
2018-07-18 00:05:48 -07:00
chkAccount := func(acc auth.Account) bool {
coins := acc.GetCoins()
totalCoins = totalCoins.Add(coins)
2018-07-18 00:05:48 -07:00
return false
}
ak.IterateAccounts(ctx, chkAccount)
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
}
}