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

refactor(incentives): skip epoch distribution to perpetual gauges #1655

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ func (a *AppKeepers) InitKeepers(
a.AccountKeeper,
a.StakingKeeper,
a.IncentivesKeeper,
a.SequencerKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down
14 changes: 8 additions & 6 deletions x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge, cache types.De
lockHolders := NewRewardDistributionTracker()
totalDistributedCoins := sdk.Coins{}

// for each gauge,
// - update the lockHolders map with account -> coins (according to the distribution type)
// - update the gauge's filled epochs
for _, gauge := range gauges {
var (
gaugeDistributedCoins sdk.Coins
Expand All @@ -56,13 +59,11 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge, cache types.De
return nil, err
}

if !gaugeDistributedCoins.Empty() {
err = k.updateGaugePostDistribute(ctx, gauge, gaugeDistributedCoins, epochEnd)
if err != nil {
return nil, err
}
totalDistributedCoins = totalDistributedCoins.Add(gaugeDistributedCoins...)
err = k.updateGaugePostDistribute(ctx, gauge, gaugeDistributedCoins, epochEnd)
if err != nil {
return nil, err
}
totalDistributedCoins = totalDistributedCoins.Add(gaugeDistributedCoins...)
}

// apply the distribution to asset gauges
Expand Down Expand Up @@ -133,6 +134,7 @@ func (k Keeper) checkFinishedGauges(ctx sdk.Context, gauges []types.Gauge) {

// filled epoch is increased in this step and we compare with +1
if gauge.NumEpochsPaidOver <= gauge.FilledEpochs+1 {
// TODO: burn/community pool all remaining coins
if err := k.moveActiveGaugeToFinishedGauge(ctx, gauge); err != nil {
panic(err)
}
Expand Down
52 changes: 49 additions & 3 deletions x/incentives/keeper/distribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension/v3/x/incentives/types"
lockuptypes "github.com/dymensionxyz/dymension/v3/x/lockup/types"
Expand Down Expand Up @@ -188,7 +189,6 @@ func (suite *KeeperTestSuite) TestNoLockPerpetualGaugeDistribution() {
}},
Coins: coins,
NumEpochsPaidOver: 1,
FilledEpochs: 0,
DistributedCoins: sdk.Coins{},
StartTime: startTime,
}
Expand All @@ -209,7 +209,8 @@ func (suite *KeeperTestSuite) TestNoLockPerpetualGaugeDistribution() {
// check state is same after distribution
gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)
suite.Require().Len(gauges, 1)
suite.Require().Equal(gauges[0].String(), expectedGauge.String())
expectedGauge.FilledEpochs = 1
suite.Require().Equal(expectedGauge.String(), gauges[0].String())
}

// TestNoLockNonPerpetualGaugeDistribution tests that the creation of a non perp gauge that has no locks associated does not distribute any tokens.
Expand All @@ -235,7 +236,6 @@ func (suite *KeeperTestSuite) TestNoLockNonPerpetualGaugeDistribution() {
}},
Coins: coins,
NumEpochsPaidOver: 2,
FilledEpochs: 0,
DistributedCoins: sdk.Coins{},
StartTime: startTime,
}
Expand All @@ -256,5 +256,51 @@ func (suite *KeeperTestSuite) TestNoLockNonPerpetualGaugeDistribution() {
// check state is same after distribution
gauges = suite.App.IncentivesKeeper.GetNotFinishedGauges(suite.Ctx)
suite.Require().Len(gauges, 1)

expectedGauge.FilledEpochs = 1
suite.Require().Equal(gauges[0].String(), expectedGauge.String())
}

