Skip to content

Commit

Permalink
Lower Epoch CPU usage (#7093) (#7097)
Browse files Browse the repository at this point in the history
(cherry picked from commit 54883e0)

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
  • Loading branch information
mergify[bot] and ValarDragon authored Dec 12, 2023
1 parent 44a4ca4 commit 2499d66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions x/lockup/types/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 2499d66

Please sign in to comment.