feemarket: migration (#1002)

* increase feemarket's consensus version and add migration handler

Closes: #1001

* unit test

* fix linter
This commit is contained in:
yihuang
2022-03-21 08:27:22 +01:00
committed by GitHub
parent edf456985b
commit 93a57bc330
3 changed files with 44 additions and 2 deletions
+20
View File
@@ -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
}
+18
View File
@@ -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))
}