// TestTooSmallDistribution tests the distribution logic where the sum of coins to be distributed is too small
func (suite *KeeperTestSuite) TestTooSmallDistribution() {
coins := sdk.Coins{sdk.NewCoin("stake", math.NewInt(1).MulRaw(1e18))}
totalLockedAmt := math.NewInt(1000).MulRaw(1e18)
numEpochsPaidOver := uint64(18446744073709522816)

// Set up the state for the test
suite.SetupTest()

addr := sdk.AccAddress([]byte("Gauge_Creation_Addr_"))
startTime2 := time.Now()
distrTo := lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: "stake",
Duration: time.Hour,
}

// mints coins so supply exists on chain
mintCoins := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)}
suite.FundAcc(addr, mintCoins)

_, gauge := suite.CreateGauge(false, addr, coins, distrTo, startTime2, numEpochsPaidOver)

numLocks := int64(1000)
users := make([]userLocks, numLocks)
perLockAmt := totalLockedAmt.QuoRaw(numLocks)
for i := 0; i < 1000; i++ {
users[i].lockDurations = []time.Duration{time.Hour}
users[i].lockAmounts = []sdk.Coins{sdk.NewCoins(sdk.NewCoin("stake", perLockAmt))}
}
addrs := suite.SetupUserLocks(users)
_ = addrs

// Call the distribution function
distrCoins, err := suite.App.IncentivesKeeper.DistributeOnEpochEnd(suite.Ctx, []types.Gauge{*gauge})
suite.Require().NoError(err)

