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

Speedup core loop of epoch (backport #7174) #7175

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
25 changes: 20 additions & 5 deletions x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,22 +654,28 @@ func (k Keeper) distributeInternal(
}
// total_denom_lock_amount * remain_epochs
lockSumTimesRemainingEpochs := lockSum.MulRaw(int64(remainEpochs))
lockSumTimesRemainingEpochsBi := lockSumTimesRemainingEpochs.BigIntMut()

for _, lock := range locks {
distrCoins := sdk.Coins{}
// too expensive + verbose even in debug mode.
// ctx.Logger().Debug("distributeInternal, distribute to lock", "module", types.ModuleName, "gaugeId", gauge.Id, "lockId", lock.ID, "remainCons", remainCoins, "height", ctx.BlockHeight())

denomLockAmt := guaranteedNonzeroCoinAmountOf(lock.Coins, denom)
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(lockSumTimesRemainingEpochs)
if amt.IsPositive() {
newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt}
amt := coin.Amount.Mul(denomLockAmt).BigIntMut()
amt = amt.Quo(amt, lockSumTimesRemainingEpochsBi)
coinAmt := osmomath.NewIntFromBigInt(amt)
if coinAmt.IsPositive() {
newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: coinAmt}
distrCoins = distrCoins.Add(newlyDistributedCoin)
}
}
distrCoins = distrCoins.Sort()
if distrCoins.Len() > 1 {
// Sort makes a runtime copy, due to some interesting golang details.
distrCoins = distrCoins.Sort()
}
if distrCoins.Empty() {
continue
}
Expand All @@ -693,6 +699,15 @@ func (k Keeper) distributeInternal(
return totalDistrCoins, err
}

// faster coins.AmountOf if we know that coins must contain the denom.
// returns a new sdk int that can be mutated.
func guaranteedNonzeroCoinAmountOf(coins sdk.Coins, denom string) osmomath.Int {
if coins.Len() == 1 {
return coins[0].Amount
}
return coins.AmountOfNoDenomValidation(denom)
}

// updateGaugePostDistribute increments the gauge's filled epochs field.
// Also adds the coins that were just distributed to the gauge's distributed coins field.
func (k Keeper) updateGaugePostDistribute(ctx sdk.Context, gauge types.Gauge, newlyDistributedCoins sdk.Coins) error {
Expand Down