Skip to content

Commit

Permalink
Speedup core loop of epoch (#7174)
Browse files Browse the repository at this point in the history
* Speedup core loop of epoch

* Update x/incentives/keeper/distribute.go

Co-authored-by: Roman <[email protected]>

---------

Co-authored-by: Roman <[email protected]>
(cherry picked from commit ec36033)
  • Loading branch information
ValarDragon authored and mergify[bot] committed Dec 21, 2023
1 parent bc6dc84 commit 3ac10a3
Showing 1 changed file with 20 additions and 5 deletions.
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

0 comments on commit 3ac10a3

Please sign in to comment.