From 2f210295ff3601b572483e04fe9bda2cb17ef028 Mon Sep 17 00:00:00 2001 From: Joe Bowman Date: Wed, 18 Oct 2023 10:23:04 +0100 Subject: [PATCH] fix: dont attempt to delegate to invalid validators! --- x/interchainstaking/types/delegation.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/x/interchainstaking/types/delegation.go b/x/interchainstaking/types/delegation.go index 7eb2e50f7..3a0e5ac3f 100644 --- a/x/interchainstaking/types/delegation.go +++ b/x/interchainstaking/types/delegation.go @@ -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) @@ -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)