/
invariants.go
51 lines (40 loc) · 1.71 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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
)
// RegisterInvariants registers all transfer invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
ir.RegisterRoute(types.ModuleName, "total-escrow-per-denom",
TotalEscrowPerDenomInvariants(k))
}
// AllInvariants runs all invariants of the transfer module.
func AllInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return TotalEscrowPerDenomInvariants(k)(ctx)
}
}
// TotalEscrowPerDenomInvariants checks that the total amount escrowed for
// each denom is not smaller than the amount stored in the state entry.
func TotalEscrowPerDenomInvariants(k *Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var actualTotalEscrowed sdk.Coins
expectedTotalEscrowed := k.GetAllTotalEscrowed(ctx)
portID := k.GetPort(ctx)
transferChannels := k.channelKeeper.GetAllChannelsWithPortPrefix(ctx, portID)
for _, channel := range transferChannels {
escrowAddress := types.GetEscrowAddress(portID, channel.ChannelId)
escrowBalances := k.bankKeeper.GetAllBalances(ctx, escrowAddress)
actualTotalEscrowed = actualTotalEscrowed.Add(escrowBalances...)
}
// the actual escrowed amount must be greater than or equal to the expected amount for all denominations
if !actualTotalEscrowed.IsAllGTE(expectedTotalEscrowed) {
return sdk.FormatInvariant(
types.ModuleName,
"total escrow per denom invariance",
fmt.Sprintf("found denom(s) with total escrow amount lower than expected:\nactual total escrowed: %s\nexpected total escrowed: %s", actualTotalEscrowed, expectedTotalEscrowed)), true
}
return "", false
}
}