/
invariants.go
48 lines (39 loc) · 1.27 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
39
40
41
42
43
44
45
46
47
48
package keeper
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
blackfury "github.com/elysium-station/blackfury/types"
"github.com/elysium-station/blackfury/x/erc20/types"
)
// RegisterInvariants registers the erc20 module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "consistent-balance", ConsistentBalanceInvariant(k))
}
// ConsistentBalanceInvariant checks that all accounts have consistent balances in bank and erc20
func ConsistentBalanceInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)
// Only iterate on native coins
k.bankKeeper.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
if strings.Contains(balance.Denom, blackfury.DisplayDenom) {
// skip gas token
return false
}
erc20Balance := k.GetBalance(ctx, addr, balance.Denom)
if !erc20Balance.IsEqual(balance) {
count++
msg += fmt.Sprintf("\t%s has unequal balances: %s != %s\n", addr, balance, erc20Balance)
}
return false
})
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, "consistent-balance",
fmt.Sprintf("inconsistent balances found %d\n%s", count, msg),
), broken
}
}