From a60290bd161b1622d8ef36c06d548aa967c78b7a Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Thu, 7 Oct 2021 01:01:41 -0500 Subject: [PATCH 1/2] Only do one massive send for all gauge distribution --- x/incentives/keeper/gauge.go | 96 +++++++++++------------------------- x/incentives/keeper/hooks.go | 8 +-- x/incentives/types/hooks.go | 6 +-- 3 files changed, 37 insertions(+), 73 deletions(-) diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index 0fbd828ffd2..ad634b56bdd 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -279,57 +279,6 @@ func (k Keeper) FilteredLocksDistributionEst(ctx sdk.Context, gauge types.Gauge, return gauge, filteredDistrCoins, nil } -// Distribute coins from gauge according to its conditions -func (k Keeper) debugDistribute(ctx sdk.Context, gauge types.Gauge) (sdk.Coins, error) { - totalDistrCoins := sdk.NewCoins() - locks := k.GetLocksToDistribution(ctx, gauge.DistributeTo) - lockSum := lockuptypes.SumLocksByDenom(locks, gauge.DistributeTo.Denom) - - if lockSum.IsZero() { - return nil, nil - } - - remainCoins := gauge.Coins.Sub(gauge.DistributedCoins) - remainEpochs := uint64(1) - if !gauge.IsPerpetual { // set remain epochs when it's not perpetual gauge - remainEpochs = gauge.NumEpochsPaidOver - gauge.FilledEpochs - } - 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(gauge.DistributeTo.Denom) - amt := coin.Amount.Mul(denomLockAmt).Quo(lockSum.Mul(sdk.NewInt(int64(remainEpochs)))) - if amt.IsPositive() { - distrCoins = distrCoins.Add(sdk.NewCoin(coin.Denom, amt)) - } - } - distrCoins = distrCoins.Sort() - if distrCoins.Empty() { - continue - } - owner, err := sdk.AccAddressFromBech32(lock.Owner) - if err != nil { - return nil, err - } - if err := k.bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, owner, distrCoins); err != nil { - return nil, err - } - - totalDistrCoins = totalDistrCoins.Add(distrCoins...) - } - - // increase filled epochs after distribution - gauge.FilledEpochs += 1 - gauge.DistributedCoins = gauge.DistributedCoins.Add(totalDistrCoins...) - if err := k.setGauge(ctx, &gauge); err != nil { - return nil, err - } - - k.hooks.AfterDistribute(ctx, gauge.Id) - return totalDistrCoins, nil -} - // distributionInfo stores all of the information for pent up sends for rewards distributions. // This enables us to lower the number of events and calls to back type distributionInfo struct { @@ -369,9 +318,9 @@ func (d *distributionInfo) addLockRewards(lock lockuptypes.PeriodLock, rewards s return nil } -func (k Keeper) doDistributionSends(ctx sdk.Context, denom string, distrs distributionInfo) error { +func (k Keeper) doDistributionSends(ctx sdk.Context, distrs distributionInfo) error { numIDs := len(distrs.idToDecodedAddr) - ctx.Logger().Debug(fmt.Sprintf("Denom %s: Beginning distribution to %d users", denom, numIDs)) + ctx.Logger().Debug(fmt.Sprintf("Beginning distribution to %d users", numIDs)) err := k.bk.SendCoinsFromModuleToManyAccounts( ctx, types.ModuleName, @@ -380,23 +329,24 @@ func (k Keeper) doDistributionSends(ctx sdk.Context, denom string, distrs distri if err != nil { return err } - ctx.Logger().Debug(fmt.Sprintf("Denom %s: Finished sending, now creating liquidity add events", denom)) + ctx.Logger().Debug(fmt.Sprintf("Finished sending, now creating liquidity add events")) for id := 0; id < numIDs; id++ { ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( types.TypeEvtDistribution, - sdk.NewAttribute(types.AttributeLockedDenom, denom), sdk.NewAttribute(types.AttributeReceiver, distrs.idToBech32Addr[id]), sdk.NewAttribute(types.AttributeAmount, distrs.idToDistrCoins[id].String()), ), }) } - ctx.Logger().Debug(fmt.Sprintf("Denom %s: Finished Distributing to %d users", denom, numIDs)) + ctx.Logger().Debug(fmt.Sprintf("Finished Distributing to %d users", numIDs)) return nil } -// Distribute coins from gauge according to its conditions -func (k Keeper) Distribute(ctx sdk.Context, gauge types.Gauge) (sdk.Coins, error) { +// distributeInternal runs the distribution logic for a gauge, and adds the sends to +// the distrInfo computed. It also updates the gauge for the distribution. +func (k Keeper) distributeInternal( + ctx sdk.Context, gauge types.Gauge, distrInfo distributionInfo) (sdk.Coins, error) { totalDistrCoins := sdk.NewCoins() locks := k.GetLocksToDistribution(ctx, gauge.DistributeTo) lockSum := lockuptypes.SumLocksByDenom(locks, gauge.DistributeTo.Denom) @@ -411,8 +361,6 @@ func (k Keeper) Distribute(ctx sdk.Context, gauge types.Gauge) (sdk.Coins, error remainEpochs = gauge.NumEpochsPaidOver - gauge.FilledEpochs } - distrInfo := newDistributionInfo() - for _, lock := range locks { distrCoins := sdk.Coins{} for _, coin := range remainCoins { @@ -436,11 +384,6 @@ func (k Keeper) Distribute(ctx sdk.Context, gauge types.Gauge) (sdk.Coins, error totalDistrCoins = totalDistrCoins.Add(distrCoins...) } - err := k.doDistributionSends(ctx, gauge.DistributeTo.Denom, distrInfo) - if err != nil { - return nil, err - } - // increase filled epochs after distribution gauge.FilledEpochs += 1 gauge.DistributedCoins = gauge.DistributedCoins.Add(totalDistrCoins...) @@ -448,10 +391,31 @@ func (k Keeper) Distribute(ctx sdk.Context, gauge types.Gauge) (sdk.Coins, error return nil, err } - k.hooks.AfterDistribute(ctx, gauge.Id) return totalDistrCoins, nil } +// Distribute coins from gauge according to its conditions +func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, error) { + distrInfo := newDistributionInfo() + + totalDistributedCoins := sdk.Coins{} + for _, gauge := range gauges { + gaugeDistributedCoins, err := k.distributeInternal(ctx, gauge, distrInfo) + if err != nil { + return nil, err + } + totalDistributedCoins = totalDistributedCoins.Add(gaugeDistributedCoins...) + } + + err := k.doDistributionSends(ctx, distrInfo) + if err != nil { + return nil, err + } + + k.hooks.AfterEpochDistribution(ctx) + return totalDistributedCoins, nil +} + // GetModuleToDistributeCoins returns sum of to distribute coins for all of the module func (k Keeper) GetModuleToDistributeCoins(ctx sdk.Context) sdk.Coins { activeGaugesDistr := k.getToDistributeCoinsFromIterator(ctx, k.ActiveGaugesIterator(ctx)) diff --git a/x/incentives/keeper/hooks.go b/x/incentives/keeper/hooks.go index 702f5e562f0..b5bdaa17b93 100644 --- a/x/incentives/keeper/hooks.go +++ b/x/incentives/keeper/hooks.go @@ -23,11 +23,11 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb // distribute due to epoch event gauges = k.GetActiveGauges(ctx) + _, err := k.Distribute(ctx, gauges) + if err != nil { + panic(err) + } for _, gauge := range gauges { - _, err := k.Distribute(ctx, gauge) - if err != nil { - panic(err) - } // filled epoch is increased in this step and we compare with +1 if !gauge.IsPerpetual && gauge.NumEpochsPaidOver <= gauge.FilledEpochs+1 { if err := k.FinishDistribution(ctx, gauge); err != nil { diff --git a/x/incentives/types/hooks.go b/x/incentives/types/hooks.go index 53beae741b7..8eb0d396c18 100644 --- a/x/incentives/types/hooks.go +++ b/x/incentives/types/hooks.go @@ -7,7 +7,7 @@ type IncentiveHooks interface { AfterAddToGauge(ctx sdk.Context, gaugeId uint64) AfterStartDistribution(ctx sdk.Context, gaugeId uint64) AfterFinishDistribution(ctx sdk.Context, gaugeId uint64) - AfterDistribute(ctx sdk.Context, gaugeId uint64) + AfterEpochDistribution(ctx sdk.Context) } var _ IncentiveHooks = MultiIncentiveHooks{} @@ -43,8 +43,8 @@ func (h MultiIncentiveHooks) AfterFinishDistribution(ctx sdk.Context, gaugeId ui } } -func (h MultiIncentiveHooks) AfterDistribute(ctx sdk.Context, gaugeId uint64) { +func (h MultiIncentiveHooks) AfterEpochDistribution(ctx sdk.Context) { for i := range h { - h[i].AfterDistribute(ctx, gaugeId) + h[i].AfterEpochDistribution(ctx) } } From 5a3949877c30e2a7556bd61d85bd5553904dedf4 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sun, 10 Oct 2021 00:33:58 -0500 Subject: [PATCH 2/2] Fix all the tests --- x/incentives/keeper/bench_test.go | 12 +++--- x/incentives/keeper/gauge.go | 10 ++--- x/incentives/keeper/gauge_test.go | 57 ++++++++++++++++++-------- x/incentives/keeper/grpc_query_test.go | 18 +++++--- x/incentives/keeper/keeper_test.go | 3 ++ 5 files changed, 66 insertions(+), 34 deletions(-) diff --git a/x/incentives/keeper/bench_test.go b/x/incentives/keeper/bench_test.go index 8b41aeccda0..9969904ff4b 100644 --- a/x/incentives/keeper/bench_test.go +++ b/x/incentives/keeper/bench_test.go @@ -10,6 +10,7 @@ import ( simtypes "github.com/cosmos/cosmos-sdk/types/simulation" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/osmosis-labs/osmosis/app" + "github.com/osmosis-labs/osmosis/x/incentives/types" lockuptypes "github.com/osmosis-labs/osmosis/x/lockup/types" "github.com/tendermint/tendermint/crypto/secp256k1" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -151,13 +152,14 @@ func benchmarkDistributionLogic(numAccts, numDenoms, numGauges, numLockups, numD b.StartTimer() // distribute coins from gauges to lockup owners for i := 0; i < numDistrs; i++ { + gauges := []types.Gauge{} for _, gaugeId := range gaugeIds { gauge, _ := app.IncentivesKeeper.GetGaugeByID(ctx, gaugeId) - _, err := app.IncentivesKeeper.Distribute(ctx, *gauge) - if err != nil { - fmt.Printf("Distribute, %v\n", err) - b.FailNow() - } + gauges = append(gauges, *gauge) + } + _, err := app.IncentivesKeeper.Distribute(ctx, gauges) + if err != nil { + b.FailNow() } } } diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index ad634b56bdd..cbfb5ba58fb 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -318,7 +318,7 @@ func (d *distributionInfo) addLockRewards(lock lockuptypes.PeriodLock, rewards s return nil } -func (k Keeper) doDistributionSends(ctx sdk.Context, distrs distributionInfo) error { +func (k Keeper) doDistributionSends(ctx sdk.Context, distrs *distributionInfo) error { numIDs := len(distrs.idToDecodedAddr) ctx.Logger().Debug(fmt.Sprintf("Beginning distribution to %d users", numIDs)) err := k.bk.SendCoinsFromModuleToManyAccounts( @@ -329,7 +329,7 @@ func (k Keeper) doDistributionSends(ctx sdk.Context, distrs distributionInfo) er if err != nil { return err } - ctx.Logger().Debug(fmt.Sprintf("Finished sending, now creating liquidity add events")) + ctx.Logger().Debug("Finished sending, now creating liquidity add events") for id := 0; id < numIDs; id++ { ctx.EventManager().EmitEvents(sdk.Events{ sdk.NewEvent( @@ -346,7 +346,7 @@ func (k Keeper) doDistributionSends(ctx sdk.Context, distrs distributionInfo) er // distributeInternal runs the distribution logic for a gauge, and adds the sends to // the distrInfo computed. It also updates the gauge for the distribution. func (k Keeper) distributeInternal( - ctx sdk.Context, gauge types.Gauge, distrInfo distributionInfo) (sdk.Coins, error) { + ctx sdk.Context, gauge types.Gauge, distrInfo *distributionInfo) (sdk.Coins, error) { totalDistrCoins := sdk.NewCoins() locks := k.GetLocksToDistribution(ctx, gauge.DistributeTo) lockSum := lockuptypes.SumLocksByDenom(locks, gauge.DistributeTo.Denom) @@ -400,14 +400,14 @@ func (k Keeper) Distribute(ctx sdk.Context, gauges []types.Gauge) (sdk.Coins, er totalDistributedCoins := sdk.Coins{} for _, gauge := range gauges { - gaugeDistributedCoins, err := k.distributeInternal(ctx, gauge, distrInfo) + gaugeDistributedCoins, err := k.distributeInternal(ctx, gauge, &distrInfo) if err != nil { return nil, err } totalDistributedCoins = totalDistributedCoins.Add(gaugeDistributedCoins...) } - err := k.doDistributionSends(ctx, distrInfo) + err := k.doDistributionSends(ctx, &distrInfo) if err != nil { return nil, err } diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index b8c55f25b74..a946ea5f8e9 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -12,7 +12,7 @@ import ( // a provided gauge, func (suite *KeeperTestSuite) TestDistribute() { twoLockupUser := userLocks{ - lockDurations: []time.Duration{time.Second, 2 * time.Second}, + lockDurations: []time.Duration{defaultLockDuration, 2 * defaultLockDuration}, lockAmounts: []sdk.Coins{defaultLPTokens, defaultLPTokens}, } defaultGauge := perpGaugeDesc{ @@ -20,31 +20,45 @@ func (suite *KeeperTestSuite) TestDistribute() { lockDuration: defaultLockDuration, rewardAmount: sdk.Coins{sdk.NewInt64Coin(defaultRewardDenom, 3000)}, } + doubleLengthGauge := perpGaugeDesc{ + lockDenom: defaultLPDenom, + lockDuration: 2 * defaultLockDuration, + rewardAmount: sdk.Coins{sdk.NewInt64Coin(defaultRewardDenom, 3000)}, + } oneKRewardCoins := sdk.Coins{sdk.NewInt64Coin(defaultRewardDenom, 1000)} - twoKRewardCoins := oneKRewardCoins.Add(oneKRewardCoins...) + twoKRewardCoins := sdk.Coins{sdk.NewInt64Coin(defaultRewardDenom, 2000)} + fiveKRewardCoins := sdk.Coins{sdk.NewInt64Coin(defaultRewardDenom, 5000)} tests := []struct { users []userLocks gauges []perpGaugeDesc expectedRewards []sdk.Coins }{ + // gauge 1 gives 3k coins. Three locks, all eligible. 1k coins per lock + // so 1k to oneLockupUser, 2k to twoLockupUser { users: []userLocks{oneLockupUser, twoLockupUser}, gauges: []perpGaugeDesc{defaultGauge}, expectedRewards: []sdk.Coins{oneKRewardCoins, twoKRewardCoins}, }, + // gauge 1 gives 3k coins. Three locks, all eligible. + // gauge 2 gives 3k coins to one lock, in twoLockupUser + // so 1k to oneLockupUser, 5k to twoLockupUser + { + users: []userLocks{oneLockupUser, twoLockupUser}, + gauges: []perpGaugeDesc{defaultGauge, doubleLengthGauge}, + expectedRewards: []sdk.Coins{oneKRewardCoins, fiveKRewardCoins}, + }, } - for _, tc := range tests { + for tcIndex, tc := range tests { suite.SetupTest() gauges := suite.SetupGauges(tc.gauges) addrs := suite.SetupUserLocks(tc.users) - for _, g := range gauges { - _, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, g) - suite.Require().NoError(err) - } + _, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, gauges) + suite.Require().NoError(err) // Check expected rewards for i, addr := range addrs { bal := suite.app.BankKeeper.GetAllBalances(suite.ctx, addr) - suite.Require().Equal(bal.String(), tc.expectedRewards[i].String()) + suite.Require().Equal(tc.expectedRewards[i].String(), bal.String(), "tcnum %d, person %d", tcIndex, i) } } } @@ -66,6 +80,8 @@ func (suite *KeeperTestSuite) TestInvalidDurationGaugeCreationValidation() { suite.Require().NoError(err) } +// TODO: Make this test table driven, or move whatever it tests into +// the much simpler TestDistribute func (suite *KeeperTestSuite) TestGetModuleToDistributeCoins() { // test for module get gauges suite.SetupTest() @@ -101,7 +117,7 @@ func (suite *KeeperTestSuite) TestGetModuleToDistributeCoins() { suite.Require().NoError(err) // distribute coins to stakers - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 105)}) @@ -110,6 +126,8 @@ func (suite *KeeperTestSuite) TestGetModuleToDistributeCoins() { suite.Require().Equal(coins, gaugeCoins.Add(addCoins...).Add(gaugeCoins2...).Sub(distrCoins)) } +// TODO: Make this test table driven, or move whatever it tests into +// the much simpler TestDistribute func (suite *KeeperTestSuite) TestGetModuleDistributedCoins() { suite.SetupTest() @@ -132,7 +150,7 @@ func (suite *KeeperTestSuite) TestGetModuleDistributedCoins() { suite.Require().NoError(err) // distribute coins to stakers - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 5)}) @@ -141,6 +159,9 @@ func (suite *KeeperTestSuite) TestGetModuleDistributedCoins() { suite.Require().Equal(coins, distrCoins) } +// TODO: Make this test table driven +// OR if it needs to be script based, +// remove lots of boilerplate so this can actually be followed func (suite *KeeperTestSuite) TestNonPerpetualGaugeOperations() { // test for module get gauges suite.SetupTest() @@ -207,7 +228,7 @@ func (suite *KeeperTestSuite) TestNonPerpetualGaugeOperations() { suite.Require().Len(gauges, 0) // distribute coins to stakers - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 105)}) @@ -310,16 +331,16 @@ func (suite *KeeperTestSuite) TestPerpetualGaugeOperations() { suite.Require().Len(gauges, 0) // distribute coins to stakers, since it's perpetual distribute everything on single distribution - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 10)}) // distributing twice without adding more for perpetual gauge gauge, err = suite.app.IncentivesKeeper.GetGaugeByID(suite.ctx, gaugeID) suite.Require().NoError(err) - distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) - suite.Require().Equal(distrCoins, sdk.Coins{}) + suite.Require().True(distrCoins.Empty()) // add to gauge addCoins := sdk.Coins{sdk.NewInt64Coin("stake", 200)} @@ -328,7 +349,7 @@ func (suite *KeeperTestSuite) TestPerpetualGaugeOperations() { // distributing twice with adding more for perpetual gauge gauge, err = suite.app.IncentivesKeeper.GetGaugeByID(suite.ctx, gaugeID) suite.Require().NoError(err) - distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 200)}) @@ -384,7 +405,7 @@ func (suite *KeeperTestSuite) TestNoLockPerpetualGaugeDistribution() { suite.Require().NoError(err) // distribute coins to stakers, since it's perpetual distribute everything on single distribution - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins(nil)) @@ -429,7 +450,7 @@ func (suite *KeeperTestSuite) TestNoLockNonPerpetualGaugeDistribution() { suite.Require().NoError(err) // distribute coins to stakers, since it's perpetual distribute everything on single distribution - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins(nil)) @@ -474,7 +495,7 @@ func (suite *KeeperTestSuite) TestGaugesByDenom() { suite.Require().Len(gaugeIds, 0) // distribute coins to stakers - _, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + _, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, []types.Gauge{*gauge}) suite.Require().NoError(err) // finish distribution for non perpetual gauge diff --git a/x/incentives/keeper/grpc_query_test.go b/x/incentives/keeper/grpc_query_test.go index b3f5fa7ba6e..3b4dddb856e 100644 --- a/x/incentives/keeper/grpc_query_test.go +++ b/x/incentives/keeper/grpc_query_test.go @@ -203,6 +203,8 @@ func (suite *KeeperTestSuite) TestRewardsEstWithPoolIncentives() { suite.Require().Equal(res.Coins, coins.Add(mintCoins)) } +// TODO: make this test table driven, or simpler +// I have no idea at a glance what its doing. func (suite *KeeperTestSuite) TestGRPCToDistributeCoins() { suite.SetupTest() @@ -222,6 +224,7 @@ func (suite *KeeperTestSuite) TestGRPCToDistributeCoins() { gauge, err := suite.app.IncentivesKeeper.GetGaugeByID(suite.ctx, gaugeID) suite.Require().NoError(err) suite.Require().NotNil(gauge) + gauges := []types.Gauge{*gauge} // check after gauge creation res, err = suite.app.IncentivesKeeper.ModuleToDistributeCoins(sdk.WrapSDKContext(suite.ctx), &types.ModuleToDistributeCoinsRequest{}) @@ -229,7 +232,7 @@ func (suite *KeeperTestSuite) TestGRPCToDistributeCoins() { suite.Require().Equal(res.Coins, coins) // distribute coins to stakers - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, gauges) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 4)}) @@ -239,6 +242,7 @@ func (suite *KeeperTestSuite) TestGRPCToDistributeCoins() { suite.Require().NotNil(gauge) suite.Require().Equal(gauge.FilledEpochs, uint64(1)) suite.Require().Equal(gauge.DistributedCoins, sdk.Coins{sdk.NewInt64Coin("stake", 4)}) + gauges = []types.Gauge{*gauge} // start distribution suite.ctx = suite.ctx.WithBlockTime(startTime) @@ -251,9 +255,9 @@ func (suite *KeeperTestSuite) TestGRPCToDistributeCoins() { suite.Require().Equal(res.Coins, coins.Sub(distrCoins)) // distribute second round to stakers - distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, gauges) suite.Require().NoError(err) - suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 6)}) + suite.Require().Equal(sdk.Coins{sdk.NewInt64Coin("stake", 6)}, distrCoins) // final check res, err = suite.app.IncentivesKeeper.ModuleToDistributeCoins(sdk.WrapSDKContext(suite.ctx), &types.ModuleToDistributeCoinsRequest{}) @@ -280,6 +284,7 @@ func (suite *KeeperTestSuite) TestGRPCDistributedCoins() { gauge, err := suite.app.IncentivesKeeper.GetGaugeByID(suite.ctx, gaugeID) suite.Require().NoError(err) suite.Require().NotNil(gauge) + gauges := []types.Gauge{*gauge} // check after gauge creation res, err = suite.app.IncentivesKeeper.ModuleDistributedCoins(sdk.WrapSDKContext(suite.ctx), &types.ModuleDistributedCoinsRequest{}) @@ -292,7 +297,7 @@ func (suite *KeeperTestSuite) TestGRPCDistributedCoins() { suite.Require().NoError(err) // distribute coins to stakers - distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err := suite.app.IncentivesKeeper.Distribute(suite.ctx, gauges) suite.Require().NoError(err) suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 4)}) @@ -302,6 +307,7 @@ func (suite *KeeperTestSuite) TestGRPCDistributedCoins() { suite.Require().NotNil(gauge) suite.Require().Equal(gauge.FilledEpochs, uint64(1)) suite.Require().Equal(gauge.DistributedCoins, sdk.Coins{sdk.NewInt64Coin("stake", 4)}) + gauges = []types.Gauge{*gauge} // check after distribution res, err = suite.app.IncentivesKeeper.ModuleDistributedCoins(sdk.WrapSDKContext(suite.ctx), &types.ModuleDistributedCoinsRequest{}) @@ -309,9 +315,9 @@ func (suite *KeeperTestSuite) TestGRPCDistributedCoins() { suite.Require().Equal(res.Coins, distrCoins) // distribute second round to stakers - distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, *gauge) + distrCoins, err = suite.app.IncentivesKeeper.Distribute(suite.ctx, gauges) suite.Require().NoError(err) - suite.Require().Equal(distrCoins, sdk.Coins{sdk.NewInt64Coin("stake", 6)}) + suite.Require().Equal(sdk.Coins{sdk.NewInt64Coin("stake", 6)}, distrCoins) // final check res, err = suite.app.IncentivesKeeper.ModuleDistributedCoins(sdk.WrapSDKContext(suite.ctx), &types.ModuleDistributedCoinsRequest{}) diff --git a/x/incentives/keeper/keeper_test.go b/x/incentives/keeper/keeper_test.go index f56e2d2e275..a9926d901ef 100644 --- a/x/incentives/keeper/keeper_test.go +++ b/x/incentives/keeper/keeper_test.go @@ -21,6 +21,9 @@ type KeeperTestSuite struct { func (suite *KeeperTestSuite) SetupTest() { suite.app = app.Setup(false) suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "osmosis-1", Time: time.Now().UTC()}) + lockableDurations := suite.app.IncentivesKeeper.GetLockableDurations(suite.ctx) + lockableDurations = append(lockableDurations, 2*time.Second) + suite.app.IncentivesKeeper.SetLockableDurations(suite.ctx, lockableDurations) } func TestKeeperTestSuite(t *testing.T) {