-
Notifications
You must be signed in to change notification settings - Fork 0
/
invariants.go
67 lines (55 loc) · 1.85 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package keeper
import (
"github.com/incubus-network/nemo/x/savings/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// RegisterInvariants registers the savings module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "deposits", DepositsInvariant(k))
ir.RegisterRoute(types.ModuleName, "solvency", SolvencyInvariant(k))
}
// AllInvariants runs all invariants of the savings module
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
if res, stop := DepositsInvariant(k)(ctx); stop {
return res, stop
}
res, stop := SolvencyInvariant(k)(ctx)
return res, stop
}
}
// DepositsInvariant iterates all deposits and asserts that they are valid
func DepositsInvariant(k Keeper) sdk.Invariant {
broken := false
message := sdk.FormatInvariant(types.ModuleName, "validate deposits broken", "deposit invalid")
return func(ctx sdk.Context) (string, bool) {
k.IterateDeposits(ctx, func(deposit types.Deposit) bool {
if err := deposit.Validate(); err != nil {
broken = true
return true
}
if !deposit.Amount.IsAllPositive() {
broken = true
return true
}
return false
})
return message, broken
}
}
// SolvencyInvariant iterates all deposits and ensures the total amount matches the module account coins
func SolvencyInvariant(k Keeper) sdk.Invariant {
message := sdk.FormatInvariant(types.ModuleName, "module solvency broken", "total deposited amount does not match module account")
return func(ctx sdk.Context) (string, bool) {
balance := k.GetSavingsModuleAccountBalances(ctx)
deposited := sdk.Coins{}
k.IterateDeposits(ctx, func(deposit types.Deposit) bool {
for _, coin := range deposit.Amount {
deposited = deposited.Add(coin)
}
return false
})
broken := !deposited.IsEqual(balance)
return message, broken
}
}