Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower Epoch CPU usage #7093

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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