/
invariants.go
42 lines (34 loc) · 1.19 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
package keeper
import (
"fmt"
"github.com/netcloth/netcloth-chain/modules/auth/exported"
"github.com/netcloth/netcloth-chain/modules/supply/internal/types"
sdk "github.com/netcloth/netcloth-chain/types"
)
// RegisterInvariants register all supply invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "total-supply", TotalSupply(k))
}
// AllInvariants runs all invariants of the supply module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return TotalSupply(k)(ctx)
}
}
// TotalSupply checks that the total supply reflects all the coins held in accounts
func TotalSupply(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var expectedTotal sdk.Coins
supply := k.GetSupply(ctx)
k.ak.IterateAccounts(ctx, func(acc exported.Account) bool {
expectedTotal = expectedTotal.Add(acc.GetCoins())
return false
})
broken := !expectedTotal.IsEqual(supply.GetTotal())
return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
"\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v\n",
expectedTotal, supply.GetTotal())), broken
}
}