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

69 lines
1.9 KiB
Go
Raw Normal View History

package keeper
2018-07-18 00:05:48 -07:00
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
2018-07-18 00:05:48 -07:00
)
// RegisterInvariants registers the bank module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding", NonnegativeBalanceInvariant(k))
ir.RegisterRoute(types.ModuleName, "total-supply", TotalSupply(k))
}
// AllInvariants runs all invariants of the X/bank module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return TotalSupply(k)(ctx)
}
Merge PR #3656: Broken-Invar Tx - aka. Crisis module * beginning thinking on issue * ... * working * working * working fee pool distribution * spek outline * spec update * gas refund calculations * simulation saved to ~/.gaiad/simulations/ * lean simulation output int * cleanup bank simulation messages * operation messges int * lint * move simulation to its own module * move simulation log code to log.go * logger overhaul int * distribution comments * fix compiling * cleanup modifications to x/distribution/keeper/allocation.go int int int * gov bug * result.IsOK() minimization * importExport typo bug * pending * address @alexanderbez comments * simple @cwgoes comments addressed * event logging unified approach * distr module name constant * implementing * compiles * gaia integration * proper constant fee removal * crisis genesis * go.sum update * ... * debugging * fix sum errors * missing err checks * working implementing CLI * remove query command * crisis expected keepers in other modules * crisis testing infrastructure * working * tests complete * modify handler to still panic if not enough pool coins, docs working * spec tags * docs complete * CL * assert invariants on a blockly basis gaiad functionality * gaiad CL * transaction details in runtime invariance panic * Apply suggestions from code review Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * sender tags * @mossid suggestions int * @cwgoes comments final * Apply suggestions from code review Co-Authored-By: rigelrozanski <rigel.rozanski@gmail.com> * bug seems fixed (#3998) * delete unused line in zero height export bug
2019-03-28 16:27:47 -07:00
}
2018-07-18 00:05:48 -07:00
// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
func NonnegativeBalanceInvariant(k ViewKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
2020-01-30 13:31:16 -08:00
var (
msg string
count int
)
k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
2020-01-30 13:31:16 -08:00
if balance.IsNegative() {
count++
2020-01-30 13:31:16 -08:00
msg += fmt.Sprintf("\t%s has a negative balance of %s\n", addr, balance)
}
2020-01-30 13:31:16 -08:00
return false
})
broken := count != 0
2018-07-18 00:05:48 -07:00
2020-01-30 13:31:16 -08:00
return sdk.FormatInvariant(
types.ModuleName, "nonnegative-outstanding",
fmt.Sprintf("amount of negative balances found %d\n%s", count, msg),
), broken
2018-07-18 00:05:48 -07:00
}
}
// TotalSupply checks that the total supply reflects all the coins held in accounts
func TotalSupply(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
expectedTotal := sdk.Coins{}
supply := k.GetSupply(ctx)
k.IterateAllBalances(ctx, func(_ sdk.AccAddress, balance sdk.Coin) bool {
expectedTotal = expectedTotal.Add(balance)
return false
})
broken := !expectedTotal.IsEqual(supply.GetTotal())
return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
"\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v\n",
expectedTotal, supply.GetTotal())), broken
}
}