-
Notifications
You must be signed in to change notification settings - Fork 834
/
clawback_vesting_account_test.go
505 lines (483 loc) · 17.3 KB
/
clawback_vesting_account_test.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package types_test
import (
"testing"
"time"
tmtime "github.com/cometbft/cometbft/types/time"
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
utiltx "github.com/evmos/evmos/v18/testutil/tx"
"github.com/evmos/evmos/v18/x/vesting/types"
)
var (
stakeDenom = "stake"
feeDenom = "fee"
lockupPeriods = sdkvesting.Periods{
sdkvesting.Period{
Length: int64(16 * 60 * 60), // 16hs
Amount: sdk.NewCoins(sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)),
},
}
vestingPeriods = sdkvesting.Periods{
sdkvesting.Period{
Length: int64(12 * 60 * 60), // 12hs
Amount: getPercentOfVestingCoins(50),
},
sdkvesting.Period{
Length: int64(6 * 60 * 60), // 6hs
Amount: getPercentOfVestingCoins(25),
},
sdkvesting.Period{
Length: int64(6 * 60 * 60), // 6hs
Amount: getPercentOfVestingCoins(25),
},
}
origCoins = sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)}
)
type VestingAccountTestSuite struct {
suite.Suite
}
func TestVestingAccountSuite(t *testing.T) {
suite.Run(t, new(VestingAccountTestSuite))
}
func (suite *VestingAccountTestSuite) TestClawbackAccountNew() {
addr := sdk.AccAddress("test_address")
baseAcc := authtypes.NewBaseAccountWithAddress(addr)
initialVesting := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 50))
testCases := []struct {
name string
acc authtypes.GenesisAccount
expErr bool
expErrMsg string
}{
{
name: "Clawback vesting account - pass",
acc: types.NewClawbackVestingAccount(
baseAcc,
sdk.AccAddress("the funder"),
initialVesting,
time.Now(),
sdkvesting.Periods{sdkvesting.Period{Length: 101, Amount: initialVesting}},
sdkvesting.Periods{sdkvesting.Period{Length: 201, Amount: initialVesting}},
),
expErr: false,
},
{
name: "Clawback vesting account - invalid vesting end",
acc: &types.ClawbackVestingAccount{
BaseVestingAccount: &sdkvesting.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: initialVesting,
EndTime: 50,
},
FunderAddress: "funder",
StartTime: time.Unix(100, 0),
LockupPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
VestingPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
},
expErr: true,
expErrMsg: "vesting start-time must be before end-time",
},
{
name: "Clawback vesting account - lockup too long",
acc: &types.ClawbackVestingAccount{
BaseVestingAccount: &sdkvesting.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: initialVesting,
EndTime: 60,
},
FunderAddress: "funder",
StartTime: time.Unix(50, 0),
LockupPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 20, Amount: initialVesting}},
VestingPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
},
expErr: true,
expErrMsg: "lockup schedule extends beyond account end time",
},
{
name: "Clawback vesting account - invalid lockup coins",
acc: &types.ClawbackVestingAccount{
BaseVestingAccount: &sdkvesting.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: initialVesting,
EndTime: 120,
},
FunderAddress: "funder",
StartTime: time.Unix(100, 0),
LockupPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting.Add(initialVesting...)}},
VestingPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
},
expErr: true,
expErrMsg: "original vesting coins does not match the sum of all coins in lockup periods",
},
{
name: "Clawback vesting account - vesting too long",
acc: &types.ClawbackVestingAccount{
BaseVestingAccount: &sdkvesting.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: initialVesting,
EndTime: 110,
},
FunderAddress: "funder",
StartTime: time.Unix(100, 0),
LockupPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
VestingPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 20, Amount: initialVesting}},
},
expErr: true,
expErrMsg: "vesting schedule extends beyond account end time",
},
{
name: "Clawback vesting account - invalid vesting coins",
acc: &types.ClawbackVestingAccount{
BaseVestingAccount: &sdkvesting.BaseVestingAccount{
BaseAccount: baseAcc,
OriginalVesting: initialVesting,
EndTime: 120,
},
FunderAddress: "funder",
StartTime: time.Unix(100, 0),
LockupPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting}},
VestingPeriods: sdkvesting.Periods{sdkvesting.Period{Length: 10, Amount: initialVesting.Add(initialVesting...)}},
},
expErr: true,
expErrMsg: "original vesting coins does not match the sum of all coins in vesting periods",
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
err := tc.acc.Validate()
if tc.expErr {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expErrMsg)
return
}
suite.Require().NoError(err)
})
}
}
func (suite *VestingAccountTestSuite) TestGetCoinsFunctions() {
now := tmtime.Now()
endTime := now.Add(24 * time.Hour)
addr := sdk.AccAddress(utiltx.GenerateAddress().Bytes())
bacc := authtypes.NewBaseAccountWithAddress(addr)
va := types.NewClawbackVestingAccount(bacc, sdk.AccAddress([]byte("funder")), origCoins, now, lockupPeriods, vestingPeriods)
testCases := []struct {
name string
time time.Time
expVestedCoins sdk.Coins
expLockedUpVestedCoins sdk.Coins
expUnlockedVestedCoins sdk.Coins
expUnvestedCoins sdk.Coins
expLockedUpCoins sdk.Coins
expUnlockedCoins sdk.Coins
expNotSpendable sdk.Coins
}{
{
name: "no coins vested at the beginning of the vesting schedule, all locked",
time: now,
expVestedCoins: sdk.Coins{},
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: sdk.Coins{},
expUnvestedCoins: origCoins,
expLockedUpCoins: origCoins,
expUnlockedCoins: sdk.Coins{},
expNotSpendable: origCoins,
},
{
name: "all coins vested and unlocked at the end of the vesting schedule",
time: endTime,
expVestedCoins: origCoins,
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: origCoins,
expUnvestedCoins: sdk.Coins{},
expLockedUpCoins: sdk.Coins{},
expUnlockedCoins: origCoins,
expNotSpendable: sdk.Coins{},
},
{
name: "no coins vested during first vesting period, all still locked",
time: now.Add(6 * time.Hour),
expVestedCoins: sdk.Coins{},
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: sdk.Coins{},
expUnvestedCoins: origCoins,
expLockedUpCoins: origCoins,
expUnlockedCoins: sdk.Coins{},
expNotSpendable: origCoins,
},
{
name: "50 percent of coins are vested after 1st vesting period, but before unlocking (all locked coins)",
time: now.Add(12 * time.Hour),
expVestedCoins: getPercentOfVestingCoins(50),
expLockedUpVestedCoins: getPercentOfVestingCoins(50),
expUnlockedVestedCoins: sdk.Coins{},
expUnvestedCoins: getPercentOfVestingCoins(50),
expLockedUpCoins: origCoins,
expUnlockedCoins: sdk.Coins{},
expNotSpendable: origCoins,
},
{
name: "after lockup period (all coins unlocked) - 50 percent of coins already vested",
time: now.Add(16 * time.Hour),
expVestedCoins: getPercentOfVestingCoins(50),
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: getPercentOfVestingCoins(50),
expUnvestedCoins: getPercentOfVestingCoins(50),
expLockedUpCoins: sdk.Coins{},
expUnlockedCoins: origCoins,
expNotSpendable: getPercentOfVestingCoins(50),
},
{
name: "in between vesting periods 1 and 2 - no new coins don't vested",
time: now.Add(17 * time.Hour),
expVestedCoins: getPercentOfVestingCoins(50),
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: getPercentOfVestingCoins(50),
expUnvestedCoins: getPercentOfVestingCoins(50),
expLockedUpCoins: sdk.Coins{},
expUnlockedCoins: origCoins,
expNotSpendable: getPercentOfVestingCoins(50),
},
{
name: "75 percent of coins vested after period 2",
time: now.Add(18 * time.Hour),
expVestedCoins: getPercentOfVestingCoins(75),
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: getPercentOfVestingCoins(75),
expUnvestedCoins: getPercentOfVestingCoins(25),
expLockedUpCoins: sdk.Coins{},
expUnlockedCoins: origCoins,
expNotSpendable: getPercentOfVestingCoins(25),
},
{
name: "100 percent of coins vested",
time: now.Add(48 * time.Hour),
expVestedCoins: origCoins,
expLockedUpVestedCoins: sdk.Coins{},
expUnlockedVestedCoins: origCoins,
expUnvestedCoins: sdk.Coins{},
expLockedUpCoins: sdk.Coins{},
expUnlockedCoins: origCoins,
expNotSpendable: sdk.Coins{},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
vestedCoins := va.GetVestedCoins(tc.time)
suite.Require().Equal(tc.expVestedCoins, vestedCoins)
lockedUpVested := va.GetLockedUpVestedCoins(tc.time)
suite.Require().Equal(tc.expLockedUpVestedCoins, lockedUpVested)
unlockedVestedCoins := va.GetUnlockedVestedCoins(tc.time)
suite.Require().Equal(tc.expUnlockedVestedCoins, unlockedVestedCoins)
unvestedCoins := va.GetVestingCoins(tc.time)
suite.Require().Equal(tc.expUnvestedCoins, unvestedCoins)
lockedUpCoins := va.GetLockedUpCoins(tc.time)
suite.Require().Equal(tc.expLockedUpCoins, lockedUpCoins)
unlockedCoins := va.GetUnlockedCoins(tc.time)
suite.Require().Equal(tc.expUnlockedCoins, unlockedCoins)
suite.Require().Equal(tc.expNotSpendable, va.LockedCoins(tc.time))
})
}
}
func (suite *VestingAccountTestSuite) TestTrackDelegationUndelegation() {
now := tmtime.Now()
endTime := now.Add(24 * time.Hour)
testCases := []struct {
name string
delegate func(*types.ClawbackVestingAccount)
expDelegatedFree sdk.Coins
undelegate func(*types.ClawbackVestingAccount)
expUndelegatedFree sdk.Coins
expDelegationPanic bool
expUndelegationPanic bool
}{
{
"delegate and undelegated all vested coins",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(endTime, origCoins, origCoins)
},
origCoins,
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(origCoins)
},
sdk.Coins{},
false,
false,
},
{
"delegate and undelegate half of vested coins",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(now, origCoins, vestingPeriods[0].Amount)
},
vestingPeriods[0].Amount,
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(vestingPeriods[0].Amount)
},
sdk.Coins{},
false,
false,
},
{
"no modifications when delegation amount is zero or not enough funds",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(now, origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 1000000)})
},
vestingPeriods[0].Amount,
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(vestingPeriods[0].Amount)
},
sdk.Coins{},
true,
false,
},
{
"no modifications when undelegation amount is zero or not enough funds",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(now, origCoins, origCoins)
},
vestingPeriods[0].Amount,
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 0)})
},
sdk.Coins{},
false,
true,
},
{
"vest 50% and delegate to two validator and undelegate from one validator that got slashed 50%",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(now.Add(17*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)})
va.TrackDelegation(now.Add(17*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)})
},
sdk.Coins{sdk.NewInt64Coin(stakeDenom, 100)},
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)})
},
sdk.Coins{sdk.NewInt64Coin(stakeDenom, 75)},
false,
false,
},
{
"vest 50% and delegate to two validator and undelegate from one validator that got slashed 50% and undelegate from the other validator that did not get slashed",
func(va *types.ClawbackVestingAccount) {
va.TrackDelegation(now.Add(17*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)})
va.TrackDelegation(now.Add(17*time.Hour), origCoins, sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)})
},
sdk.Coins{sdk.NewInt64Coin(stakeDenom, 100)},
func(va *types.ClawbackVestingAccount) {
va.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)})
va.TrackUndelegation(sdk.Coins{sdk.NewInt64Coin(stakeDenom, 50)})
},
sdk.Coins{sdk.NewInt64Coin(stakeDenom, 25)},
false,
false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
addr := sdk.AccAddress(utiltx.GenerateAddress().Bytes())
bacc := authtypes.NewBaseAccountWithAddress(addr)
va := types.NewClawbackVestingAccount(bacc, sdk.AccAddress([]byte("funder")), origCoins, now, lockupPeriods, vestingPeriods)
if tc.expDelegationPanic { //nolint:gocritic
suite.Require().Panics(func() {
tc.delegate(va)
})
} else if tc.expUndelegationPanic {
suite.Require().Panics(func() {
tc.undelegate(va)
})
} else {
var emptyCoins sdk.Coins
// Track Delegation
tc.delegate(va)
suite.Require().Equal(emptyCoins, va.DelegatedVesting)
suite.Require().Equal(tc.expDelegatedFree, va.DelegatedFree)
// Track Undelegation
tc.undelegate(va)
suite.Require().Equal(emptyCoins, va.DelegatedVesting)
suite.Require().Equal(tc.expUndelegatedFree, va.DelegatedFree)
}
})
}
}
func (suite *VestingAccountTestSuite) TestComputeClawback() {
fee := func(x int64) sdk.Coin { return sdk.NewInt64Coin(feeDenom, x) }
stake := func(x int64) sdk.Coin { return sdk.NewInt64Coin(stakeDenom, x) }
now := tmtime.Now()
lockupPeriods := sdkvesting.Periods{
{Length: int64(12 * 3600), Amount: sdk.NewCoins(fee(1000), stake(100))}, // noon
}
vestingPeriods := sdkvesting.Periods{
{Length: int64(8 * 3600), Amount: sdk.NewCoins(fee(200))}, // 8am
{Length: int64(1 * 3600), Amount: sdk.NewCoins(fee(200), stake(50))}, // 9am
{Length: int64(6 * 3600), Amount: sdk.NewCoins(fee(200), stake(50))}, // 3pm
{Length: int64(2 * 3600), Amount: sdk.NewCoins(fee(200))}, // 5pm
{Length: int64(1 * 3600), Amount: sdk.NewCoins(fee(200))}, // 6pm
}
testCases := []struct {
name string
time int64
expClawedBack sdk.Coins
expOriginalVesting sdk.Coins
expLockupPeriods sdkvesting.Periods
expVestingPeriods sdkvesting.Periods
}{
{
"should claw back everything if clawed back before start time",
now.Add(-time.Hour).Unix(),
origCoins,
sdk.Coins{},
sdkvesting.Periods{},
sdkvesting.Periods{},
},
{
"should clawback everything before any vesting or lockup period passes",
now.Unix(),
sdk.NewCoins(fee(1000), stake(100)),
sdk.Coins{},
sdkvesting.Periods{},
sdkvesting.Periods{},
},
{
"it should clawback after two vesting periods and before the first lock period",
now.Add(11 * time.Hour).Unix(),
sdk.Coins{fee(600), stake(50)}, // last 3 periods are still vesting
sdk.Coins{fee(400), stake(50)}, // first 2 periods
sdkvesting.Periods{{Length: int64(12 * 3600), Amount: sdk.NewCoins(fee(400), stake(50))}},
vestingPeriods[:2],
},
{
"should clawback zero coins after all vesting and locked periods",
now.Add(23 * time.Hour).Unix(),
sdk.Coins{},
sdk.Coins{fee(1000), stake(100)},
lockupPeriods,
vestingPeriods,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
addr := sdk.AccAddress(utiltx.GenerateAddress().Bytes())
bacc := authtypes.NewBaseAccountWithAddress(addr)
va := types.NewClawbackVestingAccount(bacc, sdk.AccAddress([]byte("funder")), origCoins, now, lockupPeriods, vestingPeriods)
va2, amt := va.ComputeClawback(tc.time)
suite.Require().Equal(tc.expClawedBack, amt)
suite.Require().Equal(tc.expOriginalVesting, va2.OriginalVesting)
suite.Require().Equal(tc.expLockupPeriods, va2.LockupPeriods)
suite.Require().Equal(tc.expVestingPeriods, va2.VestingPeriods)
})
}
}
// getPercentOfVestingCoins is a helper function to calculate
// the specified percentage of the coins in the vesting schedule
func getPercentOfVestingCoins(percentage int64) sdk.Coins {
if percentage < 0 || percentage > 100 {
panic("invalid percentage passed!")
}
var retCoins sdk.Coins
for _, coin := range origCoins {
retCoins = retCoins.Add(sdk.NewCoin(coin.Denom, coin.Amount.MulRaw(percentage).QuoRaw(100)))
}
return retCoins
}