// Check the result
gauge, err = suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gauge.Id)
suite.Require().NoError(err)
suite.Assert().Equal(uint64(1), gauge.FilledEpochs)
suite.Require().Equal(distrCoins, sdk.Coins{})
}
8 changes: 5 additions & 3 deletions x/incentives/keeper/gauge_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ func (k Keeper) calculateAssetGaugeRewards(ctx sdk.Context, gauge types.Gauge, l
for _, lock := range locks {
distrCoins := sdk.Coins{}
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))))
// coinForEpoch = coin.Amount / remainEpochs
// lockShare = lockAmt / lockSum
// amt = coinForEpoch * lockShare = coin.Amount * lockAmt / (lockSum * remainEpochs)
lockAmt := lock.Coins.AmountOfNoDenomValidation(denom)
amt := coin.Amount.Mul(lockAmt).Quo(lockSum.MulRaw(int64(remainEpochs))) //nolint:gosec
if amt.IsPositive() {
newlyDistributedCoin := sdk.Coin{Denom: coin.Denom, Amount: amt}
distrCoins = distrCoins.Add(newlyDistributedCoin)
Expand Down
16 changes: 11 additions & 5 deletions x/incentives/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"github.com/dymensionxyz/dymension/v3/x/incentives/types"
epochstypes "github.com/osmosis-labs/osmosis/v15/x/epochs/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -15,7 +16,7 @@ func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochN
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
params := k.GetParams(ctx)
if epochIdentifier == params.DistrEpochIdentifier {
// begin distribution if it's start time
// activate gauges that have started
gauges := k.GetUpcomingGauges(ctx)
for _, gauge := range gauges {
if !ctx.BlockTime().Before(gauge.StartTime) {
Expand All @@ -25,12 +26,17 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
}
}

// if len(gauges) > 10 {
// ctx.EventManager().IncreaseCapacity(2e6)
// }
// get active, non-perpetual gauges
// perpetual gauges are paid directly by the protocol (i.e streamer)
newGauges := make([]types.Gauge, 0, len(gauges))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: i guess most of the gauges will be perpetual, so maybe it's worth doing make([]types.Gauge, 0) to avoid runtime overhead. otherwise runtime will try to find a memory slice for len(gauges) though in fact we need much less.

for _, gauge := range k.GetActiveGauges(ctx) {
if !gauge.IsPerpetual {
newGauges = append(newGauges, gauge)
}
}
gauges = newGauges

// distribute due to epoch event
gauges = k.GetActiveGauges(ctx)
_, err := k.DistributeOnEpochEnd(ctx, gauges)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion x/incentives/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateG
return nil, fmt.Errorf("charge gauge fee: %w", err)
}

gaugeID, err := server.keeper.CreateGauge(ctx, msg.IsPerpetual, owner, msg.Coins, msg.DistributeTo, msg.StartTime, msg.NumEpochsPaidOver)
gaugeID, err := server.keeper.CreateGauge(ctx, false, owner, msg.Coins, msg.DistributeTo, msg.StartTime, msg.NumEpochsPaidOver)
if err != nil {
return nil, fmt.Errorf("create gauge: %w", err)
}
Expand Down
5 changes: 4 additions & 1 deletion x/incentives/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ var (
DefaultAddDenomFee = DYM // 1 DYM
)

const DefaultDistrEpochIdentifier = "week"
const (
DefaultDistrEpochIdentifier = "week"
MaxEpochsPerDistributionPeriod = 1000
)
9 changes: 5 additions & 4 deletions x/incentives/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func (m MsgCreateGauge) ValidateBasic() error {
if m.StartTime.Equal(time.Time{}) {
return errors.New("distribution start time should be set")
}
if m.NumEpochsPaidOver == 0 {
return errors.New("distribution period should be at least 1 epoch")
if m.NumEpochsPaidOver == 0 || m.NumEpochsPaidOver > MaxEpochsPerDistributionPeriod {
return errors.New("invalid number of epochs to distribute over")
}
if m.IsPerpetual && m.NumEpochsPaidOver != 1 {
return errors.New("distribution period should be 1 epoch for perpetual gauge")

if m.IsPerpetual {
return errors.New("custom perpetual gauges are not supported")
}

if lockuptypes.LockQueryType_name[int32(m.DistributeTo.LockQueryType)] != "ByDuration" {
Expand Down
18 changes: 0 additions & 18 deletions x/incentives/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,6 @@ func TestMsgCreateGauge(t *testing.T) {
}),
expectPass: false,
},
{
name: "invalid num epochs paid over for perpetual gauge",
msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge {
msg.NumEpochsPaidOver = 2
msg.IsPerpetual = true
return msg
}),
expectPass: false,
},
{
name: "valid num epochs paid over for perpetual gauge",
msg: createMsg(func(msg incentivestypes.MsgCreateGauge) incentivestypes.MsgCreateGauge {
msg.NumEpochsPaidOver = 1
msg.IsPerpetual = true
return msg
}),
expectPass: true,
},
}

for _, test := range tests {
Expand Down
3 changes: 0 additions & 3 deletions x/sponsorship/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Keeper struct {

stakingKeeper types.StakingKeeper
incentivesKeeper types.IncentivesKeeper
sequencerKeeper types.SequencerKeeper
}

// NewKeeper returns a new instance of the x/sponsorship keeper.
Expand All @@ -34,7 +33,6 @@ func NewKeeper(
ak types.AccountKeeper,
sk types.StakingKeeper,
ik types.IncentivesKeeper,
sqk types.SequencerKeeper,
authority string,
) Keeper {
// ensure the module account is set
Expand Down Expand Up @@ -82,7 +80,6 @@ func NewKeeper(
),
stakingKeeper: sk,
incentivesKeeper: ik,
sequencerKeeper: sqk,
}

// SchemaBuilder CANNOT be used after Build is called,
Expand Down
5 changes: 0 additions & 5 deletions x/sponsorship/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

incentivestypes "github.com/dymensionxyz/dymension/v3/x/incentives/types"
sequencertypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
)

// AccountKeeper defines the contract required for account APIs.
Expand All @@ -22,7 +21,3 @@ type StakingKeeper interface {
type IncentivesKeeper interface {
GetGaugeByID(ctx sdk.Context, gaugeID uint64) (*incentivestypes.Gauge, error)
}

type SequencerKeeper interface {
RollappSequencersByStatus(ctx sdk.Context, rollappId string, status sequencertypes.OperatingStatus) []sequencertypes.Sequencer
}
Loading