cosmos-sdk/x/bank/internal/keeper/invariants.go

38 lines
1.0 KiB
Go

package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)
// RegisterInvariants registers the bank module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, ak types.AccountKeeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
NonnegativeBalanceInvariant(ak))
}
// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var count int
accts := ak.GetAllAccounts(ctx)
for _, acc := range accts {
coins := acc.GetCoins()
if coins.IsAnyNegative() {
count++
msg += fmt.Sprintf("\t%s has a negative denomination of %s\n",
acc.GetAddress().String(),
coins.String())
}
}
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "nonnegative-outstanding",
fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg)), broken
}
}