From 0c67bc4ed952ee08689ebae88c7c4d2d2fc3a490 Mon Sep 17 00:00:00 2001 From: Brendan Chou <3680392+BrendanChou@users.noreply.github.com> Date: Wed, 29 May 2024 13:07:07 -0400 Subject: [PATCH] Remove `big.Rat` usage from `x/ratelimit` module (#1596) --- protocol/x/ratelimit/util/capacity.go | 55 +++++++-------------------- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/protocol/x/ratelimit/util/capacity.go b/protocol/x/ratelimit/util/capacity.go index 8ca32a677e..59bdb14a74 100644 --- a/protocol/x/ratelimit/util/capacity.go +++ b/protocol/x/ratelimit/util/capacity.go @@ -36,55 +36,26 @@ func CalculateNewCapacityList( newCapacityList = make([]dtypes.SerializableInt, len(limiterCapacityList)) for i, limiterCapacity := range limiterCapacityList { - limiter, bigPrevCapacity := limiterCapacity.Limiter, limiterCapacity.Capacity.BigInt() - - // For each limiter, calculate the current baseline. + limiter := limiterCapacity.Limiter + capacity := limiterCapacity.Capacity.BigInt() baseline := GetBaseline(bigTvl, limiter) + capacityMinusBaseline := new(big.Int).Sub(capacity, baseline) - capacityMinusBaseline := new(big.Int).Sub( - bigPrevCapacity, - baseline, - ) - - // Calculate left operand: `max(baseline, capacity-baseline)`. This equals `baseline` when `capacity <= 2 * baseline` - operandL := new(big.Rat).SetInt( - lib.BigMax( - baseline, - capacityMinusBaseline, - ), - ) - - // Calculate right operand: `time_since_last_block / period` - operandR := new(big.Rat).SetFrac64( - timeSinceLastBlock.Milliseconds(), - limiter.Period.Milliseconds(), - ) - - // Calculate: `capacity_diff = max(baseline, capacity-baseline) * (time_since_last_block / period)` - // Since both operands > 0, `capacity_diff` is positive or zero (due to rounding). - capacityDiffRat := new(big.Rat).Mul(operandL, operandR) - capacityDiff := lib.BigRatRound(capacityDiffRat, false) // rounds down `capacity_diff` + // Calculate the absolute value of the capacity delta. + capacityDiff := lib.BigMax(baseline, capacityMinusBaseline) + capacityDiff.Mul(capacityDiff, lib.BigI(timeSinceLastBlock.Milliseconds())) + capacityDiff.Div(capacityDiff, lib.BigI(limiter.Period.Milliseconds())) + // Move the capacity towards the baseline by capacityDiff. Do not cross the baseline. + // Capacity is modified in-place if necessary. if new(big.Int).Abs(capacityMinusBaseline).Cmp(capacityDiff) <= 0 { - // if `abs(capacity - baseline) < capacity_diff` then `capacity = baseline`` - newCapacityList[i] = dtypes.NewIntFromBigInt(baseline) + capacity = baseline } else if capacityMinusBaseline.Sign() < 0 { - // else if `capacity < baseline` then `capacity += capacity_diff` - newCapacityList[i] = dtypes.NewIntFromBigInt( - new(big.Int).Add( - bigPrevCapacity, - capacityDiff, - ), - ) + capacity.Add(capacity, capacityDiff) } else { - // else `capacity -= capacity_diff` - newCapacityList[i] = dtypes.NewIntFromBigInt( - new(big.Int).Sub( - bigPrevCapacity, - capacityDiff, - ), - ) + capacity.Sub(capacity, capacityDiff) } + newCapacityList[i] = dtypes.NewIntFromBigInt(capacity) } return newCapacityList