/
invariants.go
46 lines (40 loc) · 1.51 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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/spn/x/reward/types"
)
const (
insufficientRewardsBalanceRoute = "insufficient-rewards-balance"
)
// RegisterInvariants registers all module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, insufficientRewardsBalanceRoute,
InsufficientRewardsBalanceInvariant(k))
}
// AllInvariants runs all invariants of the module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return InsufficientRewardsBalanceInvariant(k)(ctx)
}
}
// InsufficientRewardsBalanceInvariant checks if module account balance is greater or equal than the sum of all
// `remainingCoins` for all reward pools
func InsufficientRewardsBalanceInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
all := k.GetAllRewardPool(ctx)
totalRewards := sdk.NewCoins()
for _, rewardPool := range all {
// we don't need to check if reward pool is `closed` since properly closed pools should have no remaining coins
totalRewards = totalRewards.Add(rewardPool.RemainingCoins...)
}
moduleAddr := k.authKeeper.GetModuleAddress(types.ModuleName)
balance := k.bankKeeper.SpendableCoins(ctx, moduleAddr)
if !balance.IsAllGTE(totalRewards) {
return sdk.FormatInvariant(
types.ModuleName, insufficientRewardsBalanceRoute,
"module account balance lower than total remaining coins in reward pools",
), true
}
return "", false
}
}