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

41 lines
1.0 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/internal/types"
2018-07-18 00:05:48 -07:00
)
// RegisterInvariants registers the bank module invariants
2020-01-30 13:31:16 -08:00
func RegisterInvariants(ir sdk.InvariantRegistry, bk ViewKeeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
2020-01-30 13:31:16 -08:00
NonnegativeBalanceInvariant(bk))
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
2020-01-30 13:31:16 -08:00
func NonnegativeBalanceInvariant(bk ViewKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
2020-01-30 13:31:16 -08:00
var (
msg string
count int
)
2020-01-30 13:31:16 -08:00
bk.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
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
}
}