From 93a57bc330cc3f3101064f957fba0ba371f38852 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 21 Mar 2022 15:27:22 +0800 Subject: [PATCH] feemarket: migration (#1002) * increase feemarket's consensus version and add migration handler Closes: #1001 * unit test * fix linter --- x/feemarket/keeper/migrations.go | 20 ++++++++++++++++++++ x/feemarket/keeper/migrations_test.go | 18 ++++++++++++++++++ x/feemarket/module.go | 8 ++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 x/feemarket/keeper/migrations_test.go diff --git a/x/feemarket/keeper/migrations.go b/x/feemarket/keeper/migrations.go index 3be1ada6..bfcd294b 100644 --- a/x/feemarket/keeper/migrations.go +++ b/x/feemarket/keeper/migrations.go @@ -1,5 +1,14 @@ package keeper +import ( + "math/big" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// KeyPrefixBaseFeeV1 is the base fee key prefix used in version 1 +var KeyPrefixBaseFeeV1 = []byte{2} + // Migrator is a struct for handling in-place store migrations. type Migrator struct { keeper Keeper @@ -11,3 +20,14 @@ func NewMigrator(keeper Keeper) Migrator { keeper: keeper, } } + +func (m Migrator) Migrate1to2(ctx sdk.Context) error { + store := ctx.KVStore(m.keeper.storeKey) + bz := store.Get(KeyPrefixBaseFeeV1) + if len(bz) > 0 { + baseFee := new(big.Int).SetBytes(bz) + m.keeper.SetBaseFee(ctx, baseFee) + } + store.Delete(KeyPrefixBaseFeeV1) + return nil +} diff --git a/x/feemarket/keeper/migrations_test.go b/x/feemarket/keeper/migrations_test.go new file mode 100644 index 00000000..768c0b42 --- /dev/null +++ b/x/feemarket/keeper/migrations_test.go @@ -0,0 +1,18 @@ +package keeper_test + +import ( + "math/big" + + "github.com/tharsis/ethermint/x/feemarket/keeper" +) + +func (suite *KeeperTestSuite) TestMigration1To2() { + suite.SetupTest() + storeKey := suite.app.GetKey("feemarket") + store := suite.ctx.KVStore(storeKey) + baseFee := big.NewInt(1000) + store.Set(keeper.KeyPrefixBaseFeeV1, baseFee.Bytes()) + m := keeper.NewMigrator(suite.app.FeeMarketKeeper) + suite.Require().NoError(m.Migrate1to2(suite.ctx)) + suite.Require().Equal(baseFee, suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx)) +} diff --git a/x/feemarket/module.go b/x/feemarket/module.go index 3abf7c76..71e0c6d0 100644 --- a/x/feemarket/module.go +++ b/x/feemarket/module.go @@ -44,7 +44,7 @@ func (AppModuleBasic) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) { // ConsensusVersion returns the consensus state-breaking version for the module. func (AppModuleBasic) ConsensusVersion() uint64 { - return 1 + return 2 } // DefaultGenesis returns default genesis state as raw bytes for the fee market @@ -117,7 +117,11 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - _ = keeper.NewMigrator(am.keeper) + m := keeper.NewMigrator(am.keeper) + err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2) + if err != nil { + panic(err) + } } // Route returns the message routing key for the fee market module.