From 7817cb13dc389e58012b77abcb3084dc29f832ad Mon Sep 17 00:00:00 2001 From: Draco Date: Thu, 14 Sep 2023 16:19:49 -0400 Subject: [PATCH] split disable inflation upgrade and the upgrade check --- x/community/abci.go | 5 ++- x/community/keeper/incentives.go | 25 +++++++----- x/community/keeper/incentives_test.go | 59 +++++++++++++-------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/x/community/abci.go b/x/community/abci.go index 19c13f7cae..1e51386dbb 100644 --- a/x/community/abci.go +++ b/x/community/abci.go @@ -13,7 +13,10 @@ import ( func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - k.DisableInflationAfterUpgrade(ctx) + if k.ShouldStartDisableInflationUpgrade(ctx) { + k.StartDisableInflationUpgrade(ctx) + } + if err := k.PayCommunityRewards(ctx); err != nil { panic(err) } diff --git a/x/community/keeper/incentives.go b/x/community/keeper/incentives.go index 62b251bc8e..08506a0739 100644 --- a/x/community/keeper/incentives.go +++ b/x/community/keeper/incentives.go @@ -9,30 +9,37 @@ func (k Keeper) PayCommunityRewards(ctx sdk.Context) error { return nil } -// DisableInflationAfterUpgrade disables inflation on or after the upgrade time -func (k Keeper) DisableInflationAfterUpgrade(ctx sdk.Context) { - logger := k.Logger(ctx) - +// ShouldStartDisableInflationUpgrade returns true if the disable inflation upgrade should be started +func (k Keeper) ShouldStartDisableInflationUpgrade(ctx sdk.Context) bool { params, found := k.GetParams(ctx) if !found { - return + return false } // skip if we have already upgraded - previousBlockTime is first set on upgrade _, found = k.GetPreviousBlockTime(ctx) if found { - return + return false } blockTime := ctx.BlockTime() upgradeTime := params.UpgradeTimeDisableInflation // a vanilla kava chain should disable inflation on the first block if `upgradeTime` is not set. - // thus, we only skip upgrade here if `upgradeTime` is set and `blockTime` is before `upgradeTime`. + // thus, we don't upgrade if `upgradeTime` is set and `blockTime` is before `upgradeTime`. if !upgradeTime.IsZero() && blockTime.Before(upgradeTime) { - return + return false } + return true +} + +// StartDisableInflationUpgrade disables x/mint and x/kavadist inflation +func (k Keeper) StartDisableInflationUpgrade(ctx sdk.Context) { + logger := k.Logger(ctx) + + blockTime := ctx.BlockTime() + logger.Info("disable inflation upgrade started") k.SetPreviousBlockTime(ctx, blockTime) @@ -53,6 +60,4 @@ func (k Keeper) DisableInflationAfterUpgrade(ctx sdk.Context) { // todo: consolidate community funds (transfer from kavadist and community pool) logger.Info("disable inflation upgrade finished successfully!") - - return } diff --git a/x/community/keeper/incentives_test.go b/x/community/keeper/incentives_test.go index 18a4cd79d7..e00cfd1634 100644 --- a/x/community/keeper/incentives_test.go +++ b/x/community/keeper/incentives_test.go @@ -50,11 +50,11 @@ func TestIncentivesTestSuite(t *testing.T) { suite.Run(t, new(IncentivesTestSuite)) } -func (suite *IncentivesTestSuite) TestDisableInflationAfterUpgrade() { - isUpgraded := func() bool { - _, found := suite.Keeper.GetPreviousBlockTime(suite.Ctx) - return found +func (suite *IncentivesTestSuite) TestShouldStartDisableInflationUpgrade() { + shouldUpgrade := func() bool { + return suite.Keeper.ShouldStartDisableInflationUpgrade(suite.Ctx) } + setUpgradeTimeFromNow := func(t time.Duration) { suite.Keeper.SetParams( suite.Ctx, @@ -71,16 +71,14 @@ func (suite *IncentivesTestSuite) TestDisableInflationAfterUpgrade() { _, found := suite.Keeper.GetParams(suite.Ctx) suite.False(found) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.False(isUpgraded()) + suite.False(shouldUpgrade()) }) suite.Run("skips upgrade if upgrade time is set in the future", func() { suite.SetupTest() setUpgradeTimeFromNow(1 * time.Hour) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.False(isUpgraded()) + suite.False(shouldUpgrade()) }) suite.Run("upgrades if params are set to the default", func() { @@ -90,40 +88,39 @@ func (suite *IncentivesTestSuite) TestDisableInflationAfterUpgrade() { param, found := suite.Keeper.GetParams(suite.Ctx) suite.True(found) suite.Equal(time.Time{}, param.UpgradeTimeDisableInflation) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.True(isUpgraded()) + suite.True(shouldUpgrade()) }) suite.Run("upgrades if blockTime is at or after upgrade time", func() { suite.SetupTest() - setUpgradeTimeFromNow(-2 * time.Minute) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.True(isUpgraded()) - setUpgradeTimeFromNow(0) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.True(isUpgraded()) + suite.True(shouldUpgrade()) + setUpgradeTimeFromNow(-2 * time.Minute) + suite.True(shouldUpgrade()) }) suite.Run("skips upgrade if already upgraded", func() { suite.SetupTest() setUpgradeTimeFromNow(-2 * time.Minute) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.True(isUpgraded()) - - // reset kavadist param active to true after upgrade - newParams := suite.App.GetKavadistKeeper().GetParams(suite.Ctx) - suite.False(newParams.Active) - newParams.Active = true - suite.App.GetKavadistKeeper().SetParams(suite.Ctx, newParams) - - // kavadist should be active since upgrade is not ran - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) - suite.True(isUpgraded()) - suite.True(suite.App.GetKavadistKeeper().GetParams(suite.Ctx).Active) + suite.True(shouldUpgrade()) + suite.Keeper.StartDisableInflationUpgrade(suite.Ctx) + suite.False(shouldUpgrade()) }) +} + +func (suite *IncentivesTestSuite) TestStartDisableInflationUpgrade() { + isUpgraded := func() bool { + _, found := suite.Keeper.GetPreviousBlockTime(suite.Ctx) + return found + } + setUpgradeTimeFromNow := func(t time.Duration) { + suite.Keeper.SetParams( + suite.Ctx, + types.Params{UpgradeTimeDisableInflation: suite.Ctx.BlockTime().Add(t)}, + ) + } suite.Run("upgrade should set mint and kavadist inflation to 0", func() { suite.SetupTest() @@ -134,7 +131,7 @@ func (suite *IncentivesTestSuite) TestDisableInflationAfterUpgrade() { suite.True(kavadistParams.Active) setUpgradeTimeFromNow(-2 * time.Minute) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) + suite.Keeper.StartDisableInflationUpgrade(suite.Ctx) suite.True(isUpgraded()) mintParams = suite.App.GetMintKeeper().GetParams(suite.Ctx) @@ -149,7 +146,7 @@ func (suite *IncentivesTestSuite) TestDisableInflationAfterUpgrade() { suite.SetupTest() setUpgradeTimeFromNow(-2 * time.Minute) - suite.Keeper.DisableInflationAfterUpgrade(suite.Ctx) + suite.Keeper.StartDisableInflationUpgrade(suite.Ctx) suite.True(isUpgraded()) prevBlockTime, found := suite.Keeper.GetPreviousBlockTime(suite.Ctx) suite.True(found)