-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
unbonding.go
419 lines (339 loc) · 10.8 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
package keeper
import (
"encoding/binary"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// Increments and returns a unique ID for an unbonding operation
func (k Keeper) IncrementUnbondingId(ctx sdk.Context) (unbondingId uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.UnbondingIdKey)
if bz == nil {
unbondingId = 0
} else {
unbondingId = binary.BigEndian.Uint64(bz)
}
unbondingId = unbondingId + 1
// Convert back into bytes for storage
bz = make([]byte, 8)
binary.BigEndian.PutUint64(bz, unbondingId)
store.Set(types.UnbondingIdKey, bz)
return unbondingId
}
// Remove a mapping from UnbondingId to unbonding operation
func (k Keeper) DeleteUnbondingIndex(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetUnbondingIndexKey(id))
}
// return a unbonding delegation that has an unbonding delegation entry with a certain ID
func (k Keeper) GetUnbondingDelegationByUnbondingId(
ctx sdk.Context, id uint64,
) (ubd types.UnbondingDelegation, found bool) {
store := ctx.KVStore(k.storeKey)
ubdeKey := store.Get(types.GetUnbondingIndexKey(id))
if ubdeKey == nil {
return types.UnbondingDelegation{}, false
}
value := store.Get(ubdeKey)
if value == nil {
return types.UnbondingDelegation{}, false
}
ubd, err := types.UnmarshalUBD(k.cdc, value)
// An error here means that what we got wasn't the right type
if err != nil {
return types.UnbondingDelegation{}, false
}
return ubd, true
}
// return a unbonding delegation that has an unbonding delegation entry with a certain ID
func (k Keeper) GetRedelegationByUnbondingId(
ctx sdk.Context, id uint64,
) (red types.Redelegation, found bool) {
store := ctx.KVStore(k.storeKey)
redKey := store.Get(types.GetUnbondingIndexKey(id))
if redKey == nil {
return types.Redelegation{}, false
}
value := store.Get(redKey)
if value == nil {
return types.Redelegation{}, false
}
red, err := types.UnmarshalRED(k.cdc, value)
// An error here means that what we got wasn't the right type
if err != nil {
return types.Redelegation{}, false
}
return red, true
}
// return the validator that is unbonding with a certain unbonding op ID
func (k Keeper) GetValidatorByUnbondingId(
ctx sdk.Context, id uint64,
) (val types.Validator, found bool) {
store := ctx.KVStore(k.storeKey)
valKey := store.Get(types.GetUnbondingIndexKey(id))
if valKey == nil {
return types.Validator{}, false
}
value := store.Get(valKey)
if value == nil {
return types.Validator{}, false
}
val, err := types.UnmarshalValidator(k.cdc, value)
// An error here means that what we got wasn't the right type
if err != nil {
return types.Validator{}, false
}
return val, true
}
// Set an index to look up an UnbondingDelegation by the unbondingId of an UnbondingDelegationEntry that it contains
func (k Keeper) SetUnbondingDelegationByUnbondingId(ctx sdk.Context, ubd types.UnbondingDelegation, id uint64) {
store := ctx.KVStore(k.storeKey)
delAddr, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress)
if err != nil {
panic(err)
}
valAddr, err := sdk.ValAddressFromBech32(ubd.ValidatorAddress)
if err != nil {
panic(err)
}
ubdKey := types.GetUBDKey(delAddr, valAddr)
store.Set(types.GetUnbondingIndexKey(id), ubdKey)
}
// Set an index to look up an Redelegation by the unbondingId of an RedelegationEntry that it contains
func (k Keeper) SetRedelegationByUnbondingId(ctx sdk.Context, red types.Redelegation, id uint64) {
store := ctx.KVStore(k.storeKey)
delAddr, err := sdk.AccAddressFromBech32(red.DelegatorAddress)
if err != nil {
panic(err)
}
valSrcAddr, err := sdk.ValAddressFromBech32(red.ValidatorSrcAddress)
if err != nil {
panic(err)
}
valDstAddr, err := sdk.ValAddressFromBech32(red.ValidatorDstAddress)
if err != nil {
panic(err)
}
redKey := types.GetREDKey(delAddr, valSrcAddr, valDstAddr)
store.Set(types.GetUnbondingIndexKey(id), redKey)
}
// Set an index to look up a Validator by the unbondingId corresponding to its current unbonding
func (k Keeper) SetValidatorByUnbondingId(ctx sdk.Context, val types.Validator, id uint64) {
store := ctx.KVStore(k.storeKey)
valAddr, err := sdk.ValAddressFromBech32(val.OperatorAddress)
if err != nil {
panic(err)
}
valKey := types.GetValidatorKey(valAddr)
store.Set(types.GetUnbondingIndexKey(id), valKey)
}
// unbondingDelegationEntryArrayIndex and redelegationEntryArrayIndex are utilities to find
// at which position in the Entries array the entry with a given id is
// ----------------------------------------------------------------------------------------
func unbondingDelegationEntryArrayIndex(ubd types.UnbondingDelegation, id uint64) (index int, found bool) {
for i, entry := range ubd.Entries {
// we find the entry with the right ID
if entry.UnbondingId == id {
return i, true
}
}
return 0, false
}
func redelegationEntryArrayIndex(red types.Redelegation, id uint64) (index int, found bool) {
for i, entry := range red.Entries {
// we find the entry with the right ID
if entry.UnbondingId == id {
return i, true
}
}
return 0, false
}
// UnbondingCanComplete allows a stopped unbonding operation, such as an
// unbonding delegation, a redelegation, or a validator unbonding to complete.
// In order for the unbonding operation with `id` to eventually complete, every call
// to PutUnbondingOnHold(id) must be matched by a call to UnbondingCanComplete(id).
// ----------------------------------------------------------------------------------------
func (k Keeper) UnbondingCanComplete(ctx sdk.Context, id uint64) error {
found, err := k.unbondingDelegationEntryCanComplete(ctx, id)
if err != nil {
return err
}
if found {
return nil
}
found, err = k.redelegationEntryCanComplete(ctx, id)
if err != nil {
return err
}
if found {
return nil
}
found, err = k.validatorUnbondingCanComplete(ctx, id)
if err != nil {
return err
}
if found {
return nil
}
// If an entry was not found
return types.ErrUnbondingNotFound
}
func (k Keeper) unbondingDelegationEntryCanComplete(ctx sdk.Context, id uint64) (found bool, err error) {
ubd, found := k.GetUnbondingDelegationByUnbondingId(ctx, id)
if !found {
return false, nil
}
i, found := unbondingDelegationEntryArrayIndex(ubd, id)
if !found {
return false, nil
}
// The entry must be on hold
if !ubd.Entries[i].OnHold() {
return true,
sdkerrors.Wrapf(
types.ErrUnbondingOnHoldRefCountNegative,
"undelegation unbondingId(%d), expecting UnbondingOnHoldRefCount > 0, got %T",
id, ubd.Entries[i].UnbondingOnHoldRefCount,
)
}
ubd.Entries[i].UnbondingOnHoldRefCount--
// Check if entry is matured.
if !ubd.Entries[i].OnHold() && ubd.Entries[i].IsMature(ctx.BlockHeader().Time) {
// If matured, complete it.
delegatorAddress, err := sdk.AccAddressFromBech32(ubd.DelegatorAddress)
if err != nil {
return false, err
}
bondDenom := k.GetParams(ctx).BondDenom
// track undelegation only when remaining or truncated shares are non-zero
if !ubd.Entries[i].Balance.IsZero() {
amt := sdk.NewCoin(bondDenom, ubd.Entries[i].Balance)
if err := k.bankKeeper.UndelegateCoinsFromModuleToAccount(
ctx, types.NotBondedPoolName, delegatorAddress, sdk.NewCoins(amt),
); err != nil {
return false, err
}
}
// Remove entry
ubd.RemoveEntry(int64(i))
// Remove from the UnbondingIndex
k.DeleteUnbondingIndex(ctx, id)
}
// set the unbonding delegation or remove it if there are no more entries
if len(ubd.Entries) == 0 {
k.RemoveUnbondingDelegation(ctx, ubd)
} else {
k.SetUnbondingDelegation(ctx, ubd)
}
// Successfully completed unbonding
return true, nil
}
func (k Keeper) redelegationEntryCanComplete(ctx sdk.Context, id uint64) (found bool, err error) {
red, found := k.GetRedelegationByUnbondingId(ctx, id)
if !found {
return false, nil
}
i, found := redelegationEntryArrayIndex(red, id)
if !found {
return false, nil
}
// The entry must be on hold
if !red.Entries[i].OnHold() {
return true,
sdkerrors.Wrapf(
types.ErrUnbondingOnHoldRefCountNegative,
"redelegation unbondingId(%d), expecting UnbondingOnHoldRefCount > 0, got %T",
id, red.Entries[i].UnbondingOnHoldRefCount,
)
}
red.Entries[i].UnbondingOnHoldRefCount--
if !red.Entries[i].OnHold() && red.Entries[i].IsMature(ctx.BlockHeader().Time) {
// If matured, complete it.
// Remove entry
red.RemoveEntry(int64(i))
// Remove from the Unbonding index
k.DeleteUnbondingIndex(ctx, id)
}
// set the redelegation or remove it if there are no more entries
if len(red.Entries) == 0 {
k.RemoveRedelegation(ctx, red)
} else {
k.SetRedelegation(ctx, red)
}
// Successfully completed unbonding
return true, nil
}
func (k Keeper) validatorUnbondingCanComplete(ctx sdk.Context, id uint64) (found bool, err error) {
val, found := k.GetValidatorByUnbondingId(ctx, id)
if !found {
return false, nil
}
if val.UnbondingOnHoldRefCount <= 0 {
return true,
sdkerrors.Wrapf(
types.ErrUnbondingOnHoldRefCountNegative,
"val(%s), expecting UnbondingOnHoldRefCount > 0, got %T",
val.OperatorAddress, val.UnbondingOnHoldRefCount,
)
}
val.UnbondingOnHoldRefCount--
k.SetValidator(ctx, val)
return true, nil
}
// PutUnbondingOnHold allows an external module to stop an unbonding operation,
// such as an unbonding delegation, a redelegation, or a validator unbonding.
// In order for the unbonding operation with `id` to eventually complete, every call
// to PutUnbondingOnHold(id) must be matched by a call to UnbondingCanComplete(id).
// ----------------------------------------------------------------------------------------
func (k Keeper) PutUnbondingOnHold(ctx sdk.Context, id uint64) error {
found := k.putUnbondingDelegationEntryOnHold(ctx, id)
if found {
return nil
}
found = k.putRedelegationEntryOnHold(ctx, id)
if found {
return nil
}
found = k.putValidatorOnHold(ctx, id)
if found {
return nil
}
// If an entry was not found
return types.ErrUnbondingNotFound
}
func (k Keeper) putUnbondingDelegationEntryOnHold(ctx sdk.Context, id uint64) (found bool) {
ubd, found := k.GetUnbondingDelegationByUnbondingId(ctx, id)
if !found {
return false
}
i, found := unbondingDelegationEntryArrayIndex(ubd, id)
if !found {
return false
}
ubd.Entries[i].UnbondingOnHoldRefCount++
k.SetUnbondingDelegation(ctx, ubd)
return true
}
func (k Keeper) putRedelegationEntryOnHold(ctx sdk.Context, id uint64) (found bool) {
red, found := k.GetRedelegationByUnbondingId(ctx, id)
if !found {
return false
}
i, found := redelegationEntryArrayIndex(red, id)
if !found {
return false
}
red.Entries[i].UnbondingOnHoldRefCount++
k.SetRedelegation(ctx, red)
return true
}
func (k Keeper) putValidatorOnHold(ctx sdk.Context, id uint64) (found bool) {
val, found := k.GetValidatorByUnbondingId(ctx, id)
if !found {
return false
}
val.UnbondingOnHoldRefCount++
k.SetValidator(ctx, val)
return true
}