diff --git a/client/docs/statik/statik.go b/client/docs/statik/statik.go index ee79fad2..8e4c7777 100644 --- a/client/docs/statik/statik.go +++ b/client/docs/statik/statik.go @@ -1,5 +1,6 @@ // Code generated by statik. DO NOT EDIT. +// Package statik contains static assets. package statik import ( diff --git a/proto/tendermint/farming/v1beta1/farming.proto b/proto/tendermint/farming/v1beta1/farming.proto index 8dddbf4d..0e856b41 100644 --- a/proto/tendermint/farming/v1beta1/farming.proto +++ b/proto/tendermint/farming/v1beta1/farming.proto @@ -170,6 +170,8 @@ message HistoricalRewards { (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false ]; + + uint32 reference_count = 2 [(gogoproto.moretags) = "yaml:\"reference_count\""]; } // OutstandingRewards represents outstanding (un-withdrawn) rewards diff --git a/x/farming/keeper/invariants_test.go b/x/farming/keeper/invariants_test.go index 7c52af25..ba01923e 100644 --- a/x/farming/keeper/invariants_test.go +++ b/x/farming/keeper/invariants_test.go @@ -121,7 +121,7 @@ func (suite *KeeperTestSuite) TestStakingReservedAmountInvariant() { func (suite *KeeperTestSuite) TestRemainingRewardsAmountInvariant() { k, ctx := suite.keeper, suite.ctx - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.AdvanceEpoch() @@ -203,7 +203,7 @@ func (suite *KeeperTestSuite) TestNonNegativeOutstandingRewardsInvariant() { func (suite *KeeperTestSuite) TestOutstandingRewardsAmountInvariant() { k, ctx := suite.keeper, suite.ctx - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.AdvanceEpoch() diff --git a/x/farming/keeper/keeper_test.go b/x/farming/keeper/keeper_test.go index aa7a7149..57862103 100644 --- a/x/farming/keeper/keeper_test.go +++ b/x/farming/keeper/keeper_test.go @@ -166,15 +166,15 @@ func (suite *KeeperTestSuite) AdvanceEpoch() { suite.Require().NoError(err) } -func (suite *KeeperTestSuite) CreateFixedAmountPlan(farmingPoolAcc sdk.AccAddress, stakingCoinWeightsMap map[string]string, epochAmountMap map[string]int64) { - stakingCoinWeights := sdk.NewDecCoins() - for denom, weight := range stakingCoinWeightsMap { - stakingCoinWeights = stakingCoinWeights.Add(sdk.NewDecCoinFromDec(denom, sdk.MustNewDecFromStr(weight))) +func (suite *KeeperTestSuite) CreateFixedAmountPlan(farmingPoolAcc sdk.AccAddress, stakingCoinWeightsStr, epochAmountStr string) { + stakingCoinWeights, err := sdk.ParseDecCoins(stakingCoinWeightsStr) + if err != nil { + panic(err) } - epochAmount := sdk.NewCoins() - for denom, amount := range epochAmountMap { - epochAmount = epochAmount.Add(sdk.NewInt64Coin(denom, amount)) + epochAmount, err := sdk.ParseCoinsNormalized(epochAmountStr) + if err != nil { + panic(err) } msg := types.NewMsgCreateFixedAmountPlan( @@ -185,14 +185,14 @@ func (suite *KeeperTestSuite) CreateFixedAmountPlan(farmingPoolAcc sdk.AccAddres types.ParseTime("9999-12-31T00:00:00Z"), epochAmount, ) - _, err := suite.keeper.CreateFixedAmountPlan(suite.ctx, msg, farmingPoolAcc, farmingPoolAcc, types.PlanTypePublic) + _, err = suite.keeper.CreateFixedAmountPlan(suite.ctx, msg, farmingPoolAcc, farmingPoolAcc, types.PlanTypePublic) suite.Require().NoError(err) } -func (suite *KeeperTestSuite) CreateRatioPlan(farmingPoolAcc sdk.AccAddress, stakingCoinWeightsMap map[string]string, epochRatioStr string) { - stakingCoinWeights := sdk.NewDecCoins() - for denom, weight := range stakingCoinWeightsMap { - stakingCoinWeights = stakingCoinWeights.Add(sdk.NewDecCoinFromDec(denom, sdk.MustNewDecFromStr(weight))) +func (suite *KeeperTestSuite) CreateRatioPlan(farmingPoolAcc sdk.AccAddress, stakingCoinWeightsStr, epochRatioStr string) { + stakingCoinWeights, err := sdk.ParseDecCoins(stakingCoinWeightsStr) + if err != nil { + panic(err) } epochRatio := sdk.MustNewDecFromStr(epochRatioStr) @@ -205,7 +205,7 @@ func (suite *KeeperTestSuite) CreateRatioPlan(farmingPoolAcc sdk.AccAddress, sta types.ParseTime("9999-12-31T00:00:00Z"), epochRatio, ) - _, err := suite.keeper.CreateRatioPlan(suite.ctx, msg, farmingPoolAcc, farmingPoolAcc, types.PlanTypePublic) + _, err = suite.keeper.CreateRatioPlan(suite.ctx, msg, farmingPoolAcc, farmingPoolAcc, types.PlanTypePublic) suite.Require().NoError(err) } diff --git a/x/farming/keeper/proposal_handler_test.go b/x/farming/keeper/proposal_handler_test.go index 33a88164..df67d661 100644 --- a/x/farming/keeper/proposal_handler_test.go +++ b/x/farming/keeper/proposal_handler_test.go @@ -113,7 +113,7 @@ func testModifyPlanRequest( } func (suite *KeeperTestSuite) TestModifyPlanRequest() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") addr := suite.addrs[5].String() @@ -175,7 +175,7 @@ func (suite *KeeperTestSuite) TestModifyPlanRequest() { } func (suite *KeeperTestSuite) TestDeletePlanRequest() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") proposal := types.NewPublicPlanProposal("title", "description", nil, nil, []types.DeletePlanRequest{{PlanId: 1}}) err := suite.govHandler(suite.ctx, proposal) @@ -185,7 +185,7 @@ func (suite *KeeperTestSuite) TestDeletePlanRequest() { suite.Require().Empty(plans) // Test for private plan cannot be deleted. - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") plans = suite.keeper.GetPlans(suite.ctx) suite.Require().Equal(plans[0].GetId(), uint64(2)) @@ -200,7 +200,7 @@ func (suite *KeeperTestSuite) TestDeletePlanRequest() { } func (suite *KeeperTestSuite) TestWithdrawRewardsAfterPlanDeleted() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -228,7 +228,7 @@ func (suite *KeeperTestSuite) TestWithdrawRewardsAfterPlanDeleted() { } func (suite *KeeperTestSuite) TestWithdrawRewardsAfterPlanTerminated() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -265,7 +265,7 @@ func (suite *KeeperTestSuite) TestAccumulatedRewardsAfterPlanModification() { sdk.NewInt64Coin(denom3, 10000000), ))[0] - suite.CreateRatioPlan(farmingPool, map[string]string{denom1: "1"}, "0.1") + suite.CreateRatioPlan(farmingPool, "1denom1", "0.1") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.AdvanceEpoch() diff --git a/x/farming/keeper/reward.go b/x/farming/keeper/reward.go index b58c6873..b9a5847c 100644 --- a/x/farming/keeper/reward.go +++ b/x/farming/keeper/reward.go @@ -66,6 +66,33 @@ func (k Keeper) IterateHistoricalRewards(ctx sdk.Context, cb func(stakingCoinDen } } +// incrementReferenceCount increments the reference count for a historical rewards. +func (k Keeper) incrementReferenceCount(ctx sdk.Context, stakingCoinDenom string, epoch uint64) { + historical, found := k.GetHistoricalRewards(ctx, stakingCoinDenom, epoch) + if !found { + panic("historical rewards not found") + } + historical.ReferenceCount++ + k.SetHistoricalRewards(ctx, stakingCoinDenom, epoch, historical) +} + +// decrementReferenceCount decrements the reference count for a historical rewards. +func (k Keeper) decrementReferenceCount(ctx sdk.Context, stakingCoinDenom string, epoch uint64) { + historical, found := k.GetHistoricalRewards(ctx, stakingCoinDenom, epoch) + if !found { + panic("historical rewards not found") + } + if historical.ReferenceCount == 0 { + panic("cannot set negative reference count") + } + historical.ReferenceCount-- + if historical.ReferenceCount == 0 { + k.DeleteHistoricalRewards(ctx, stakingCoinDenom, epoch) + } else { + k.SetHistoricalRewards(ctx, stakingCoinDenom, epoch, historical) + } +} + // GetCurrentEpoch returns the current epoch number for a given // staking coin denom. func (k Keeper) GetCurrentEpoch(ctx sdk.Context, stakingCoinDenom string) uint64 { @@ -226,6 +253,9 @@ func (k Keeper) WithdrawRewards(ctx sdk.Context, farmerAcc sdk.AccAddress, staki } currentEpoch := k.GetCurrentEpoch(ctx, stakingCoinDenom) + if currentEpoch == staking.StartingEpoch { + return sdk.Coins{}, nil + } rewards := k.CalculateRewards(ctx, farmerAcc, stakingCoinDenom, currentEpoch-1) truncatedRewards, _ := rewards.TruncateDecimal() @@ -248,6 +278,8 @@ func (k Keeper) WithdrawRewards(ctx sdk.Context, farmerAcc sdk.AccAddress, staki k.DecreaseOutstandingRewards(ctx, stakingCoinDenom, rewards) } + k.decrementReferenceCount(ctx, stakingCoinDenom, staking.StartingEpoch-1) + k.incrementReferenceCount(ctx, stakingCoinDenom, currentEpoch-1) staking.StartingEpoch = currentEpoch k.SetStaking(ctx, stakingCoinDenom, farmerAcc, staking) @@ -461,8 +493,10 @@ 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) + k.decrementReferenceCount(ctx, stakingCoinDenom, currentEpoch-1) k.SetHistoricalRewards(ctx, stakingCoinDenom, currentEpoch, types.HistoricalRewards{ CumulativeUnitRewards: historical.CumulativeUnitRewards.Add(unitRewards...), + ReferenceCount: 1, }) k.SetCurrentEpoch(ctx, stakingCoinDenom, currentEpoch+1) } diff --git a/x/farming/keeper/reward_test.go b/x/farming/keeper/reward_test.go index e4f61294..89f071bd 100644 --- a/x/farming/keeper/reward_test.go +++ b/x/farming/keeper/reward_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "math/rand" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -256,8 +257,8 @@ func (suite *KeeperTestSuite) TestAllocateRewards_FixedAmountPlanAllBalances() { suite.Require().NoError(err) // The sum of epoch ratios is exactly 1. - suite.CreateFixedAmountPlan(farmingPoolAcc, map[string]string{denom1: "1"}, map[string]int64{denom3: 600000}) - suite.CreateFixedAmountPlan(farmingPoolAcc, map[string]string{denom2: "1"}, map[string]int64{denom3: 400000}) + suite.CreateFixedAmountPlan(farmingPoolAcc, "1denom1", "600000denom3") + suite.CreateFixedAmountPlan(farmingPoolAcc, "1denom2", "400000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000), sdk.NewInt64Coin(denom2, 1000000))) @@ -274,8 +275,8 @@ func (suite *KeeperTestSuite) TestAllocateRewards_RatioPlanAllBalances() { suite.Require().NoError(err) // The sum of epoch ratios is exactly 1. - suite.CreateRatioPlan(farmingPoolAcc, map[string]string{denom1: "1"}, "0.5") - suite.CreateRatioPlan(farmingPoolAcc, map[string]string{denom2: "1"}, "0.5") + suite.CreateRatioPlan(farmingPoolAcc, "1denom1", "0.5") + suite.CreateRatioPlan(farmingPoolAcc, "1denom2", "0.5") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000), sdk.NewInt64Coin(denom2, 1000000))) @@ -293,8 +294,8 @@ func (suite *KeeperTestSuite) TestAllocateRewards_FixedAmountPlanOverBalances() // The sum of epoch amounts is over the balances the farming pool has, // so the reward allocation should never happen. - suite.CreateFixedAmountPlan(farmingPoolAcc, map[string]string{denom1: "1"}, map[string]int64{denom3: 700000}) - suite.CreateFixedAmountPlan(farmingPoolAcc, map[string]string{denom2: "1"}, map[string]int64{denom3: 400000}) + suite.CreateFixedAmountPlan(farmingPoolAcc, "1denom1", "700000denom3") + suite.CreateFixedAmountPlan(farmingPoolAcc, "1denom2", "400000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000), sdk.NewInt64Coin(denom2, 1000000))) @@ -311,8 +312,8 @@ func (suite *KeeperTestSuite) TestAllocateRewards_RatioPlanOverBalances() { suite.Require().NoError(err) // The sum of epoch ratios is over 1, so the reward allocation should never happen. - suite.CreateRatioPlan(farmingPoolAcc, map[string]string{denom1: "1"}, "0.8") - suite.CreateRatioPlan(farmingPoolAcc, map[string]string{denom2: "1"}, "0.5") + suite.CreateRatioPlan(farmingPoolAcc, "1denom1", "0.8") + suite.CreateRatioPlan(farmingPoolAcc, "1denom2", "0.5") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000), sdk.NewInt64Coin(denom2, 1000000))) @@ -327,14 +328,14 @@ func (suite *KeeperTestSuite) TestOutstandingRewards() { // The block time here is not important, and has chosen randomly. suite.ctx = suite.ctx.WithBlockTime(types.ParseTime("2021-09-01T00:00:00Z")) - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000denom3") // Three farmers stake same amount of coins. suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.Stake(suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.Stake(suite.addrs[2], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) - // At first, the outstanding rewards shouldn't exist. + // At first, the outstanding rewards should be zero. _, found := suite.keeper.GetOutstandingRewards(suite.ctx, denom1) suite.Require().False(found) @@ -384,7 +385,7 @@ func (suite *KeeperTestSuite) TestHarvest() { } func (suite *KeeperTestSuite) TestMultipleHarvest() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -407,8 +408,8 @@ 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.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") // Advancing epoch(s) before any staking is made doesn't create any historical rewards records. suite.AdvanceEpoch() @@ -430,25 +431,28 @@ func (suite *KeeperTestSuite) TestHistoricalRewards() { suite.AdvanceEpoch() suite.AdvanceEpoch() - // First, ensure that we have only 3 entries in the store. + // First, ensure that we have only two entries in the store. + // One is for the epoch 0, and other one is for epoch 3. count = 0 suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { count++ return false }) - suite.Require().Equal(4, count) + suite.Require().Equal(2, count) // Next, check if cumulative unit rewards is correct. - for i := uint64(1); 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*2))), historical.CumulativeUnitRewards)) - } + historical, found := suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 0) + suite.Require().True(found) + suite.Require().True(decCoinsEq(sdk.DecCoins{}, historical.CumulativeUnitRewards)) + + historical, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 3) + suite.Require().True(found) + suite.Require().True(decCoinsEq(sdk.NewDecCoins(sdk.NewInt64DecCoin(denom3, 6)), historical.CumulativeUnitRewards)) } // Test if initialization and pruning of staking coin info work properly. func (suite *KeeperTestSuite) TestInitializeAndPruneStakingCoinInfo() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -502,3 +506,183 @@ func (suite *KeeperTestSuite) TestInitializeAndPruneStakingCoinInfo() { _, found = suite.keeper.GetOutstandingRewards(suite.ctx, denom1) suite.Require().False(found) } + +func (suite *KeeperTestSuite) TestPruneHistoricalRewards() { + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") + + for i := 0; i < 3; i++ { + suite.AdvanceEpoch() + } + + for _, addr := range suite.addrs[:4] { + suite.Stake(addr, sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + } + + for i := 0; i < 3; i++ { + suite.AdvanceEpoch() + } + + for _, addr := range suite.addrs[:4] { + suite.Stake(addr, sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + } + + for i := 0; i < 3; i++ { + suite.AdvanceEpoch() + } + + for _, addr := range suite.addrs[:4] { + suite.Unstake(addr, sdk.NewCoins(sdk.NewInt64Coin(denom1, 2000000))) + } + + cnt := 0 + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + cnt++ + return false + }) + suite.Require().Zero(cnt) +} + +func (suite *KeeperTestSuite) TestReferenceCounting() { + // Staking coins before any plan has created will create a historical + // rewards record at epoch 0 for that denom. + suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000))) + suite.Stake(suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000))) + suite.AdvanceEpoch() + + // Even for multiple stakings, it does not increment the denom's current epoch. + suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000))) + suite.Stake(suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000))) + suite.AdvanceEpoch() + + historical, found := suite.keeper.GetHistoricalRewards(suite.ctx, sdk.DefaultBondDenom, 0) + suite.Require().True(found) + suite.Require().True(decCoinsEq(sdk.DecCoins{}, historical.CumulativeUnitRewards)) + suite.Require().Equal(uint32(3), historical.ReferenceCount) + + // Next, create two plans. + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") + suite.CreateRatioPlan(suite.addrs[5], "1denom1", "0.01") + + // Farmers stake. + suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + suite.Stake(suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + + suite.AdvanceEpoch() + suite.AdvanceEpoch() // Rewards allocated. + suite.AdvanceEpoch() + suite.AdvanceEpoch() + + // Ensure there are only two historical rewards records found, and check + // reference counts. + cnt := 0 + refCnt := uint32(0) + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + if stakingCoinDenom == denom1 { + cnt++ + refCnt += rewards.ReferenceCount + } + return false + }) + suite.Require().Equal(2, cnt) + suite.Require().Equal(uint32(3), refCnt) + + // After a farmer harvests, check historical rewards records again. + suite.Harvest(suite.addrs[0], []string{denom1}) + + cnt = 0 + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + if stakingCoinDenom == denom1 { + cnt++ + } + return false + }) + suite.Require().Equal(2, cnt) + historical, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 0) + suite.Require().True(found) + suite.Require().Equal(uint32(1), historical.ReferenceCount) + historical, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 3) + suite.Require().True(found) + suite.Require().Equal(uint32(2), historical.ReferenceCount) + + suite.AdvanceEpoch() + suite.AdvanceEpoch() + + suite.Unstake(suite.addrs[1], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + refCnt = 0 + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + if stakingCoinDenom == denom1 { + refCnt += rewards.ReferenceCount + } + return false + }) + suite.Require().Equal(uint32(2), refCnt) + _, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 0) + suite.Require().False(found) + historical, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 3) + suite.Require().True(found) + suite.Require().Equal(uint32(1), historical.ReferenceCount) + historical, found = suite.keeper.GetHistoricalRewards(suite.ctx, denom1, 5) + suite.Require().True(found) + suite.Require().Equal(uint32(1), historical.ReferenceCount) +} + +func (suite *KeeperTestSuite) TestReferenceCountingFuzz() { + for seed := int64(0); seed < 100; seed++ { + r := rand.New(rand.NewSource(seed)) + + suite.SetupTest() + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") + suite.CreateRatioPlan(suite.addrs[5], "1denom1", "0.0001") + + staked := map[int]sdk.Coins{} + queued := map[int]sdk.Coins{} + for epoch := 0; epoch < 100; epoch++ { + for i, addr := range suite.addrs[:5] { + if r.Intn(5) == 0 { // Unstake with a 20% chance. + amount, ok := staked[i] + if ok { + if r.Intn(5) == 0 { + // Unstake all(staked) coins. + suite.Unstake(addr, amount) + delete(staked, i) + } else { + // Unstake staked - (1 ~ (staked - 1)) coins. + if amount.AmountOf(denom1).GT(sdk.OneInt()) { + subAmount := sdk.NewCoins(sdk.NewInt64Coin(denom1, 1+r.Int63n(amount.AmountOf(denom1).Int64()-1))) + suite.Unstake(addr, subAmount) + staked[i] = amount.Sub(subAmount) + } + } + } + } + if r.Intn(5) == 0 { // Stake with a 20% chance. + amount := sdk.NewCoins(sdk.NewInt64Coin(denom1, 1+r.Int63n(50))) + suite.Stake(addr, amount) + queued[i] = amount + } + if r.Intn(5) == 0 { + _, ok := staked[i] + if ok { + suite.Harvest(addr, []string{denom1}) + } + } + } + suite.AdvanceEpoch() + for i, amount := range queued { + staked[i] = staked[i].Add(amount...) + delete(queued, i) + } + + refCnt := uint32(0) + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + refCnt += rewards.ReferenceCount + return false + }) + if len(staked) > 0 { + suite.Require().Equal(uint32(len(staked))+1, refCnt) + } else { + suite.Require().Equal(uint32(0), refCnt) + } + } + } +} diff --git a/x/farming/keeper/staking.go b/x/farming/keeper/staking.go index 6a2d2884..5b107c44 100644 --- a/x/farming/keeper/staking.go +++ b/x/farming/keeper/staking.go @@ -269,7 +269,7 @@ func (k Keeper) ReleaseStakingCoins(ctx sdk.Context, farmerAcc sdk.AccAddress, s // afterStakingCoinAdded is called after a new staking coin denom appeared // during ProcessQueuedCoins. func (k Keeper) afterStakingCoinAdded(ctx sdk.Context, stakingCoinDenom string) { - k.SetHistoricalRewards(ctx, stakingCoinDenom, 0, types.HistoricalRewards{CumulativeUnitRewards: sdk.DecCoins{}}) + k.SetHistoricalRewards(ctx, stakingCoinDenom, 0, types.HistoricalRewards{CumulativeUnitRewards: sdk.DecCoins{}, ReferenceCount: 1}) k.SetCurrentEpoch(ctx, stakingCoinDenom, 1) k.SetOutstandingRewards(ctx, stakingCoinDenom, types.OutstandingRewards{Rewards: sdk.DecCoins{}}) } @@ -359,13 +359,15 @@ func (k Keeper) Unstake(ctx sdk.Context, farmerAcc sdk.AccAddress, amount sdk.Co return err } + currentEpoch := k.GetCurrentEpoch(ctx, coin.Denom) + removedFromStaking := queuedStaking.Amount.Neg() // Make negative a positive staking.Amount = staking.Amount.Sub(removedFromStaking) if staking.Amount.IsPositive() { - currentEpoch := k.GetCurrentEpoch(ctx, coin.Denom) staking.StartingEpoch = currentEpoch k.SetStaking(ctx, coin.Denom, farmerAcc, staking) } else { + k.decrementReferenceCount(ctx, coin.Denom, currentEpoch-1) k.DeleteStaking(ctx, coin.Denom, farmerAcc) } @@ -397,21 +399,29 @@ func (k Keeper) Unstake(ctx sdk.Context, farmerAcc sdk.AccAddress, amount sdk.Co // It causes accumulated rewards to be withdrawn to the farmer. func (k Keeper) ProcessQueuedCoins(ctx sdk.Context) { k.IterateQueuedStakings(ctx, func(stakingCoinDenom string, farmerAcc sdk.AccAddress, queuedStaking types.QueuedStaking) (stop bool) { + incremented := false + staking, found := k.GetStaking(ctx, stakingCoinDenom, farmerAcc) if found { if _, err := k.WithdrawRewards(ctx, farmerAcc, stakingCoinDenom); err != nil { panic(err) } + incremented = true } else { staking.Amount = sdk.ZeroInt() } k.DeleteQueuedStaking(ctx, stakingCoinDenom, farmerAcc) k.IncreaseTotalStakings(ctx, stakingCoinDenom, queuedStaking.Amount) + + currentEpoch := k.GetCurrentEpoch(ctx, stakingCoinDenom) k.SetStaking(ctx, stakingCoinDenom, farmerAcc, types.Staking{ Amount: staking.Amount.Add(queuedStaking.Amount), - StartingEpoch: k.GetCurrentEpoch(ctx, stakingCoinDenom), + StartingEpoch: currentEpoch, }) + if !incremented { + k.incrementReferenceCount(ctx, stakingCoinDenom, currentEpoch-1) + } return false }) diff --git a/x/farming/keeper/staking_test.go b/x/farming/keeper/staking_test.go index 6852131b..134f6164 100644 --- a/x/farming/keeper/staking_test.go +++ b/x/farming/keeper/staking_test.go @@ -4,9 +4,11 @@ import ( "math/rand" sdk "github.com/cosmos/cosmos-sdk/types" - _ "github.com/stretchr/testify/suite" + simapp "github.com/tendermint/farming/app" "github.com/tendermint/farming/x/farming/types" + + _ "github.com/stretchr/testify/suite" ) func (suite *KeeperTestSuite) TestStake() { @@ -46,7 +48,7 @@ func (suite *KeeperTestSuite) TestStake() { } func (suite *KeeperTestSuite) TestMultipleStake() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -81,7 +83,7 @@ func (suite *KeeperTestSuite) TestStakeInAdvance() { suite.AdvanceEpoch() suite.AdvanceEpoch() - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Require().True(coinsEq(sdk.NewCoins(), suite.AllRewards(suite.addrs[0]))) suite.AdvanceEpoch() suite.Require().True(coinsEq(sdk.NewCoins(sdk.NewInt64Coin(denom3, 1000000)), suite.AllRewards(suite.addrs[0]))) @@ -202,7 +204,7 @@ func (suite *KeeperTestSuite) TestUnstakeNotAlwaysWithdraw() { // Unstaking from queued staking coins should not trigger // reward withdrawal. - suite.CreateRatioPlan(suite.addrs[4], map[string]string{denom1: "1"}, "0.1") + suite.CreateRatioPlan(suite.addrs[4], "1denom1", "0.1") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) suite.AdvanceEpoch() @@ -221,7 +223,7 @@ func (suite *KeeperTestSuite) TestUnstakeNotAlwaysWithdraw() { } func (suite *KeeperTestSuite) TestMultipleUnstake() { - suite.CreateFixedAmountPlan(suite.addrs[4], map[string]string{denom1: "1"}, map[string]int64{denom3: 1000000}) + suite.CreateFixedAmountPlan(suite.addrs[4], "1denom1", "1000000denom3") suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) @@ -318,6 +320,24 @@ func (suite *KeeperTestSuite) TestDelayedStakingGasFee() { suite.Require().Greater(gasConsumedWithStaking, gasConsumedNormal) } +func (suite *KeeperTestSuite) TestPruneStateWithoutRewards() { + for i := 0; i < 10; i++ { + suite.Stake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 1000000))) + suite.AdvanceEpoch() + } + + suite.Unstake(suite.addrs[0], sdk.NewCoins(sdk.NewInt64Coin(denom1, 10000000))) + + cnt := 0 + suite.keeper.IterateHistoricalRewards(suite.ctx, func(stakingCoinDenom string, epoch uint64, rewards types.HistoricalRewards) (stop bool) { + if stakingCoinDenom == denom1 { + cnt++ + } + return false + }) + suite.Require().Zero(cnt) +} + func (suite *KeeperTestSuite) TestReserveAndReleaseStakingCoins() { denom9 := "denom9" denom10 := "denom10" diff --git a/x/farming/types/farming.pb.go b/x/farming/types/farming.pb.go index f76dd2be..60b2900b 100644 --- a/x/farming/types/farming.pb.go +++ b/x/farming/types/farming.pb.go @@ -403,6 +403,7 @@ var xxx_messageInfo_TotalStakings proto.InternalMessageInfo // HistoricalRewards defines the cumulative unit rewards for a given staking coin denom and an epoch number. type HistoricalRewards struct { CumulativeUnitRewards github_com_cosmos_cosmos_sdk_types.DecCoins `protobuf:"bytes,1,rep,name=cumulative_unit_rewards,json=cumulativeUnitRewards,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.DecCoins" json:"cumulative_unit_rewards" yaml:"cumulative_unit_rewards"` + ReferenceCount uint32 `protobuf:"varint,2,opt,name=reference_count,json=referenceCount,proto3" json:"reference_count,omitempty" yaml:"reference_count"` } func (m *HistoricalRewards) Reset() { *m = HistoricalRewards{} } @@ -496,85 +497,87 @@ func init() { } var fileDescriptor_5b657e0809d9de86 = []byte{ - // 1248 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0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, 0xa2, 0xc7, 0x00, 0x5e, 0x70, 0x88, 0x87, 0x07, - 0xc4, 0xb1, 0x29, 0xc3, 0xf7, 0x79, 0x5c, 0x07, 0x53, 0x21, 0x62, 0x52, 0x07, 0xc5, 0xa4, 0xd9, - 0xe0, 0x4a, 0xfd, 0x19, 0x69, 0x1f, 0xbd, 0x83, 0x0a, 0x7b, 0x98, 0x0e, 0x23, 0xad, 0x20, 0x69, - 0x9c, 0x02, 0x6b, 0x58, 0x2b, 0x6a, 0x67, 0x5f, 0x6e, 0xec, 0x61, 0xba, 0x4b, 0xc8, 0x56, 0xea, - 0xc9, 0x33, 0x2d, 0xf1, 0xdd, 0x33, 0x2d, 0x61, 0xfc, 0x30, 0x07, 0x53, 0x26, 0xa6, 0x42, 0x45, - 0xb4, 0x00, 0x67, 0x5c, 0x27, 0x07, 0x38, 0x15, 0x6b, 0xc6, 0x75, 0x10, 0x82, 0x49, 0x1f, 0x77, - 0x89, 0xd0, 0x2f, 0x6d, 0x89, 0x77, 0xf4, 0x09, 0x4c, 0xf2, 0xfc, 0x42, 0x89, 0x85, 0x8a, 0x5e, - 0x9a, 0x7c, 0x5e, 0x4b, 0x1c, 0xaf, 0x39, 0xe8, 0x11, 0x4b, 0x78, 0xa3, 0x5b, 0x70, 0x25, 0x56, - 0xaa, 0x17, 0x04, 0x9e, 0x8d, 0x1d, 0x27, 0x24, 0x94, 0x8a, 0xb2, 0xd3, 0xa6, 0x36, 0x8c, 0xb4, - 0x8b, 0xff, 0xd6, 0x73, 0xdc, 0xcb, 0xb0, 0x90, 0x32, 0x37, 0x82, 0xc0, 0xdb, 0x96, 0x46, 0x74, - 0x13, 0x2e, 0x33, 0xd1, 0x52, 0xf2, 0xfc, 0xc4, 0x88, 0xe7, 0x05, 0x62, 0x61, 0x18, 0x69, 0x79, - 0x89, 0x38, 0xc1, 0xc9, 0xb0, 0xd0, 0x98, 0x35, 0x06, 0xfc, 0x11, 0xc0, 0x95, 0x58, 0x3f, 0xde, - 0x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0xa3, 0xdf, 0xf2, 0xdc, 0x36, 0xda, 0x80, 0x4b, 0x63, 0x9e, 0x56, 0xfd, 0xf3, 0xed, 0x66, 0x2d, - 0x3b, 0x93, 0x5f, 0x3e, 0x3c, 0xd2, 0x17, 0x47, 0xae, 0xf2, 0x2f, 0x2f, 0x9f, 0x7c, 0xf2, 0x53, - 0x21, 0xb1, 0x31, 0x80, 0x19, 0x75, 0x5d, 0x0b, 0x5a, 0xd7, 0xe0, 0xea, 0x76, 0xb5, 0x6a, 0xd5, - 0xf6, 0xf7, 0x25, 0xc6, 0x66, 0xc5, 0x36, 0xef, 0x34, 0x6b, 0xfb, 0xd9, 0x44, 0x7e, 0xed, 0xf0, - 0x48, 0x47, 0x63, 0xbe, 0x9b, 0x15, 0x73, 0xc0, 0x08, 0x7d, 0x2b, 0xa4, 0x72, 0x55, 0x85, 0x80, - 0xb7, 0x42, 0x2a, 0x57, 0x45, 0x88, 0x4c, 0x6d, 0xee, 0xbd, 0x78, 0x5d, 0x00, 0x2f, 0x5f, 0x17, - 0xc0, 0x5f, 0xaf, 0x0b, 0xe0, 0xe9, 0x71, 0x21, 0xf1, 0xf2, 0xb8, 0x90, 0xf8, 0xe3, 0xb8, 0x90, - 0xf8, 0xf2, 0xca, 0x98, 0xca, 0x13, 0x7e, 0xf4, 0x1f, 0x8d, 0xde, 0x84, 0xe0, 0xad, 0x59, 0x71, - 0x99, 0x6d, 0xfe, 0x13, 0x00, 0x00, 0xff, 0xff, 0xba, 0xcd, 0x71, 0x31, 0x15, 0x0c, 0x00, 0x00, + // 1277 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x57, 0x4d, 0x6f, 0xdb, 0x46, + 0x13, 0xd6, 0x3a, 0x8a, 0x2d, 0xad, 0x5e, 0xdb, 0xf2, 0xfa, 0x23, 0xb2, 0x92, 0x88, 0x02, 0x81, + 0xf7, 0x85, 0xe0, 0x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xbf, 0xed, 0xb9, + 0x36, 0xda, 0x80, 0x4b, 0x63, 0x9e, 0xe6, 0xde, 0xa7, 0xdb, 0xad, 0x46, 0x76, 0x26, 0xbf, 0x7c, + 0x74, 0x5c, 0x5c, 0x1c, 0xb9, 0xca, 0xab, 0x62, 0x3e, 0xf9, 0xf4, 0xc7, 0x42, 0x62, 0x63, 0x00, + 0x33, 0xea, 0xcc, 0x17, 0x65, 0xdd, 0x80, 0xab, 0xdb, 0xf5, 0xba, 0xd9, 0xd8, 0xdf, 0x97, 0x18, + 0x9b, 0x55, 0xcb, 0xb8, 0xd7, 0x6a, 0xec, 0x67, 0x13, 0xf9, 0xb5, 0xa3, 0xe3, 0x22, 0x1a, 0xf3, + 0xdd, 0xac, 0x1a, 0x03, 0x46, 0xe8, 0x3b, 0x21, 0xd5, 0xeb, 0x2a, 0x04, 0xbc, 0x13, 0x52, 0xbd, + 0x2e, 0x42, 0x64, 0x6a, 0x63, 0xf7, 0xe5, 0x9b, 0x02, 0x78, 0xf5, 0xa6, 0x00, 0xfe, 0x78, 0x53, + 0x00, 0xcf, 0x4e, 0x0a, 0x89, 0x57, 0x27, 0x85, 0xc4, 0x6f, 0x27, 0x85, 0xc4, 0xe7, 0xd7, 0xc6, + 0x58, 0x9e, 0xf0, 0x6f, 0xe1, 0xf1, 0xe8, 0x49, 0x10, 0xde, 0x9e, 0x15, 0x27, 0xe2, 0xe6, 0x5f, + 0x01, 0x00, 0x00, 0xff, 0xff, 0xdb, 0x20, 0x89, 0xca, 0x5a, 0x0c, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -967,6 +970,11 @@ func (m *HistoricalRewards) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.ReferenceCount != 0 { + i = encodeVarintFarming(dAtA, i, uint64(m.ReferenceCount)) + i-- + dAtA[i] = 0x10 + } if len(m.CumulativeUnitRewards) > 0 { for iNdEx := len(m.CumulativeUnitRewards) - 1; iNdEx >= 0; iNdEx-- { { @@ -1189,6 +1197,9 @@ func (m *HistoricalRewards) Size() (n int) { n += 1 + l + sovFarming(uint64(l)) } } + if m.ReferenceCount != 0 { + n += 1 + sovFarming(uint64(m.ReferenceCount)) + } return n } @@ -2315,6 +2326,25 @@ func (m *HistoricalRewards) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ReferenceCount", wireType) + } + m.ReferenceCount = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFarming + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.ReferenceCount |= uint32(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipFarming(dAtA[iNdEx:]) diff --git a/x/farming/types/keys_test.go b/x/farming/types/keys_test.go index 7e92f11b..663d1662 100644 --- a/x/farming/types/keys_test.go +++ b/x/farming/types/keys_test.go @@ -254,6 +254,11 @@ func (s *keysTestSuite) TestGetHistoricalRewardsKey() { } } +func (s *keysTestSuite) TestGetHistoricalRewardsPrefix() { + s.Require().Equal([]byte{0x31, 0x5, 0x73, 0x74, 0x61, 0x6b, 0x65}, types.GetHistoricalRewardsPrefix(sdk.DefaultBondDenom)) + s.Require().Equal([]byte{0x31, 0x6, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x31}, types.GetHistoricalRewardsPrefix("denom1")) +} + func (s *keysTestSuite) TestGetCurrentEpochKey() { // key0 stakingCoinDenom0 := ""