From 2499d66414c79487f03f1640cfcbb0d2b1f7d632 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 12 Dec 2023 19:41:09 +0100 Subject: [PATCH] Lower Epoch CPU usage (#7093) (#7097) (cherry picked from commit 54883e084cd1084d284b0c3c3f6b2624cd3b5b06) Co-authored-by: Dev Ojha --- x/incentives/keeper/distribute.go | 7 +++++-- x/lockup/types/lock.go | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index 496eef9e653..40e3581df86 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -652,14 +652,17 @@ func (k Keeper) distributeInternal( if lockSum.IsZero() { return nil, nil } + remainingEpochsAsInt := osmomath.NewInt(int64(remainEpochs)) + // total_denom_lock_amount * remain_epochs + lockSumTimesRemainingEpochs := lockSum.Mul(remainingEpochsAsInt) for _, lock := range locks { distrCoins := sdk.Coins{} - ctx.Logger().Debug("distributeInternal, distribute to lock", "module", types.ModuleName, "gaugeId", gauge.Id, "lockId", lock.ID, "remainCons", remainCoins, "height", ctx.BlockHeight()) + // ctx.Logger().Debug("distributeInternal, distribute to lock", "module", types.ModuleName, "gaugeId", gauge.Id, "lockId", lock.ID, "remainCons", remainCoins, "height", ctx.BlockHeight()) for _, coin := range remainCoins { // distribution amount = gauge_size * denom_lock_amount / (total_denom_lock_amount * remain_epochs) denomLockAmt := lock.Coins.AmountOfNoDenomValidation(denom) - amt := coin.Amount.Mul(denomLockAmt).Quo(lockSum.Mul(osmomath.NewInt(int64(remainEpochs)))) + amt := coin.Amount.Mul(denomLockAmt).Quo(lockSumTimesRemainingEpochs) if amt.IsPositive() { newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt} distrCoins = distrCoins.Add(newlyDistributedCoin) diff --git a/x/lockup/types/lock.go b/x/lockup/types/lock.go index b876d92ca2c..fc3edce6691 100644 --- a/x/lockup/types/lock.go +++ b/x/lockup/types/lock.go @@ -62,6 +62,7 @@ func (p PeriodLock) SingleCoin() (sdk.Coin, error) { return p.Coins[0], nil } +// TODO: Can we use sumtree instead here? func SumLocksByDenom(locks []PeriodLock, denom string) osmomath.Int { sum := osmomath.NewInt(0) // validate the denom once, so we can avoid the expensive validate check in the hot loop. @@ -70,6 +71,7 @@ func SumLocksByDenom(locks []PeriodLock, denom string) osmomath.Int { panic(fmt.Errorf("invalid denom used internally: %s, %v", denom, err)) } for _, lock := range locks { + // TODO: Replace with Mutative Int Operations sum = sum.Add(lock.Coins.AmountOfNoDenomValidation(denom)) } return sum