Skip to content

Commit

Permalink
[Hotfix] treasury empty epoch data (#445)
Browse files Browse the repository at this point in the history
* prevent division by zero, even though no epoch data is stored

* add test case for empty epoch data
  • Loading branch information
yys authored Feb 16, 2021
1 parent 2ddf402 commit 01571de
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
40 changes: 40 additions & 0 deletions x/treasury/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,43 @@ func TestUpdate(t *testing.T) {
newRewardWeight := input.TreasuryKeeper.GetRewardWeight(input.Ctx)
require.Equal(t, rewardWeight.Add(input.TreasuryKeeper.RewardPolicy(input.Ctx).ChangeRateMax), newRewardWeight)
}

func TestEmptyIndicator(t *testing.T) {
input := keeper.CreateTestInput(t)

windowProbation := input.TreasuryKeeper.WindowProbation(input.Ctx)

// Set total staked luna to prevent divide by zero error when computing TRL
bondedModuleAcc := input.SupplyKeeper.GetModuleAccount(input.Ctx, stakingtypes.BondedPoolName)
err := bondedModuleAcc.SetCoins(sdk.NewCoins(sdk.NewInt64Coin(core.MicroLunaDenom, 100000000000)))
require.NoError(t, err)
input.SupplyKeeper.SetModuleAccount(input.Ctx, bondedModuleAcc)

targetEpoch := windowProbation + 1
for epoch := int64(0); epoch < targetEpoch; epoch++ {
// skip last epoch end blocker
// to make indicators empty
if epoch == targetEpoch-1 {
continue
}

input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerWeek*epoch - 1)
EndBlocker(input.Ctx, input.TreasuryKeeper)
}

// Must result in the same output as normal zero tax & mining rewards
// load old tax rate & reward weight
taxRate := input.TreasuryKeeper.GetTaxRate(input.Ctx)
rewardWeight := input.TreasuryKeeper.GetRewardWeight(input.Ctx)

input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerWeek*targetEpoch - 1)
EndBlocker(input.Ctx, input.TreasuryKeeper)

// zero tax proceeds will increase tax rate with change max amount
newTaxRate := input.TreasuryKeeper.GetTaxRate(input.Ctx)
require.Equal(t, taxRate.Add(input.TreasuryKeeper.TaxPolicy(input.Ctx).ChangeRateMax), newTaxRate)

// zero mining rewards will increase reward weight with change max amount
newRewardWeight := input.TreasuryKeeper.GetRewardWeight(input.Ctx)
require.Equal(t, rewardWeight.Add(input.TreasuryKeeper.RewardPolicy(input.Ctx).ChangeRateMax), newRewardWeight)
}
2 changes: 1 addition & 1 deletion x/treasury/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (k Keeper) GetTSL(ctx sdk.Context, epoch int64) (res sdk.Int) {
bz := store.Get(types.GetTSLKey(epoch))

if bz == nil {
res = sdk.ZeroInt()
res = sdk.OneInt()
} else {
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
}
Expand Down
2 changes: 1 addition & 1 deletion x/treasury/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestIndicatorGetterSetter(t *testing.T) {
for e := int64(0); e < 10; e++ {
require.Equal(t, sdk.ZeroDec(), input.TreasuryKeeper.GetTR(input.Ctx, e))
require.Equal(t, sdk.ZeroDec(), input.TreasuryKeeper.GetSR(input.Ctx, e))
require.Equal(t, sdk.ZeroInt(), input.TreasuryKeeper.GetTSL(input.Ctx, e))
require.Equal(t, sdk.OneInt(), input.TreasuryKeeper.GetTSL(input.Ctx, e))
}
}

Expand Down

0 comments on commit 01571de

Please sign in to comment.