2019-06-14 07:10:37 -07:00
|
|
|
package keeper
|
2018-07-18 00:05:48 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2018-07-18 00:05:48 -07:00
|
|
|
)
|
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
// RegisterInvariants registers the bank module invariants
|
2020-01-30 13:31:16 -08:00
|
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, bk ViewKeeper) {
|
2019-06-05 16:26:17 -07:00
|
|
|
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
|
2020-01-30 13:31:16 -08:00
|
|
|
NonnegativeBalanceInvariant(bk))
|
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 {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
2020-01-30 13:31:16 -08:00
|
|
|
var (
|
|
|
|
msg string
|
|
|
|
count int
|
|
|
|
)
|
2019-07-11 03:56:43 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
bk.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
|
|
|
|
if balance.IsNegative() {
|
2019-07-15 09:56:38 -07:00
|
|
|
count++
|
2020-01-30 13:31:16 -08:00
|
|
|
msg += fmt.Sprintf("\t%s has a negative balance of %s\n", addr, balance)
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
2020-01-30 13:31:16 -08:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-07-15 09:56:38 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|