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

feat: prune abandoned state #185

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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: 1 addition & 0 deletions client/docs/statik/statik.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/tendermint/farming/v1beta1/farming.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/farming/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 13 additions & 13 deletions x/farming/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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)
}

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

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

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

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

Expand Down Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions x/farming/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

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

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