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"
|
2019-06-14 07:10:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
2018-07-18 00:05:48 -07:00
|
|
|
)
|
|
|
|
|
2019-03-28 16:27:47 -07:00
|
|
|
// register bank invariants
|
2019-06-24 13:45:09 -07:00
|
|
|
func RegisterInvariants(ir sdk.InvariantRegistry, ak types.AccountKeeper) {
|
2019-06-05 16:26:17 -07:00
|
|
|
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
|
2019-03-28 16:27:47 -07:00
|
|
|
NonnegativeBalanceInvariant(ak))
|
|
|
|
}
|
|
|
|
|
2018-07-18 00:05:48 -07:00
|
|
|
// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
|
2019-06-24 13:45:09 -07:00
|
|
|
func NonnegativeBalanceInvariant(ak types.AccountKeeper) sdk.Invariant {
|
2019-07-11 03:56:43 -07:00
|
|
|
return func(ctx sdk.Context) (string, bool) {
|
|
|
|
var msg string
|
|
|
|
var amt int
|
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
accts := ak.GetAllAccounts(ctx)
|
2018-07-18 00:05:48 -07:00
|
|
|
for _, acc := range accts {
|
|
|
|
coins := acc.GetCoins()
|
2019-02-06 14:45:15 -08:00
|
|
|
if coins.IsAnyNegative() {
|
2019-07-11 03:56:43 -07:00
|
|
|
amt++
|
|
|
|
msg += fmt.Sprintf("\t%s has a negative denomination of %s\n",
|
2018-07-18 00:05:48 -07:00
|
|
|
acc.GetAddress().String(),
|
2018-09-09 08:34:09 -07:00
|
|
|
coins.String())
|
|
|
|
}
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
2019-07-11 03:56:43 -07:00
|
|
|
broken := amt != 0
|
2018-07-18 00:05:48 -07:00
|
|
|
|
2019-07-11 03:56:43 -07:00
|
|
|
return sdk.FormatInvariant(types.ModuleName, "nonnegative-outstanding",
|
|
|
|
fmt.Sprintf("amount of negative accounts found %d\n%s", amt, msg), broken)
|
2018-07-18 00:05:48 -07:00
|
|
|
}
|
|
|
|
}
|