-
Notifications
You must be signed in to change notification settings - Fork 194
/
validator_selection.go
96 lines (83 loc) · 3.75 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
package keeper
import (
"fmt"
"sort"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Stride-Labs/stride/v11/utils"
"github.com/Stride-Labs/stride/v11/x/stakeibc/types"
)
// This function returns a map from Validator Address to how many extra tokens need to be given to that validator
//
// positive implies extra tokens need to be given,
// negative impleis tokens need to be taken away
func (k Keeper) GetValidatorDelegationAmtDifferences(ctx sdk.Context, hostZone types.HostZone) (map[string]sdkmath.Int, error) {
validators := hostZone.GetValidators()
delegationDelta := make(map[string]sdkmath.Int)
totalDelegatedAmt := k.GetTotalValidatorDelegations(hostZone)
targetDelegation, err := k.GetTargetValAmtsForHostZone(ctx, hostZone, totalDelegatedAmt)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error getting target val amts for host zone %s", hostZone.ChainId))
return nil, err
}
for _, validator := range validators {
targetDelForVal := targetDelegation[validator.GetAddress()]
delegationDelta[validator.GetAddress()] = targetDelForVal.Sub(validator.DelegationAmt)
}
return delegationDelta, nil
}
// This will get the target validator delegation for the given hostZone
// such that the total validator delegation is equal to the finalDelegation
// output key is ADDRESS not NAME
func (k Keeper) GetTargetValAmtsForHostZone(ctx sdk.Context, hostZone types.HostZone, finalDelegation sdkmath.Int) (map[string]sdkmath.Int, error) {
// Confirm the expected delegation amount is greater than 0
if finalDelegation.Equal(sdkmath.ZeroInt()) {
k.Logger(ctx).Error(fmt.Sprintf("Cannot calculate target delegation if final amount is 0 %s", hostZone.ChainId))
return nil, types.ErrNoValidatorWeights
}
// Sum the total weight across all validators
totalWeight := k.GetTotalValidatorWeight(hostZone)
if totalWeight == 0 {
k.Logger(ctx).Error(fmt.Sprintf("No non-zero validators found for host zone %s", hostZone.ChainId))
return nil, types.ErrNoValidatorWeights
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Total Validator Weight: %d", totalWeight))
// sort validators by weight ascending, this is inplace sorting!
validators := hostZone.Validators
for i, j := 0, len(validators)-1; i < j; i, j = i+1, j-1 {
validators[i], validators[j] = validators[j], validators[i]
}
sort.SliceStable(validators, func(i, j int) bool { // Do not use `Slice` here, it is stochastic
return validators[i].Weight < validators[j].Weight
})
// 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] = finalDelegation.Sub(totalAllocated)
} else {
delegateAmt := sdkmath.NewIntFromUint64(validator.Weight).Mul(finalDelegation).Quo(sdkmath.NewIntFromUint64(totalWeight))
totalAllocated = totalAllocated.Add(delegateAmt)
targetUnbondingsByValidator[validator.Address] = delegateAmt
}
}
return targetUnbondingsByValidator, nil
}
func (k Keeper) GetTotalValidatorDelegations(hostZone types.HostZone) sdkmath.Int {
validators := hostZone.GetValidators()
total_delegation := sdkmath.ZeroInt()
for _, validator := range validators {
total_delegation = total_delegation.Add(validator.DelegationAmt)
}
return total_delegation
}
func (k Keeper) GetTotalValidatorWeight(hostZone types.HostZone) uint64 {
validators := hostZone.GetValidators()
total_weight := uint64(0)
for _, validator := range validators {
total_weight += validator.Weight
}
return total_weight
}