-
Notifications
You must be signed in to change notification settings - Fork 138
/
unbonding.go
469 lines (383 loc) · 18.4 KB
/
unbonding.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package integration
import (
"time"
"cosmossdk.io/math"
providerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper"
ccv "github.com/cosmos/interchain-security/v3/x/ccv/types"
)
// TestUndelegationNormalOperation tests that undelegations complete after
// the unbonding period elapses on both the consumer and provider, without
// VSC packets timing out.
func (s *CCVTestSuite) TestUndelegationNormalOperation() {
unbondConsumer := func(expectedPackets int) {
// relay 1 VSC packet from provider to consumer
relayAllCommittedPackets(s, s.providerChain, s.path, ccv.ProviderPortID, s.path.EndpointB.ChannelID, expectedPackets)
// increment time so that the unbonding period ends on the consumer
incrementTimeByUnbondingPeriod(s, Consumer)
// relay 1 VSCMatured packet from consumer to provider
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, expectedPackets)
}
testCases := []struct {
name string
shareDiv int64
unbond func(expBalance, balance math.Int)
}{
{
"provider unbonding period elapses first", 2, func(expBalance, balance math.Int) {
// increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
// check that onHold is true
checkStakingUnbondingOps(s, 1, true, true, "unbonding should be on hold")
// check that the unbonding is not complete
s.Require().Equal(expBalance, balance, "unexpected balance after provider unbonding")
// undelegation complete on consumer
unbondConsumer(1)
},
},
{
"consumer unbonding period elapses first", 2, func(expBalance, balance math.Int) {
// undelegation complete on consumer
unbondConsumer(1)
// check that onHold is false
checkStakingUnbondingOps(s, 1, true, false, "unbonding should be not be on hold")
// check that the unbonding is not complete
s.Require().Equal(expBalance, balance, "unexpected balance after consumer unbonding")
// increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
},
},
{
"no valset changes", 1, func(expBalance, balance math.Int) {
// undelegation complete on consumer
unbondConsumer(1)
// check that onHold is false
checkStakingUnbondingOps(s, 1, true, false, "unbonding should be not be on hold")
// check that the unbonding is not complete
s.Require().Equal(expBalance, balance, "unexpected balance after consumer unbonding")
// increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
},
},
}
for i, tc := range testCases {
providerKeeper := s.providerApp.GetProviderKeeper()
consumerKeeper := s.consumerApp.GetConsumerKeeper()
stakingKeeper := s.providerApp.GetTestStakingKeeper()
s.SetupCCVChannel(s.path)
// set VSC timeout period to not trigger the removal of the consumer chain
providerUnbondingPeriod, err := stakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
consumerUnbondingPeriod := consumerKeeper.GetUnbondingPeriod(s.consumerCtx())
providerKeeper.SetVscTimeoutPeriod(s.providerCtx(), providerUnbondingPeriod+consumerUnbondingPeriod+24*time.Hour)
// delegate bondAmt and undelegate tc.shareDiv of it
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
initBalance, valsetUpdateID := delegateAndUndelegate(s, delAddr, bondAmt, tc.shareDiv)
// - check that staking unbonding op was created and onHold is true
checkStakingUnbondingOps(s, 1, true, true, "test: "+tc.name)
// - check that CCV unbonding op was created
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, true, "test: "+tc.name)
// call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// unbond both on provider and consumer and check that
// the balance remains unchanged in between
tc.unbond(initBalance.Sub(bondAmt), getBalance(s, s.providerCtx(), delAddr))
// check that the unbonding operation completed
// - check that ccv unbonding op has been deleted
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, false, "test: "+tc.name)
// - check that staking unbonding op has been deleted
checkStakingUnbondingOps(s, valsetUpdateID, false, false, "test: "+tc.name)
// - check that necessary delegated coins have been returned
unbondAmt := bondAmt.Sub(bondAmt.Quo(math.NewInt(tc.shareDiv)))
s.Require().Equal(
initBalance.Sub(unbondAmt),
getBalance(s, s.providerCtx(), delAddr),
"unexpected initial balance after unbonding; test: %s", tc.name,
)
if i+1 < len(testCases) {
// reset suite to reset provider client
s.SetupTest()
}
}
}
// TestUndelegationVscTimeout tests that an undelegation
// completes after vscTimeoutPeriod even if it does not
// reach maturity on the consumer chain. In this case,
// the consumer chain is removed.
func (s *CCVTestSuite) TestUndelegationVscTimeout() {
providerKeeper := s.providerApp.GetProviderKeeper()
s.SetupCCVChannel(s.path)
// set VSC timeout period to trigger the removal of the consumer chain
vscTimeout := providerKeeper.GetVscTimeoutPeriod(s.providerCtx())
// delegate bondAmt and undelegate 1/2 of it
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
initBalance, valsetUpdateID := delegateAndUndelegate(s, delAddr, bondAmt, 2)
// - check that staking unbonding op was created and onHold is true
checkStakingUnbondingOps(s, 1, true, true)
// - check that CCV unbonding op was created
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, true)
// call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
// check that onHold is true
checkStakingUnbondingOps(s, 1, true, true, "unbonding should be on hold")
// check that the unbonding is not complete
s.Require().Equal(
initBalance.Sub(bondAmt),
getBalance(s, s.providerCtx(), delAddr),
"unexpected balance after provider unbonding")
// increment time
incrementTime(s, vscTimeout)
// check whether the chain was removed
chainID := s.consumerChain.ChainID
_, found := providerKeeper.GetConsumerClientId(s.providerCtx(), chainID)
s.Require().Equal(false, found, "consumer chain was not removed")
// check if the chain was properly removed
s.checkConsumerChainIsRemoved(chainID, true)
// check that the unbonding operation completed
// - check that ccv unbonding op has been deleted
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, false)
// - check that staking unbonding op has been deleted
checkStakingUnbondingOps(s, valsetUpdateID, false, false)
// - check that necessary delegated coins have been returned
unbondAmt := bondAmt.Sub(bondAmt.Quo(math.NewInt(2)))
s.Require().Equal(
initBalance.Sub(unbondAmt),
getBalance(s, s.providerCtx(), delAddr),
"unexpected initial balance after VSC timeout",
)
}
// TestUndelegationDuringInit checks that before the CCV channel is established
// - no undelegations can complete, even if the provider unbonding period elapses
// - all the VSC packets are stored in state as pending
// - if the channel handshake times out, then the undelegation completes
func (s *CCVTestSuite) TestUndelegationDuringInit() {
testCases := []struct {
name string
updateInitTimeoutTimestamp func(*providerkeeper.Keeper, time.Duration)
removed bool
}{
{
"channel handshake completes after unbonding period", func(pk *providerkeeper.Keeper, pUnbondingPeriod time.Duration) {
// change the init timeout timestamp for this consumer chain
// to make sure the chain is not removed before the unbonding period elapses
ts := s.providerCtx().BlockTime().Add(pUnbondingPeriod + 24*time.Hour)
pk.SetInitTimeoutTimestamp(s.providerCtx(), s.consumerChain.ChainID, uint64(ts.UnixNano()))
}, false,
},
{
"channel handshake times out before unbonding period", func(pk *providerkeeper.Keeper, pUnbondingPeriod time.Duration) {
// change the init timeout timestamp for this consumer chain
// to make sure the chain is removed before the unbonding period elapses
ts := s.providerCtx().BlockTime().Add(pUnbondingPeriod - 24*time.Hour)
pk.SetInitTimeoutTimestamp(s.providerCtx(), s.consumerChain.ChainID, uint64(ts.UnixNano()))
}, true,
},
}
for i, tc := range testCases {
providerKeeper := s.providerApp.GetProviderKeeper()
stakingKeeper := s.providerApp.GetTestStakingKeeper()
// delegate bondAmt and undelegate 1/2 of it
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
initBalance, valsetUpdateID := delegateAndUndelegate(s, delAddr, bondAmt, 2)
// - check that staking unbonding op was created and onHold is true
checkStakingUnbondingOps(s, 1, true, true, "test: "+tc.name)
// - check that CCV unbonding op was created
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, true, "test: "+tc.name)
// get provider unbonding period
providerUnbondingPeriod, err := stakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
// update init timeout timestamp
tc.updateInitTimeoutTimestamp(&providerKeeper, providerUnbondingPeriod)
// call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// check that the VSC packet is stored in state as pending
pendingVSCs := providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().Lenf(pendingVSCs, 1, "no pending VSC packet found; test: %s", tc.name)
// delegate again to create another VSC packet
delegate(s, delAddr, bondAmt)
// call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// check that the VSC packet is stored in state as pending
pendingVSCs = providerKeeper.GetPendingVSCPackets(s.providerCtx(), s.consumerChain.ChainID)
s.Require().Lenf(pendingVSCs, 2, "only one pending VSC packet found; test: %s", tc.name)
// increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
// check whether the unbonding op is still there and onHold is true
checkStakingUnbondingOps(s, 1, !tc.removed, true, "test: "+tc.name)
if !tc.removed {
// check that unbonding has not yet completed, i.e., the initBalance
// is still lower by the bond amount, because it has been taken out of
// the delegator's account
s.Require().Equal(
initBalance.Sub(bondAmt).Sub(bondAmt),
getBalance(s, s.providerCtx(), delAddr),
"unexpected initial balance before unbonding; test: %s", tc.name,
)
// complete CCV channel setup
s.SetupCCVChannel(s.path)
// relay VSC packets from provider to consumer
relayAllCommittedPackets(s, s.providerChain, s.path, ccv.ProviderPortID, s.path.EndpointB.ChannelID, 2)
// increment time so that the unbonding period ends on the consumer
incrementTimeByUnbondingPeriod(s, Consumer)
// relay VSCMatured packets from consumer to provider
relayAllCommittedPackets(s, s.consumerChain, s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 2)
// check that the unbonding operation completed
// - check that ccv unbonding op has been deleted
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, false, "test: "+tc.name)
// - check that staking unbonding op has been deleted
checkStakingUnbondingOps(s, valsetUpdateID, false, false, "test: "+tc.name)
// - check that one quarter the delegated coins have been returned
s.Require().Equal(
initBalance.Sub(bondAmt).Sub(bondAmt.Quo(math.NewInt(2))),
getBalance(s, s.providerCtx(), delAddr),
"unexpected initial balance after unbonding; test: %s", tc.name,
)
}
if i+1 < len(testCases) {
// reset suite to reset provider client
s.SetupTest()
}
}
}
// Bond some tokens on provider
// Unbond them to create unbonding op
// Check unbonding ops on both sides
// Advance time so that provider's unbonding op completes
// Check that unbonding has completed in provider staking
func (s *CCVTestSuite) TestUnbondingNoConsumer() {
providerKeeper := s.providerApp.GetProviderKeeper()
providerStakingKeeper := s.providerApp.GetTestStakingKeeper()
// remove all consumer chains, which were already started during setup
for chainID := range s.consumerBundles {
err := providerKeeper.StopConsumerChain(s.providerCtx(), chainID, true)
s.Require().NoError(err)
}
// delegate bondAmt and undelegate 1/2 of it
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
initBalance, valsetUpdateID := delegateAndUndelegate(s, delAddr, bondAmt, 2)
// - check that staking unbonding op was created and onHold is FALSE
checkStakingUnbondingOps(s, 1, true, false)
// - check that CCV unbonding op was NOT created
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, false)
// increment time so that the unbonding period ends on the provider;
// cannot use incrementTimeByUnbondingPeriod() since it tries
// to also update the provider's client on the consumer
providerUnbondingPeriod, err := providerStakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
s.coordinator.IncrementTimeBy(providerUnbondingPeriod + time.Hour)
// call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// check that the unbonding operation completed
// - check that staking unbonding op has been deleted
checkStakingUnbondingOps(s, valsetUpdateID, false, false)
// - check that half the coins have been returned
s.Require().True(getBalance(s, s.providerCtx(), delAddr).Equal(initBalance.Sub(bondAmt.Quo(math.NewInt(2)))))
}
// TestRedelegationNoConsumer tests a redelegate transaction
// submitted on a provider chain with no consumers
func (s *CCVTestSuite) TestRedelegationNoConsumer() {
providerKeeper := s.providerApp.GetProviderKeeper()
stakingKeeper := s.providerApp.GetTestStakingKeeper()
// stop the consumer chain, which was already started during setup
err := providerKeeper.StopConsumerChain(s.providerCtx(), s.consumerChain.ChainID, true)
s.Require().NoError(err)
// Setup delegator, bond amount, and src/dst validators
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
_, srcVal := s.getValByIdx(0)
_, dstVal := s.getValByIdx(1)
delegateAndRedelegate(
s,
delAddr,
srcVal,
dstVal,
bondAmt,
)
// 1 redelegation record should exist for original delegator
redelegations := checkRedelegations(s, delAddr, 1)
unbondingTime, err := stakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
// Check that the only entry has appropriate maturation time, the unbonding period from now
checkRedelegationEntryCompletionTime(
s,
redelegations[0].Entries[0],
s.providerCtx().BlockTime().Add(unbondingTime),
)
// required before call to incrementTimeByUnbondingPeriod or else a panic
// occurs in ibc-go because trusted validators don't match last trusted.
s.providerChain.NextBlock()
// Increment time so that the unbonding period passes on the provider
incrementTimeByUnbondingPeriod(s, Provider)
// Call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// No redelegation records should exist for original delegator anymore
checkRedelegations(s, delAddr, 0)
}
// TestRedelegationWithConsumer tests a redelegate transaction submitted on a provider chain
// when the unbonding period elapses first on the provider chain
func (s *CCVTestSuite) TestRedelegationProviderFirst() {
s.SetupCCVChannel(s.path)
s.SetupTransferChannel()
providerKeeper := s.providerApp.GetProviderKeeper()
consumerKeeper := s.consumerApp.GetConsumerKeeper()
stakingKeeper := s.providerApp.GetTestStakingKeeper()
// set VSC timeout period to not trigger the removal of the consumer chain
providerUnbondingPeriod, err := stakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
consumerUnbondingPeriod := consumerKeeper.GetUnbondingPeriod(s.consumerCtx())
providerKeeper.SetVscTimeoutPeriod(s.providerCtx(), providerUnbondingPeriod+consumerUnbondingPeriod+24*time.Hour)
// Setup delegator, bond amount, and src/dst validators
bondAmt := math.NewInt(10000000)
delAddr := s.providerChain.SenderAccount.GetAddress()
_, srcVal := s.getValByIdx(0)
_, dstVal := s.getValByIdx(1)
delegateAndRedelegate(
s,
delAddr,
srcVal,
dstVal,
bondAmt,
)
// 1 redelegation record should exist for original delegator
redelegations := checkRedelegations(s, delAddr, 1)
unbondingTime, err := stakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
// Check that the only entry has appropriate maturation time, the unbonding period from now
checkRedelegationEntryCompletionTime(
s,
redelegations[0].Entries[0],
s.providerCtx().BlockTime().Add(unbondingTime),
)
// Save the current valset update ID
valsetUpdateID := providerKeeper.GetValidatorSetUpdateId(s.providerCtx())
// Check that CCV unbonding op was created from AfterUnbondingInitiated hook
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, true)
// Call NextBlock on the provider (which increments the height)
s.providerChain.NextBlock()
// Relay 2 VSC packets from provider to consumer (original delegation, and redelegation)
relayAllCommittedPackets(s, s.providerChain, s.path,
ccv.ProviderPortID, s.path.EndpointB.ChannelID, 2)
// Increment time so that the unbonding period ends on the provider
incrementTimeByUnbondingPeriod(s, Provider)
// 1 redelegation record should still exist for original delegator on provider
checkRedelegations(s, delAddr, 1)
// CCV unbonding op should also still exist
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, true)
// Increment time so that the unbonding period ends on the consumer
incrementTimeByUnbondingPeriod(s, Consumer)
// Relay 2 VSCMatured packets from consumer to provider (original delegation and redelegation)
relayAllCommittedPackets(s, s.consumerChain,
s.path, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, 2)
//
// Check that the redelegation operation has now completed on provider
//
// Redelegation record should be deleted for original delegator
checkRedelegations(s, delAddr, 0)
// Check that ccv unbonding op has been deleted
checkCCVUnbondingOp(s, s.providerCtx(), s.consumerChain.ChainID, valsetUpdateID, false)
}