/
staking_helpers.go
319 lines (291 loc) · 10.5 KB
/
staking_helpers.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
package keeper
import (
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// TODO replace methods once available in the sdk `x/stake` module
// Taken from https://github.com/agoric-labs/cosmos-sdk/blob/4085-true-vesting/x/staking/keeper/delegation.go
// GetDelegatorUnbonding returns the total amount a delegator has unbonding
func (k Keeper) GetDelegatorUnbonding(
ctx sdk.Context,
delegator sdk.AccAddress,
) sdk.Int {
unbonding := sdk.ZeroInt()
k.IterateDelegatorUnbondingDelegations(ctx, delegator, func(ubd types.UnbondingDelegation) bool {
for _, entry := range ubd.Entries {
unbonding = unbonding.Add(entry.Balance)
}
return false
})
return unbonding
}
// GetDelegatorBonded returs the total amount a delegator has bonded
func (k Keeper) GetDelegatorBonded(
ctx sdk.Context,
delegator sdk.AccAddress,
) sdk.Int {
bonded := sdk.ZeroInt()
k.IterateDelegatorDelegations(ctx, delegator, func(delegation types.Delegation) bool {
validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err) // shouldn't happen
}
validator, found := k.stakingKeeper.GetValidator(ctx, validatorAddr)
if found {
shares := delegation.Shares
tokens := validator.TokensFromSharesTruncated(shares).RoundInt()
bonded = bonded.Add(tokens)
}
return false
})
return bonded
}
// TransferUnbonding changes the ownership of UnbondingDelegation entries
// until the desired number of tokens have changed hands. Returns the actual
// number of tokens transferred.
func (k Keeper) TransferUnbonding(
ctx sdk.Context,
fromAddr, toAddr sdk.AccAddress,
valAddr sdk.ValAddress,
wantAmt sdk.Int,
) sdk.Int {
transferred := sdk.ZeroInt()
ubdFrom, found := k.stakingKeeper.GetUnbondingDelegation(ctx, fromAddr, valAddr)
if !found {
return transferred
}
ubdFromModified := false
for i := 0; i < len(ubdFrom.Entries) && wantAmt.IsPositive(); i++ {
entry := ubdFrom.Entries[i]
toXfer := entry.Balance
if toXfer.GT(wantAmt) {
toXfer = wantAmt
}
if !toXfer.IsPositive() {
continue
}
if k.stakingKeeper.HasMaxUnbondingDelegationEntries(ctx, toAddr, valAddr) {
// TODO pre-compute the maximum entries we can add rather than checking each time
break
}
ubdTo := k.stakingKeeper.SetUnbondingDelegationEntry(ctx, toAddr, valAddr, entry.CreationHeight, entry.CompletionTime, toXfer)
k.stakingKeeper.InsertUBDQueue(ctx, ubdTo, entry.CompletionTime)
transferred = transferred.Add(toXfer)
wantAmt = wantAmt.Sub(toXfer)
ubdFromModified = true
remaining := entry.Balance.Sub(toXfer)
if remaining.IsZero() {
ubdFrom.RemoveEntry(int64(i))
i--
continue
}
entry.Balance = remaining
ubdFrom.Entries[i] = entry
}
if ubdFromModified {
if len(ubdFrom.Entries) == 0 {
k.stakingKeeper.RemoveUnbondingDelegation(ctx, ubdFrom)
} else {
k.stakingKeeper.SetUnbondingDelegation(ctx, ubdFrom)
}
}
return transferred
}
// TransferDelegation changes the ownership of at most the desired number of shares.
// Returns the actual number of shares transferred. Will also transfer redelegation
// entries to ensure that all redelegations are matched by sufficient shares.
// Note that no tokens are transferred to or from any pool or account, since no
// delegation is actually changing state.
func (k Keeper) TransferDelegation(
ctx sdk.Context,
fromAddr, toAddr sdk.AccAddress,
valAddr sdk.ValAddress,
wantShares sdk.Dec,
) sdk.Dec {
transferred := sdk.ZeroDec()
// sanity checks
if !wantShares.IsPositive() {
return transferred
}
validator, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return transferred
}
delFrom, found := k.stakingKeeper.GetDelegation(ctx, fromAddr, valAddr)
if !found {
return transferred
}
// Check redelegation entry limits while we can still return early.
// Assume the worst case that we need to transfer all redelegation entries
mightExceedLimit := false
k.IterateDelegatorRedelegations(ctx, fromAddr, func(toRedelegation types.Redelegation) (stop bool) {
// There's no redelegation index by delegator and dstVal or vice-versa.
// The minimum cardinality is to look up by delegator, so scan and skip.
if toRedelegation.ValidatorDstAddress != valAddr.String() {
return false
}
fromRedelegation, found := k.stakingKeeper.GetRedelegation(ctx, fromAddr, sdk.ValAddress(toRedelegation.ValidatorSrcAddress), sdk.ValAddress(toRedelegation.ValidatorDstAddress))
if found && len(toRedelegation.Entries)+len(fromRedelegation.Entries) >= int(k.stakingKeeper.MaxEntries(ctx)) {
mightExceedLimit = true
return true
}
return false
})
if mightExceedLimit {
// avoid types.ErrMaxRedelegationEntries
return transferred
}
// compute shares to transfer, amount left behind
transferred = delFrom.Shares
if transferred.GT(wantShares) {
transferred = wantShares
}
remaining := delFrom.Shares.Sub(transferred)
// Update or create the delTo object, calling appropriate hooks
delTo, found := k.stakingKeeper.GetDelegation(ctx, toAddr, validator.GetOperator())
if !found {
delTo = types.NewDelegation(toAddr, validator.GetOperator(), sdk.ZeroDec())
}
if found {
k.stakingKeeper.BeforeDelegationSharesModified(ctx, toAddr, validator.GetOperator())
} else {
k.stakingKeeper.BeforeDelegationCreated(ctx, toAddr, validator.GetOperator())
}
delTo.Shares = delTo.Shares.Add(transferred)
k.stakingKeeper.SetDelegation(ctx, delTo)
k.stakingKeeper.AfterDelegationModified(ctx, toAddr, valAddr)
// Update source delegation
if remaining.IsZero() {
k.stakingKeeper.BeforeDelegationRemoved(ctx, fromAddr, valAddr)
k.stakingKeeper.RemoveDelegation(ctx, delFrom)
} else {
k.stakingKeeper.BeforeDelegationSharesModified(ctx, fromAddr, valAddr)
delFrom.Shares = remaining
k.stakingKeeper.SetDelegation(ctx, delFrom)
k.stakingKeeper.AfterDelegationModified(ctx, fromAddr, valAddr)
}
// If there are not enough remaining shares to be responsible for
// the redelegations, transfer some redelegations.
// For instance, if the original delegation of 300 shares to validator A
// had redelegations for 100 shares each from validators B, C, and D,
// and if we're transferring 175 shares, then we might keep the redelegation
// from B, transfer the one from D, and split the redelegation from C
// keeping a liability for 25 shares and transferring one for 75 shares.
// Of course, the redelegations themselves can have multiple entries for
// different timestamps, so we're actually working at a finer granularity.
redelegations := k.stakingKeeper.GetRedelegations(ctx, fromAddr, math.MaxUint16)
for j, redelegation := range redelegations {
// There's no redelegation index by delegator and dstVal or vice-versa.
// The minimum cardinality is to look up by delegator, so scan and skip.
if redelegation.ValidatorDstAddress != valAddr.String() {
continue
}
redelegationModified := false
entriesRemaining := false
for i := 0; i < len(redelegation.Entries); i++ {
entry := redelegation.Entries[i]
// Partition SharesDst between keeping and sending
sharesToKeep := entry.SharesDst
sharesToSend := sdk.ZeroDec()
if entry.SharesDst.GT(remaining) {
sharesToKeep = remaining
sharesToSend = entry.SharesDst.Sub(sharesToKeep)
}
remaining = remaining.Sub(sharesToKeep) // fewer local shares available to cover liability
if sharesToSend.IsZero() {
// Leave the entry here
entriesRemaining = true
continue
}
if sharesToKeep.IsZero() {
// Transfer the whole entry, delete locally
toRed := k.stakingKeeper.SetRedelegationEntry(
ctx, toAddr, sdk.ValAddress(redelegation.ValidatorSrcAddress),
sdk.ValAddress(redelegation.ValidatorDstAddress),
entry.CreationHeight, entry.CompletionTime, entry.InitialBalance, sdk.ZeroDec(), sharesToSend,
)
k.stakingKeeper.InsertRedelegationQueue(ctx, toRed, entry.CompletionTime)
(&redelegations[j]).RemoveEntry(int64(i))
i--
// okay to leave an obsolete entry in the queue for the removed entry
redelegationModified = true
} else {
// Proportionally divide the entry
fracSending := sharesToSend.Quo(entry.SharesDst)
balanceToSend := fracSending.MulInt(entry.InitialBalance).TruncateInt()
balanceToKeep := entry.InitialBalance.Sub(balanceToSend)
toRed := k.stakingKeeper.SetRedelegationEntry(
ctx, toAddr, sdk.ValAddress(redelegation.ValidatorSrcAddress),
sdk.ValAddress(redelegation.ValidatorDstAddress),
entry.CreationHeight, entry.CompletionTime, balanceToSend, sdk.ZeroDec(), sharesToSend,
)
k.stakingKeeper.InsertRedelegationQueue(ctx, toRed, entry.CompletionTime)
entry.InitialBalance = balanceToKeep
entry.SharesDst = sharesToKeep
redelegation.Entries[i] = entry
// not modifying the completion time, so no need to change the queue
redelegationModified = true
entriesRemaining = true
}
}
if redelegationModified {
if entriesRemaining {
k.stakingKeeper.SetRedelegation(ctx, redelegation)
} else {
k.stakingKeeper.RemoveRedelegation(ctx, redelegation)
}
}
}
return transferred
}
// iterate through a delegator's unbonding delegations
func (k Keeper) IterateDelegatorUnbondingDelegations(
ctx sdk.Context,
delegator sdk.AccAddress,
cb func(ubd types.UnbondingDelegation) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetUBDsKey(delegator))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
ubd := types.MustUnmarshalUBD(k.cdc, iterator.Value())
if cb(ubd) {
break
}
}
}
// IterateDelegatorDelegations iterates through one delegator's delegations
func (k Keeper) IterateDelegatorDelegations(
ctx sdk.Context,
delegator sdk.AccAddress,
cb func(delegation types.Delegation) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetDelegationsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
if cb(delegation) {
break
}
}
}
// iterate through one delegator's redelegations
func (k Keeper) IterateDelegatorRedelegations(
ctx sdk.Context,
delegator sdk.AccAddress,
fn func(red types.Redelegation) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetREDsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
red := types.MustUnmarshalRED(k.cdc, iterator.Value())
if stop := fn(red); stop {
break
}
}
}