-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathhooks_test.go
More file actions
339 lines (295 loc) · 13.1 KB
/
hooks_test.go
File metadata and controls
339 lines (295 loc) · 13.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package keeper_test
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
lockupkeeper "github.com/osmosis-labs/osmosis/v10/x/lockup/keeper"
lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/types"
)
func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
testCases := []struct {
name string
validatorStats []stakingtypes.BondStatus
delegatorNumber int
superDelegations []superfluidDelegation
expRewards []sdk.Coins
}{
{
"happy path with single validator and delegator",
[]stakingtypes.BondStatus{stakingtypes.Bonded},
1,
[]superfluidDelegation{{0, 0, 0, 1000000}},
// bond denom staked in pool = 15_000_000
// with risk adjustment, the actual bond denom staked via superfluid would be 15_000_000 * (1 - 0.5) = 7_500_000
// we do an arbitrary swap to set spot price, which adjusts superfluid staked equivalent base denom 20_000_000 * (1 - 0.5) = 10_000_000 during begin block
// delegation rewards are calculated using the equation (current period cumulative reward ratio - last period cumulative reward ratio) * asset amount
// in this test case, the calculation for expected reward would be the following (0.99999 - 0) * 10_000_000
// thus we expect 999_990 stake as rewards
[]sdk.Coins{{sdk.NewCoin("stake", sdk.NewInt(999990))}},
},
{
"happy path with two validator and delegator each",
[]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded},
2,
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}},
// reward for the first block propser / lock 0 that has been superfluid staked would be equivalent to calculations done above
// 999_990 stake as rewards.
// reward for the second delegation is expected to be different. Amount superfluid staked would be equivalently 7_500_000 stake.
// This would be the first block propsed by the second validator, current period cumulative reward ratio being 999_86.66684,
// last period cumulative reward ratio being 0
// Thus as rewards, we expect 999986stake, calculted using the following equation: (999_86.66684 - 0) * 7_500_000
[]sdk.Coins{{sdk.NewCoin("stake", sdk.NewInt(999990))}, {sdk.NewCoin("stake", sdk.NewInt(999986))}},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
valAddrs := suite.SetupValidators(tc.validatorStats)
denoms, poolIds := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20)})
// Generate delegator addresses
delAddrs, intermediaryAccs, locks := suite.setupSuperfluidDelegations(valAddrs, tc.superDelegations, denoms)
suite.checkIntermediaryAccountDelegations(intermediaryAccs)
// run swap and set spot price
pool, err := suite.App.GAMMKeeper.GetPoolAndPoke(suite.Ctx, poolIds[0])
suite.Require().NoError(err)
coins := pool.GetTotalPoolLiquidity(suite.Ctx)
suite.SwapAndSetSpotPrice(poolIds[0], coins[1], coins[0])
// run epoch actions
// run begin block for each validator so that both validator gets block rewards
for _, valAddr := range valAddrs {
suite.BeginNewBlockWithProposer(true, valAddr)
}
// check lptoken twap value set
newEpochMultiplier := suite.App.SuperfluidKeeper.GetOsmoEquivalentMultiplier(suite.Ctx, denoms[0])
suite.Require().Equal(newEpochMultiplier, sdk.NewDec(15))
for index, lock := range locks {
// check gauge creation in new block
intermediaryAccAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lock.ID)
intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, intermediaryAccAddr)
gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, intermediaryAcc.GaugeId)
suite.Require().NoError(err)
suite.Require().Equal(gauge.Id, intermediaryAcc.GaugeId)
suite.Require().Equal(gauge.IsPerpetual, true)
suite.Require().Equal(gauge.Coins, tc.expRewards[index])
suite.Require().Equal(gauge.DistributedCoins.String(), tc.expRewards[index].String())
}
// check delegation changes
for _, acc := range intermediaryAccs {
valAddr, err := sdk.ValAddressFromBech32(acc.ValAddr)
suite.Require().NoError(err)
delegation, found := suite.App.StakingKeeper.GetDelegation(suite.Ctx, acc.GetAccAddress(), valAddr)
suite.Require().True(found)
suite.Require().Equal(sdk.NewDec(7500000), delegation.Shares)
}
for index, delAddr := range delAddrs {
balance := suite.App.BankKeeper.GetAllBalances(suite.Ctx, delAddr)
suite.Require().Equal(tc.expRewards[index], balance)
}
})
}
}
// func (suite *KeeperTestSuite) TestOnStartUnlock() {
// testCases := []struct {
// name string
// validatorStats []stakingtypes.BondStatus
// superDelegations []superfluidDelegation
// unbondingLockIds []uint64
// expUnbondingErr []bool
// }{
// {
// "with single validator and single superfluid delegation and single lockup unlock",
// []stakingtypes.BondStatus{stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}},
// []uint64{1},
// []bool{false},
// },
// {
// "with single validator and multiple superfluid delegations and single undelegation",
// []stakingtypes.BondStatus{stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 0, "gamm/pool/1", 1000000}},
// []uint64{1},
// []bool{false},
// },
// {
// "with single validator and multiple superfluid delegations and multiple undelegation",
// []stakingtypes.BondStatus{stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 0, "gamm/pool/1", 1000000}},
// []uint64{1, 2},
// []bool{false, false},
// },
// {
// "with multiple validators and multiple superfluid delegations and multiple undelegations",
// []stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}, {0, 1, "gamm/pool/1", 1000000}},
// []uint64{1, 2},
// []bool{false, false},
// },
// {
// "undelegating not available lock id",
// []stakingtypes.BondStatus{stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}},
// []uint64{2},
// []bool{true},
// },
// {
// "try undelegating twice for same lock id",
// []stakingtypes.BondStatus{stakingtypes.Bonded},
// []superfluidDelegation{{0, 0, "gamm/pool/1", 1000000}},
// []uint64{1, 1},
// []bool{false, true},
// },
// }
// for _, tc := range testCases {
// tc := tc
// suite.Run(tc.name, func() {
// suite.SetupTest()
// poolId := suite.createGammPool([]string{appparams.BaseCoinUnit, "foo"})
// suite.Require().Equal(poolId, uint64(1))
// // Generate delegator addresses
// delAddrs := CreateRandomAccounts(1)
// // setup validators
// valAddrs := suite.SetupValidators(tc.validatorStats)
// // setup superfluid delegations
// intermediaryAccs, _ := suite.SetupSuperfluidDelegations(delAddrs, valAddrs, tc.superDelegations)
// suite.checkIntermediaryAccountDelegations(intermediaryAccs)
// for index, lockId := range tc.unbondingLockIds {
// // get intermediary account
// accAddr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId)
// intermediaryAcc := suite.App.SuperfluidKeeper.GetIntermediaryAccount(suite.Ctx, accAddr)
// valAddr := intermediaryAcc.ValAddr
// // unlock native lockup
// lock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId)
// if err == nil {
// err = suite.App.LockupKeeper.BeginUnlock(suite.Ctx, *lock, nil)
// }
// if tc.expUnbondingErr[index] {
// suite.Require().Error(err)
// continue
// }
// suite.Require().NoError(err)
// // check lockId and intermediary account connection deletion
// addr := suite.App.SuperfluidKeeper.GetLockIdIntermediaryAccountConnection(suite.Ctx, lockId)
// suite.Require().Equal(addr.String(), "")
// // check bonding synthetic lockup deletion
// _, err = suite.App.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.StakingSyntheticDenom(lock.Coins[0].Denom, valAddr))
// suite.Require().Error(err)
// // check unbonding synthetic lockup creation
// unbondingDuration := suite.App.StakingKeeper.GetParams(suite.Ctx).UnbondingTime
// synthLock, err := suite.App.LockupKeeper.GetSyntheticLockup(suite.Ctx, lockId, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr))
// suite.Require().NoError(err)
// suite.Require().Equal(synthLock.UnderlyingLockId, lockId)
// suite.Require().Equal(synthLock.SynthDenom, keeper.UnstakingSyntheticDenom(lock.Coins[0].Denom, valAddr))
// suite.Require().Equal(synthLock.EndTime, suite.Ctx.BlockTime().Add(unbondingDuration))
// }
// })
// }
// }
func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() {
testCases := []struct {
name string
validatorStats []stakingtypes.BondStatus
delegatorNumber int
superDelegations []superfluidDelegation
superUnbondingLockIds []uint64
slashedValIndexes []int64
expSlashedLockIds []uint64
expUnslashedLockIds []uint64
}{
{
"happy path with single validator and multiple superfluid delegations",
[]stakingtypes.BondStatus{stakingtypes.Bonded},
1,
[]superfluidDelegation{{0, 0, 0, 1000000}},
[]uint64{1},
[]int64{0},
[]uint64{1},
[]uint64{},
},
{
"with single validator and multiple superfluid delegations",
[]stakingtypes.BondStatus{stakingtypes.Bonded},
2,
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 0, 0, 1000000}},
[]uint64{1, 2},
[]int64{0},
[]uint64{1, 2},
[]uint64{},
},
{
"with multiple validators and multiple superfluid delegations",
[]stakingtypes.BondStatus{stakingtypes.Bonded, stakingtypes.Bonded},
2,
[]superfluidDelegation{{0, 0, 0, 1000000}, {1, 1, 0, 1000000}},
[]uint64{1, 2},
[]int64{0},
[]uint64{1},
[]uint64{2},
},
}
for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
slashFactor := sdk.NewDecWithPrec(5, 2)
// setup validators
valAddrs := suite.SetupValidators(tc.validatorStats)
denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20), sdk.NewDec(20)})
// setup superfluid delegations
_, intermediaryAccs, _ := suite.setupSuperfluidDelegations(valAddrs, tc.superDelegations, denoms)
suite.checkIntermediaryAccountDelegations(intermediaryAccs)
for _, lockId := range tc.superUnbondingLockIds {
lock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId)
suite.Require().NoError(err)
// superfluid undelegate
err = suite.App.SuperfluidKeeper.SuperfluidUndelegate(suite.Ctx, lock.Owner, lockId)
suite.Require().NoError(err)
}
// slash unbonding lockups for all intermediary accounts
for _, valIndex := range tc.slashedValIndexes {
validator, found := suite.App.StakingKeeper.GetValidator(suite.Ctx, valAddrs[valIndex])
suite.Require().True(found)
suite.Ctx = suite.Ctx.WithBlockHeight(100)
consAddr, err := validator.GetConsAddr()
suite.Require().NoError(err)
// slash by slash factor
power := sdk.TokensToConsensusPower(validator.Tokens, sdk.DefaultPowerReduction)
suite.App.StakingKeeper.Slash(suite.Ctx, consAddr, 80, power, slashFactor)
// Note: this calls BeforeSlashingUnbondingDelegation hook
}
// check slashed lockups
for _, lockId := range tc.expSlashedLockIds {
gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId)
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewInt(950000).String(), gotLock.Coins.AmountOf(denoms[0]).String())
}
// check unslashed lockups
for _, lockId := range tc.expUnslashedLockIds {
gotLock, err := suite.App.LockupKeeper.GetLockByID(suite.Ctx, lockId)
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewInt(1000000).String(), gotLock.Coins.AmountOf(denoms[0]).String())
}
})
}
}
// TestAfterAddTokensToLock_Event tests that events are correctly emitted
// when calling AfterAddTokensToLock.
func (suite *KeeperTestSuite) TestAfterAddTokensToLock_Event() {
suite.SetupTest()
valAddrs := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded})
denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20)})
// setup superfluid delegations
_, intermediaryAccs, locks := suite.setupSuperfluidDelegations(valAddrs, []superfluidDelegation{{0, 0, 0, 1000000}}, denoms)
suite.checkIntermediaryAccountDelegations(intermediaryAccs)
for index, lock := range locks {
lockupMsgServer := lockupkeeper.NewMsgServerImpl(suite.App.LockupKeeper)
c := sdk.WrapSDKContext(suite.Ctx)
coinsToLock := sdk.NewCoins(sdk.NewCoin(denoms[index], sdk.NewInt(100)))
sender, _ := sdk.AccAddressFromBech32(lock.Owner)
suite.FundAcc(sender, coinsToLock)
_, err := lockupMsgServer.LockTokens(c, lockuptypes.NewMsgLockTokens(sender, time.Hour*504, coinsToLock))
suite.Require().NoError(err)
// should call AfterAddTokensToLock hook and emit event here
suite.AssertEventEmitted(suite.Ctx, types.TypeEvtSuperfluidIncreaseDelegation, 1)
}
}