/
validator_selection.go
323 lines (279 loc) · 12.7 KB
/
validator_selection.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
package keeper
import (
"fmt"
"sort"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gogoproto/proto"
"github.com/Stride-Labs/stride/v15/utils"
epochstypes "github.com/Stride-Labs/stride/v15/x/epochs/types"
"github.com/Stride-Labs/stride/v15/x/stakeibc/types"
)
const RebalanceIcaBatchSize = 5
type RebalanceValidatorDelegationChange struct {
ValidatorAddress string
Delta sdkmath.Int
}
// Iterate each active host zone and issues redelegation messages to rebalance each
// validator's stake according to their weights
//
// This is required when accepting LSM LiquidStakes as the distribution of stake
// from the LSM Tokens will be inconsistend with the host zone's validator set
//
// Note: this cannot be run more than once in a single unbonding period
func (k Keeper) RebalanceAllHostZones(ctx sdk.Context) {
dayEpoch, found := k.GetEpochTracker(ctx, epochstypes.DAY_EPOCH)
if !found {
k.Logger(ctx).Error("Unable to get day epoch tracker")
return
}
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
if dayEpoch.EpochNumber%hostZone.UnbondingPeriod != 0 {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId,
"Host does not rebalance this epoch (Unbonding Period: %d, Epoch: %d)", hostZone.UnbondingPeriod, dayEpoch.EpochNumber))
continue
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Rebalancing delegations"))
if err := k.RebalanceDelegationsForHostZone(ctx, hostZone.ChainId); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Unable to rebalance delegations for %s: %s", hostZone.ChainId, err.Error()))
continue
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Successfully rebalanced delegations"))
}
}
// Rebalance validators according to their validator weights for a specific host zone
func (k Keeper) RebalanceDelegationsForHostZone(ctx sdk.Context, chainId string) error {
// Get the host zone and confirm the delegation account is initialized
hostZone, found := k.GetHostZone(ctx, chainId)
if !found {
return errorsmod.Wrap(types.ErrHostZoneNotFound, fmt.Sprintf("Host zone %s not found", chainId))
}
if hostZone.DelegationIcaAddress == "" {
return errorsmod.Wrapf(types.ErrICAAccountNotFound, "no delegation account found for %s", chainId)
}
// Get the difference between the actual and expected validator delegations
valDeltaList, err := k.GetValidatorDelegationDifferences(ctx, hostZone)
if err != nil {
return errorsmod.Wrapf(err, "unable to get validator deltas for host zone %s", chainId)
}
msgs, rebalancings := k.GetRebalanceICAMessages(hostZone, valDeltaList)
for start := 0; start < len(msgs); start += RebalanceIcaBatchSize {
end := start + RebalanceIcaBatchSize
if end > len(msgs) {
end = len(msgs)
}
msgsBatch := msgs[start:end]
rebalancingsBatch := rebalancings[start:end]
// marshall the callback
rebalanceCallback := types.RebalanceCallback{
HostZoneId: hostZone.ChainId,
Rebalancings: rebalancingsBatch,
}
rebalanceCallbackBz, err := k.MarshalRebalanceCallbackArgs(ctx, rebalanceCallback)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal rebalance callback args")
}
// Submit the rebalance ICA
_, err = k.SubmitTxsStrideEpoch(
ctx,
hostZone.ConnectionId,
msgsBatch,
types.ICAAccountType_DELEGATION,
ICACallbackID_Rebalance,
rebalanceCallbackBz,
)
if err != nil {
return errorsmod.Wrapf(err, "Failed to SubmitTxs for %s, messages: %+v", hostZone.ChainId, msgs)
}
// flag the delegation change in progress on each validator
for _, rebalancing := range rebalancingsBatch {
if err := k.IncrementValidatorDelegationChangesInProgress(&hostZone, rebalancing.SrcValidator); err != nil {
return err
}
if err := k.IncrementValidatorDelegationChangesInProgress(&hostZone, rebalancing.DstValidator); err != nil {
return err
}
}
k.SetHostZone(ctx, hostZone)
}
return nil
}
// Given a list of target delegation changes, builds the individual re-delegation messages by redelegating
// from surplus validators to deficit validators
// Returns the list of messages and the callback data for the ICA
func (k Keeper) GetRebalanceICAMessages(
hostZone types.HostZone,
validatorDeltas []RebalanceValidatorDelegationChange,
) (msgs []proto.Message, rebalancings []*types.Rebalancing) {
// Sort the list of delegation changes by the size of the change
// Sort descending so the surplus validators appear first
lessFunc := func(i, j int) bool {
if !validatorDeltas[i].Delta.Equal(validatorDeltas[j].Delta) {
return validatorDeltas[i].Delta.GT(validatorDeltas[j].Delta)
}
// use name as a tie breaker if deltas are equal
return validatorDeltas[i].ValidatorAddress < validatorDeltas[j].ValidatorAddress
}
sort.SliceStable(validatorDeltas, lessFunc)
// Pair surplus and deficit validators, with a redelegation from the surplus
// validator to the deficit one
// The list is sorted with the surplus validators (who should lose stake) at index 0
// and the deficit validators (who should gain stake) at index N-1
// The surplus validator's have a positive delta and the deficit validators have a negative delta
surplusIndex := 0
deficitIndex := len(validatorDeltas) - 1
for surplusIndex <= deficitIndex {
// surplus validator delta is positive, deficit validator delta is negative
deficitValidator := validatorDeltas[deficitIndex]
surplusValidator := validatorDeltas[surplusIndex]
// If the indicies flipped, or either delta is 0, we're done rebalancing
if surplusIndex > deficitIndex || deficitValidator.Delta.IsZero() || surplusValidator.Delta.IsZero() {
break
}
var redelegationAmount sdkmath.Int
if deficitValidator.Delta.Abs().GT(surplusValidator.Delta.Abs()) {
// If the deficit validator needs more stake than the surplus validator has to give,
// transfer the full surplus to deficit validator
redelegationAmount = surplusValidator.Delta.Abs()
// Update the deficit validator, and zero out the surplus validator
validatorDeltas[deficitIndex].Delta = deficitValidator.Delta.Add(redelegationAmount)
validatorDeltas[surplusIndex].Delta = sdkmath.ZeroInt()
surplusIndex += 1
} else if surplusValidator.Delta.Abs().GT(deficitValidator.Delta.Abs()) {
// If one validator's deficit is less than the other validator's surplus,
// move only enough of the surplus to cover the shortage
redelegationAmount = deficitValidator.Delta.Abs()
// Update the surplus validator, and zero out the deficit validator
validatorDeltas[surplusIndex].Delta = surplusValidator.Delta.Sub(redelegationAmount)
validatorDeltas[deficitIndex].Delta = sdkmath.ZeroInt()
deficitIndex -= 1
} else {
// if one validator's surplus is equal to the other validator's deficit,
// we'll transfer that amount and both validators will now be balanced
redelegationAmount = deficitValidator.Delta.Abs()
validatorDeltas[surplusIndex].Delta = sdkmath.ZeroInt()
validatorDeltas[deficitIndex].Delta = sdkmath.ZeroInt()
surplusIndex += 1
deficitIndex -= 1
}
// Append the new Redelegation message and Rebalancing struct for the callback
// We always send from the surplus validator to the deficit validator
srcValidator := surplusValidator.ValidatorAddress
dstValidator := deficitValidator.ValidatorAddress
msgs = append(msgs, &stakingtypes.MsgBeginRedelegate{
DelegatorAddress: hostZone.DelegationIcaAddress,
ValidatorSrcAddress: srcValidator,
ValidatorDstAddress: dstValidator,
Amount: sdk.NewCoin(hostZone.HostDenom, redelegationAmount),
})
rebalancings = append(rebalancings, &types.Rebalancing{
SrcValidator: srcValidator,
DstValidator: dstValidator,
Amt: redelegationAmount,
})
}
return msgs, rebalancings
}
// This function returns a list with the number of extra tokens that should be sent to each validator
// - Positive delta implies the validator has a surplus (and should lose stake)
// - Negative delta implies the validator has a deficit (and should gain stake)
func (k Keeper) GetValidatorDelegationDifferences(ctx sdk.Context, hostZone types.HostZone) ([]RebalanceValidatorDelegationChange, error) {
// The total rebalance amount consists of all delegations from validator's without a slash query in progress
// Validators with a slash query in progress will be excluded from rebalancing
targetRebalanceAmount := sdkmath.ZeroInt()
for _, validator := range hostZone.Validators {
if !validator.SlashQueryInProgress {
targetRebalanceAmount = targetRebalanceAmount.Add(validator.Delegation)
}
}
// Get the target delegation amount for each validator
targetDelegations, err := k.GetTargetValAmtsForHostZone(ctx, hostZone, targetRebalanceAmount)
if err != nil {
return nil, errorsmod.Wrapf(err, "unable to get target val amounts for host zone %s", hostZone.ChainId)
}
// For each validator, store the amount that their delegation should change
delegationDeltas := []RebalanceValidatorDelegationChange{}
totalDelegationChange := sdkmath.ZeroInt()
for _, validator := range hostZone.Validators {
// Compare the target with the current delegation
targetDelegation, ok := targetDelegations[validator.Address]
if !ok {
continue
}
delegationChange := validator.Delegation.Sub(targetDelegation)
// Only include validators who's delegation should change
if !delegationChange.IsZero() {
delegationDeltas = append(delegationDeltas, RebalanceValidatorDelegationChange{
ValidatorAddress: validator.Address,
Delta: delegationChange,
})
totalDelegationChange = totalDelegationChange.Add(delegationChange)
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId,
"Validator %s delegation surplus/deficit: %v", validator.Address, delegationChange))
}
}
// Sanity check that the sum of all the delegation change's is equal to 0
// (meaning the total delegation across ALL validators has not changed)
if !totalDelegationChange.IsZero() {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest,
"non-zero net delegation change (%v) across validators during rebalancing", totalDelegationChange)
}
return delegationDeltas, nil
}
// This will split a total delegation amount across validators, according to weights
// It returns a map of each portion, key'd on validator address
// Validator's with a slash query in progress are excluded
func (k Keeper) GetTargetValAmtsForHostZone(ctx sdk.Context, hostZone types.HostZone, totalDelegation sdkmath.Int) (map[string]sdkmath.Int, error) {
// Confirm the expected delegation amount is greater than 0
if !totalDelegation.IsPositive() {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest,
"Cannot calculate target delegation if final amount is less than or equal to zero (%v)", totalDelegation)
}
// Ignore any validators with a slash query in progress
validators := []types.Validator{}
for _, validator := range hostZone.Validators {
if !validator.SlashQueryInProgress {
validators = append(validators, *validator)
}
}
// Sum the total weight across all validators
totalWeight := k.GetTotalValidatorWeight(validators)
if totalWeight == 0 {
return nil, errorsmod.Wrapf(types.ErrNoValidatorWeights,
"No non-zero validators found for host zone %s", hostZone.ChainId)
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Total Validator Weight: %d", totalWeight))
// sort validators by weight ascending
sort.SliceStable(validators, func(i, j int) bool { // Do not use `Slice` here, it is stochastic
if validators[i].Weight != validators[j].Weight {
return validators[i].Weight < validators[j].Weight
}
// use name for tie breaker if weights are equal
return validators[i].Address < validators[j].Address
})
// Assign each validator their portion of the delegation (and give any overflow to the last validator)
targetUnbondingsByValidator := make(map[string]sdkmath.Int)
totalAllocated := sdkmath.ZeroInt()
for i, validator := range validators {
// For the last element, we need to make sure that the totalAllocated is equal to the finalDelegation
if i == len(validators)-1 {
targetUnbondingsByValidator[validator.Address] = totalDelegation.Sub(totalAllocated)
} else {
delegateAmt := sdkmath.NewIntFromUint64(validator.Weight).Mul(totalDelegation).Quo(sdkmath.NewIntFromUint64(totalWeight))
totalAllocated = totalAllocated.Add(delegateAmt)
targetUnbondingsByValidator[validator.Address] = delegateAmt
}
}
return targetUnbondingsByValidator, nil
}
// Sum the total weights across each validator for a host zone
func (k Keeper) GetTotalValidatorWeight(validators []types.Validator) uint64 {
totalWeight := uint64(0)
for _, validator := range validators {
totalWeight += validator.Weight
}
return totalWeight
}