-
Notifications
You must be signed in to change notification settings - Fork 0
/
invariants.go
153 lines (131 loc) · 4.66 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package keeper
// DONTCOVER
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furya-official/fury-blockchain/x/bonds/types"
)
// RegisterInvariants registers all supply invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "bonds-supply",
SupplyInvariant(k))
ir.RegisterRoute(types.ModuleName, "bonds-reserve",
ReserveInvariant(k))
ir.RegisterRoute(types.ModuleName, "bonds-available-reserve",
AvailableReserveInvariant(k))
}
// AllInvariants runs all invariants of the bonds module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
res, stop := SupplyInvariant(k)(ctx)
if stop {
return res, stop
}
res, stop = ReserveInvariant(k)(ctx)
if stop {
return res, stop
}
return AvailableReserveInvariant(k)(ctx)
}
}
func SupplyInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var count int
// Get supply of coins held in accounts (includes stake token)
supplyInAccounts := sdk.Coins{}
k.BankKeeper.IterateAllBalances(ctx, func(_ sdk.AccAddress, balance sdk.Coin) bool {
supplyInAccounts = supplyInAccounts.Add(balance)
return false
})
iterator := k.GetBondIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
bond := k.MustGetBondByKey(ctx, iterator.Key())
denom := bond.Token
batch := k.MustGetBatch(ctx, bond.BondDid)
did := bond.BondDid
// Add bond current supply
supplyInBondsAndBatches := bond.CurrentSupply
// Subtract amount to be burned (this amount was already burned
// in handleMsgSell but is still a part of bond's CurrentSupply)
for _, s := range batch.Sells {
if !s.BaseOrder.Cancelled {
supplyInBondsAndBatches = supplyInBondsAndBatches.Sub(
s.BaseOrder.Amount)
}
}
// Check that amount matches supply in accounts
inAccounts := supplyInAccounts.AmountOf(bond.Token)
if !supplyInBondsAndBatches.Amount.Equal(inAccounts) {
count++
msg += fmt.Sprintf("total %s supply invariance:\n"+
"\ttotal %s supply: %s\n"+
"\tsum of %s in accounts: %s\n",
did, denom, supplyInBondsAndBatches.Amount.String(),
denom, inAccounts.String())
}
}
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "supply", fmt.Sprintf(
"%d Bonds supply invariants broken\n%s", count, msg)), broken
}
}
func ReserveInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var msg string
var count int
iterator := k.GetBondIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
bond := k.MustGetBondByKey(ctx, iterator.Key())
denom := bond.Token
did := bond.BondDid
if bond.FunctionType == types.AugmentedFunction ||
bond.FunctionType == types.SwapperFunction {
continue // Check does not apply to augmented/swapper functions
}
expectedReserve, err := bond.ReserveAtSupply(bond.CurrentSupply.Amount)
if err != nil {
continue // ignore error
}
expectedRounded := expectedReserve.Ceil().TruncateInt()
actualReserve := k.GetReserveBalances(ctx, did)
for _, r := range actualReserve {
if r.Amount.LT(expectedRounded) {
count++
msg += fmt.Sprintf("%s reserve invariance:\n"+
"\texpected(ceil-rounded) %s reserve: %s\n"+
"\tactual %s reserve: %s\n",
did, denom, expectedReserve.String(),
denom, r.String())
}
}
}
broken := count != 0
return sdk.FormatInvariant(types.ModuleName, "reserve", fmt.Sprintf(
"%d Bonds reserve invariants broken\n%s", count, msg)), broken
}
}
func AvailableReserveInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
// Get actual available reserve
reservePool := k.accountKeeper.GetModuleAddress(types.BondsReserveAccount)
actualAvailableReserve := k.BankKeeper.GetAllBalances(ctx, reservePool)
// If no bonds (iterator invalid) then invariant automatically holds
iterator := k.GetBondIterator(ctx)
if !iterator.Valid() {
return "", false
}
// Calculate sum of available reserves reported by bonds, including any
// outcome payment reserve, since this has already reached the reserve
// but is not considered a part of the available reserve
availableReserveSum := sdk.NewCoins()
for ; iterator.Valid(); iterator.Next() {
bond := k.MustGetBondByKey(ctx, iterator.Key())
availableReserveSum = availableReserveSum.Add(bond.AvailableReserve...)
availableReserveSum = availableReserveSum.Add(bond.CurrentOutcomePaymentReserve...)
}
broken := !availableReserveSum.IsEqual(actualAvailableReserve)
return sdk.FormatInvariant(types.ModuleName, "available-reserve",
"Bonds available reserve invariant broken"), broken
}
}