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

test: add tests for HistoricalRewards #143

Merged
merged 2 commits into from
Sep 30, 2021
Merged
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
12 changes: 8 additions & 4 deletions x/farming/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/tendermint/farming/x/farming/types"
)

func (k Keeper) GetHistoricalRewards(ctx sdk.Context, stakingCoinDenom string, epoch uint64) (rewards types.HistoricalRewards) {
func (k Keeper) GetHistoricalRewards(ctx sdk.Context, stakingCoinDenom string, epoch uint64) (rewards types.HistoricalRewards, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetHistoricalRewardsKey(stakingCoinDenom, epoch))
if bz == nil {
return
}
k.cdc.MustUnmarshal(bz, &rewards)
found = true
return
}

Expand Down Expand Up @@ -132,8 +136,8 @@ func (k Keeper) CalculateRewards(ctx sdk.Context, farmerAcc sdk.AccAddress, stak
return sdk.NewDecCoins()
}

starting := k.GetHistoricalRewards(ctx, stakingCoinDenom, staking.StartingEpoch-1)
ending := k.GetHistoricalRewards(ctx, stakingCoinDenom, endingEpoch)
starting, _ := k.GetHistoricalRewards(ctx, stakingCoinDenom, staking.StartingEpoch-1)
ending, _ := k.GetHistoricalRewards(ctx, stakingCoinDenom, endingEpoch)
diff := ending.CumulativeUnitRewards.Sub(starting.CumulativeUnitRewards)
rewards = diff.MulDecTruncate(staking.Amount.ToDec())
return
Expand Down Expand Up @@ -335,7 +339,7 @@ func (k Keeper) AllocateRewards(ctx sdk.Context) error {

for stakingCoinDenom, unitRewards := range unitRewardsByDenom {
currentEpoch := k.GetCurrentEpoch(ctx, stakingCoinDenom)
historical := k.GetHistoricalRewards(ctx, stakingCoinDenom, currentEpoch-1)
historical, _ := k.GetHistoricalRewards(ctx, stakingCoinDenom, currentEpoch-1)
k.SetHistoricalRewards(ctx, stakingCoinDenom, currentEpoch, types.HistoricalRewards{
CumulativeUnitRewards: historical.CumulativeUnitRewards.Add(unitRewards...),
})
Expand Down
43 changes: 43 additions & 0 deletions x/farming/keeper/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,46 @@ func (suite *KeeperTestSuite) TestHarvest() {
func (suite *KeeperTestSuite) TestMultipleHarvest() {
// TODO: implement
}

func (suite *KeeperTestSuite) TestHistoricalRewards() {
suite.ctx = suite.ctx.WithBlockTime(types.ParseTime("2021-08-06T00:00:00Z"))

// Create two plans that share same staking coin denom in their staking coin weights.
suite.SetFixedAmountPlan(1, suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000})
suite.SetFixedAmountPlan(2, suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000})

// Advancing epoch(s) before any staking is made doesn't create any historical rewards records.
suite.AdvanceEpoch()
suite.AdvanceEpoch()
count := 0
suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) {
count++
return false
})
suite.Equal(count, 0)

suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000)))
// Advancing epoch here marks queued staking coins as staked.
suite.AdvanceEpoch()

// After a farmer has staked(not queued) coins, historical rewards records will be created for each epoch.
// Here we advance epoch three times, and this will create 3 historical rewards records.
suite.AdvanceEpoch()
suite.AdvanceEpoch()
suite.AdvanceEpoch()

// First, ensure that we have only 3 entries in the store.
count = 0
suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) {
count++
return false
})
suite.Require().Equal(count, 3)

// Next, check if cumulative unit rewards is correct.
for i := uint64(0); i < 3; i++ {
historical, found := suite.keeper.GetHistoricalRewards(suite.ctx, denom1, i)
suite.Require().True(found)
suite.Require().True(decCoinsEq(sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, int64((i+1)*2))), historical.CumulativeUnitRewards))
}
}