Skip to content

Commit

Permalink
Fix all the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Oct 10, 2021
1 parent a60290b commit 5a39498
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 34 deletions.
12 changes: 7 additions & 5 deletions x/incentives/keeper/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
57 changes: 39 additions & 18 deletions x/incentives/keeper/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,53 @@ 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{
lockDenom: defaultLPDenom,
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)
}
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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)})

Expand All @@ -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()

Expand All @@ -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)})

Expand All @@ -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()
Expand Down Expand Up @@ -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)})

Expand Down Expand Up @@ -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)}
Expand All @@ -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)})

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions x/incentives/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -222,14 +224,15 @@ 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{})
suite.Require().NoError(err)
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)})

Expand All @@ -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)
Expand All @@ -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{})
Expand All @@ -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{})
Expand All @@ -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)})

Expand All @@ -302,16 +307,17 @@ 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{})
suite.Require().NoError(err)
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{})
Expand Down
3 changes: 3 additions & 0 deletions x/incentives/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5a39498

Please sign in to comment.