Skip to content

Commit

Permalink
split disable inflation upgrade and the upgrade check
Browse files Browse the repository at this point in the history
  • Loading branch information
DracoLi committed Sep 14, 2023
1 parent d85039c commit 7817cb1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
5 changes: 4 additions & 1 deletion x/community/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
25 changes: 15 additions & 10 deletions x/community/keeper/incentives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
59 changes: 28 additions & 31 deletions x/community/keeper/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 7817cb1

Please sign in to comment.