-
Notifications
You must be signed in to change notification settings - Fork 1
/
invariants.go
executable file
·38 lines (31 loc) · 1.03 KB
/
invariants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/commitHub/commitBlockchain/modules/bank/internal/types"
)
// register bank 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)
}
}