From 4a9c2867335110991bb015ab5365ed87907d3c32 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 25 Aug 2023 09:47:50 -0500 Subject: [PATCH 1/3] State compatible epoch speedup from v18 (#6161) * The state compatible epoch speedup * update changelog --------- Co-authored-by: devbot-wizard <141283918+devbot-wizard@users.noreply.github.com> (cherry picked from commit 19c3c5081f2451691e276bd187a42c164edab937) # Conflicts: # CHANGELOG.md # x/incentives/keeper/distribute.go --- CHANGELOG.md | 26 +++++++++ x/incentives/keeper/distribute.go | 96 +++++++++++++++++++++++++++++++ x/superfluid/keeper/stake.go | 4 +- 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bbbcb92b3c..5211f9455cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +<<<<<<< HEAD +======= +### Misc Improvements + +* [#6161](https://github.com/osmosis-labs/osmosis/pull/6161) Reduce CPU time of epochs + +### API breaks + +* [#6071](https://github.com/osmosis-labs/osmosis/pull/6071) reduce number of returns for UpdatePosition and TicksToSqrtPrice functions +* [#5906](https://github.com/osmosis-labs/osmosis/pull/5906) Add `AccountLockedCoins` query in lockup module to stargate whitelist. + +## v17.0.0 + +### API breaks + +* [#6014](https://github.com/osmosis-labs/osmosis/pull/6014) refactor: reduce the number of returns in superfluid migration +* [#5983](https://github.com/osmosis-labs/osmosis/pull/5983) refactor(CL): 6 return values in CL CreatePosition with a struct +* [#6004](https://github.com/osmosis-labs/osmosis/pull/6004) reduce number of returns for creating full range position +* [#6018](https://github.com/osmosis-labs/osmosis/pull/6018) golangci: add unused parameters linter +* [#6033](https://github.com/osmosis-labs/osmosis/pull/6033) change tick API from sdk.Dec to osmomath.BigDec + +### Features + +* [#5072](https://github.com/osmosis-labs/osmosis/pull/5072) IBC-hooks: Add support for async acks when processing onRecvPacket + +>>>>>>> 19c3c508 (State compatible epoch speedup from v18 (#6161)) ### State Breaking * [#5532](https://github.com/osmosis-labs/osmosis/pull/5532) fix: Fix x/tokenfactory genesis import denoms reset x/bank existing denom metadata diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index 1ad76e5d5c1..dbd8047a5bd 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -301,7 +301,103 @@ func (k Keeper) distributeInternal( return nil, err } +<<<<<<< HEAD totalDistrCoins = totalDistrCoins.Add(distrCoins...) +======= + poolType := pool.GetType() + if poolType != poolmanagertypes.Concentrated { + return nil, fmt.Errorf("pool type %s is not supported for no lock distribution", poolType) + } + + // Get distribution epoch duration. This is used to calculate the emission rate. + currentEpoch := k.GetEpochInfo(ctx) + + // For every coin in the gauge, calculate the remaining reward per epoch + // and create a concentrated liquidity incentive record for it that + // is supposed to distribute over that epoch. + for _, remainCoin := range remainCoins { + // remaining coin amount per epoch. + remainAmountPerEpoch := remainCoin.Amount.Quo(sdk.NewIntFromUint64(remainEpochs)) + remainCoinPerEpoch := sdk.NewCoin(remainCoin.Denom, remainAmountPerEpoch) + + // emissionRate calculates amount of tokens to emit per second + // for ex: 10000uosmo to be distributed over 1day epoch will be 1000 tokens ÷ 86,400 seconds ≈ 0.01157 tokens per second (truncated) + // Note: reason why we do millisecond conversion is because floats are non-deterministic. + emissionRate := sdk.NewDecFromInt(remainAmountPerEpoch).QuoTruncate(sdk.NewDec(currentEpoch.Duration.Milliseconds()).QuoInt(sdk.NewInt(1000))) + ctx.Logger().Debug("distributeInternal, CreateIncentiveRecord NoLock gauge", "module", types.ModuleName, "gaugeId", gauge.Id, "poolId", pool.GetId(), "remainCoinPerEpoch", remainCoinPerEpoch, "height", ctx.BlockHeight()) + _, err := k.clk.CreateIncentive(ctx, + pool.GetId(), + k.ak.GetModuleAddress(types.ModuleName), + remainCoinPerEpoch, + emissionRate, + // Use current block time as start time, NOT the gauge start time. + // Gauge start time should be checked whenever moving between active + // and inactive gauges. By the time we get here, the gauge should be active. + ctx.BlockTime(), + // Only default uptime is supported at launch. + types.DefaultConcentratedUptime, + ) + if err != nil { + return nil, err + } + totalDistrCoins = totalDistrCoins.Add(remainCoinPerEpoch) + } + } else { + // This is a standard lock distribution flow that assumes that we have locks associated with the gauge. + denom := lockuptypes.NativeDenom(gauge.DistributeTo.Denom) + lockSum := lockuptypes.SumLocksByDenom(locks, denom) + + if lockSum.IsZero() { + return nil, nil + } + + // In this case, remove redundant cases. + // Namely: gauge empty OR gauge coins undistributable. + if remainCoins.Empty() { + ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is empty, why is it being ran %d. Balancer code", gauge.Id)) + err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) + return totalDistrCoins, err + } + + // Remove some spam gauges, is state compatible. + // If they're to pool 1 they can't distr at this small of a quantity. + if remainCoins.Len() == 1 && remainCoins[0].Amount.LTE(sdk.NewInt(10)) && gauge.DistributeTo.Denom == "gamm/pool/1" && remainCoins[0].Denom != "uosmo" { + ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is perceived spam, skipping %d", gauge.Id)) + err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) + return totalDistrCoins, err + } + + 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()) + 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(sdk.NewInt(int64(remainEpochs)))) + if amt.IsPositive() { + newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt} + distrCoins = distrCoins.Add(newlyDistributedCoin) + } + } + distrCoins = distrCoins.Sort() + if distrCoins.Empty() { + continue + } + // update the amount for that address + rewardReceiver := lock.RewardReceiverAddress + + // if the reward receiver stored in state is an empty string, it indicates that the owner is the reward receiver. + if rewardReceiver == "" { + rewardReceiver = lock.Owner + } + err := distrInfo.addLockRewards(lock.Owner, rewardReceiver, distrCoins) + if err != nil { + return nil, err + } + + totalDistrCoins = totalDistrCoins.Add(distrCoins...) + } +>>>>>>> 19c3c508 (State compatible epoch speedup from v18 (#6161)) } err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) diff --git a/x/superfluid/keeper/stake.go b/x/superfluid/keeper/stake.go index 077b8440371..b7f8c96fc34 100644 --- a/x/superfluid/keeper/stake.go +++ b/x/superfluid/keeper/stake.go @@ -60,7 +60,7 @@ func (k Keeper) RefreshIntermediaryDelegationAmounts(ctx sdk.Context) { if !found { // continue if current delegation is 0, in case its really a dust delegation // that becomes worth something after refresh. - k.Logger(ctx).Info(fmt.Sprintf("Existing delegation not found for %s with %s during superfluid refresh."+ + k.Logger(ctx).Debug(fmt.Sprintf("Existing delegation not found for %s with %s during superfluid refresh."+ " It may have been previously bonded, but now unbonded.", mAddr.String(), acc.ValAddr)) } else { currentAmount = validator.TokensFromShares(delegation.Shares).RoundInt() @@ -86,7 +86,7 @@ func (k Keeper) RefreshIntermediaryDelegationAmounts(ctx sdk.Context) { ctx.Logger().Error("Error in forceUndelegateAndBurnOsmoTokens, state update reverted", err) } } else { - ctx.Logger().Info("Intermediary account already has correct delegation amount?" + + ctx.Logger().Debug("Intermediary account already has correct delegation amount?" + " This with high probability implies the exact same spot price as the last epoch," + "and no delegation changes.") } From 7b4f66193042f3e331612baf3ab57649083d1480 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 25 Aug 2023 17:29:02 +0200 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 63 ---------------------------------------------------- 1 file changed, 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5211f9455cd..411fac0eac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,73 +42,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased -<<<<<<< HEAD -======= ### Misc Improvements * [#6161](https://github.com/osmosis-labs/osmosis/pull/6161) Reduce CPU time of epochs -### API breaks - -* [#6071](https://github.com/osmosis-labs/osmosis/pull/6071) reduce number of returns for UpdatePosition and TicksToSqrtPrice functions -* [#5906](https://github.com/osmosis-labs/osmosis/pull/5906) Add `AccountLockedCoins` query in lockup module to stargate whitelist. - -## v17.0.0 - -### API breaks - -* [#6014](https://github.com/osmosis-labs/osmosis/pull/6014) refactor: reduce the number of returns in superfluid migration -* [#5983](https://github.com/osmosis-labs/osmosis/pull/5983) refactor(CL): 6 return values in CL CreatePosition with a struct -* [#6004](https://github.com/osmosis-labs/osmosis/pull/6004) reduce number of returns for creating full range position -* [#6018](https://github.com/osmosis-labs/osmosis/pull/6018) golangci: add unused parameters linter -* [#6033](https://github.com/osmosis-labs/osmosis/pull/6033) change tick API from sdk.Dec to osmomath.BigDec - -### Features - -* [#5072](https://github.com/osmosis-labs/osmosis/pull/5072) IBC-hooks: Add support for async acks when processing onRecvPacket - ->>>>>>> 19c3c508 (State compatible epoch speedup from v18 (#6161)) -### State Breaking - -* [#5532](https://github.com/osmosis-labs/osmosis/pull/5532) fix: Fix x/tokenfactory genesis import denoms reset x/bank existing denom metadata - -### Misc Improvements - -* [#5534](https://github.com/osmosis-labs/osmosis/pull/5534) fix: fix the account number of x/tokenfactory module account -* [#5750](https://github.com/osmosis-labs/osmosis/pull/5750) feat: add cli commmand for converting proto structs to proto marshalled bytes - -## v16.0.0 -Osmosis Labs is excited to announce the release of v16.0.0, a major upgrade that includes a number of new features and improvements like introduction of new modules, updates existing APIs, and dependency updates. This upgrade aims to enhance capital efficiency by introducing SuperCharged Liquidity, introduce custom liquidity pools backed by CosmWasm smart contracts, and improve overall functionality. - -New Modules and Features: - -SuperCharged Liquidity Module (x/concentrated-liquidity): -- Introduces a game-changing pool model that enhances captical efficiency in Osmosis. - -CosmWasm Pool Module (x/cosmwasmpool): -- Enables the creation and management of liquidity pools backed by CosmWasm smart contracts. - -ProtoRev Changes (x/protorev): -- Modifies the payment schedule for the dev account from weekly to after every trade. -- Triggers backruns, joinPool, and exitPool using hooks. - -TokenFactory before send hooks (x/tokenfactory): -- This enhancement allows for executing custom logic before sending tokens, providing more flexibility -and control over token transfers. - - -### Security - -* Prevents a deadlock that new smart contracts could cause due to bugs in the SDK -* Adds a new app.toml config called `query-gas-limit` that limits how much computational resources a query can require. Mitigates bugs where nodes were getting knocked off the network due to responding to long queries. - -### Bug fixes - -* Makes historic queries use the correct block timestamp -* Fixes a bug where certain governance events weren't emitted -* Adds mempool protection for Barberry bug -* Various export genesis bug fixes - ## v15.1.2 ### Security From 78850df2c112d45b92dd564859ac1035c9cb52a0 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 25 Aug 2023 17:29:54 +0200 Subject: [PATCH 3/3] Update distribute.go --- x/incentives/keeper/distribute.go | 113 +++++------------------------- 1 file changed, 17 insertions(+), 96 deletions(-) diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index dbd8047a5bd..c9e1c912e6f 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -273,6 +273,23 @@ func (k Keeper) distributeInternal( } remainCoins := gauge.Coins.Sub(gauge.DistributedCoins) + + // In this case, remove redundant cases. + // Namely: gauge empty OR gauge coins undistributable. + if remainCoins.Empty() { + ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is empty, why is it being ran %d. Balancer code", gauge.Id)) + err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) + return totalDistrCoins, err + } + + // Remove some spam gauges, is state compatible. + // If they're to pool 1 they can't distr at this small of a quantity. + if remainCoins.Len() == 1 && remainCoins[0].Amount.LTE(sdk.NewInt(10)) && gauge.DistributeTo.Denom == "gamm/pool/1" && remainCoins[0].Denom != "uosmo" { + ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is perceived spam, skipping %d", gauge.Id)) + err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) + return totalDistrCoins, err + } + // if its a perpetual gauge, we set remaining epochs to 1. // otherwise is is a non perpetual gauge and we determine how many epoch payouts are left remainEpochs := uint64(1) @@ -301,103 +318,7 @@ func (k Keeper) distributeInternal( return nil, err } -<<<<<<< HEAD totalDistrCoins = totalDistrCoins.Add(distrCoins...) -======= - poolType := pool.GetType() - if poolType != poolmanagertypes.Concentrated { - return nil, fmt.Errorf("pool type %s is not supported for no lock distribution", poolType) - } - - // Get distribution epoch duration. This is used to calculate the emission rate. - currentEpoch := k.GetEpochInfo(ctx) - - // For every coin in the gauge, calculate the remaining reward per epoch - // and create a concentrated liquidity incentive record for it that - // is supposed to distribute over that epoch. - for _, remainCoin := range remainCoins { - // remaining coin amount per epoch. - remainAmountPerEpoch := remainCoin.Amount.Quo(sdk.NewIntFromUint64(remainEpochs)) - remainCoinPerEpoch := sdk.NewCoin(remainCoin.Denom, remainAmountPerEpoch) - - // emissionRate calculates amount of tokens to emit per second - // for ex: 10000uosmo to be distributed over 1day epoch will be 1000 tokens ÷ 86,400 seconds ≈ 0.01157 tokens per second (truncated) - // Note: reason why we do millisecond conversion is because floats are non-deterministic. - emissionRate := sdk.NewDecFromInt(remainAmountPerEpoch).QuoTruncate(sdk.NewDec(currentEpoch.Duration.Milliseconds()).QuoInt(sdk.NewInt(1000))) - ctx.Logger().Debug("distributeInternal, CreateIncentiveRecord NoLock gauge", "module", types.ModuleName, "gaugeId", gauge.Id, "poolId", pool.GetId(), "remainCoinPerEpoch", remainCoinPerEpoch, "height", ctx.BlockHeight()) - _, err := k.clk.CreateIncentive(ctx, - pool.GetId(), - k.ak.GetModuleAddress(types.ModuleName), - remainCoinPerEpoch, - emissionRate, - // Use current block time as start time, NOT the gauge start time. - // Gauge start time should be checked whenever moving between active - // and inactive gauges. By the time we get here, the gauge should be active. - ctx.BlockTime(), - // Only default uptime is supported at launch. - types.DefaultConcentratedUptime, - ) - if err != nil { - return nil, err - } - totalDistrCoins = totalDistrCoins.Add(remainCoinPerEpoch) - } - } else { - // This is a standard lock distribution flow that assumes that we have locks associated with the gauge. - denom := lockuptypes.NativeDenom(gauge.DistributeTo.Denom) - lockSum := lockuptypes.SumLocksByDenom(locks, denom) - - if lockSum.IsZero() { - return nil, nil - } - - // In this case, remove redundant cases. - // Namely: gauge empty OR gauge coins undistributable. - if remainCoins.Empty() { - ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is empty, why is it being ran %d. Balancer code", gauge.Id)) - err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) - return totalDistrCoins, err - } - - // Remove some spam gauges, is state compatible. - // If they're to pool 1 they can't distr at this small of a quantity. - if remainCoins.Len() == 1 && remainCoins[0].Amount.LTE(sdk.NewInt(10)) && gauge.DistributeTo.Denom == "gamm/pool/1" && remainCoins[0].Denom != "uosmo" { - ctx.Logger().Debug(fmt.Sprintf("gauge debug, this gauge is perceived spam, skipping %d", gauge.Id)) - err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins) - return totalDistrCoins, err - } - - 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()) - 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(sdk.NewInt(int64(remainEpochs)))) - if amt.IsPositive() { - newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt} - distrCoins = distrCoins.Add(newlyDistributedCoin) - } - } - distrCoins = distrCoins.Sort() - if distrCoins.Empty() { - continue - } - // update the amount for that address - rewardReceiver := lock.RewardReceiverAddress - - // if the reward receiver stored in state is an empty string, it indicates that the owner is the reward receiver. - if rewardReceiver == "" { - rewardReceiver = lock.Owner - } - err := distrInfo.addLockRewards(lock.Owner, rewardReceiver, distrCoins) - if err != nil { - return nil, err - } - - totalDistrCoins = totalDistrCoins.Add(distrCoins...) - } ->>>>>>> 19c3c508 (State compatible epoch speedup from v18 (#6161)) } err := k.updateGaugePostDistribute(ctx, gauge, totalDistrCoins)