Skip to content

Commit

Permalink
fix: dont attempt to delegate to invalid validators!
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowman committed Oct 18, 2023
1 parent 91b59b8 commit 2f21029
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions x/interchainstaking/types/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,8 @@ func (vi ValidatorIntents) Normalize() ValidatorIntents {

func DetermineAllocationsForDelegation(currentAllocations map[string]sdkmath.Int, currentSum sdkmath.Int, targetAllocations ValidatorIntents, amount sdk.Coins) map[string]sdkmath.Int {
input := amount[0].Amount
deltas, sources := CalculateAllocationDeltas(currentAllocations, map[string]bool{}, currentSum, targetAllocations)
// take targets and sources, and flip that shit.
// sources -> negate -> join -> sort.
largestSource := sources.MaxDelta()

// negate all values in sources.
sources.Negate()
deltas = append(deltas, sources...)

sum := sdk.ZeroInt()

// raise all deltas such that the minimum value is zero.
for idx := range deltas {
deltas[idx].Amount = deltas[idx].Amount.Add(largestSource)
// sum here instead of calling Sum() later to save looping over slice again.
sum = sum.Add(deltas[idx].Amount)
}
deltas, _ := CalculateAllocationDeltas(currentAllocations, map[string]bool{}, currentSum, targetAllocations)
sum := deltas.Sum()

// unequalSplit is the portion of input that should be distributed in attempt to make targets == 0
unequalSplit := sdk.MinInt(sum, input)
Expand Down Expand Up @@ -169,8 +154,10 @@ func DetermineAllocationsForDelegation(currentAllocations map[string]sdkmath.Int
outSum := sdk.ZeroInt()
outWeights := make(map[string]sdkmath.Int)
for _, delta := range deltas {
outWeights[delta.ValoperAddress] = delta.Amount
outSum = outSum.Add(delta.Amount)
if !delta.Amount.IsZero() {
outWeights[delta.ValoperAddress] = delta.Amount
outSum = outSum.Add(delta.Amount)
}
}
dust := input.Sub(outSum)
outWeights[deltas[0].ValoperAddress] = outWeights[deltas[0].ValoperAddress].Add(dust)
Expand Down

0 comments on commit 2f21029

Please sign in to comment.