Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
imp: migrate JSON for feemarket (#1021)
Browse files Browse the repository at this point in the history
* imp: migrate JSON for feemarket

* migrate JSON

* types

* fix

* fix unmarshal JSON error

* changelog

* fix lint

* fix test
  • Loading branch information
fedekunze authored Mar 30, 2022
1 parent 285c7c4 commit 3c8a95b
Show file tree
Hide file tree
Showing 11 changed files with 1,029 additions and 26 deletions.
22 changes: 17 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (rpc) [tharsis#990](https://github.com/tharsis/ethermint/pull/990) Calculate reward values from all `MsgEthereumTx` from a block in `eth_feeHistory`.
* (ante) [tharsis#991](https://github.com/tharsis/ethermint/pull/991) Set an upper bound to gasWanted to prevent DoS attack.
* (feemarket) [tharsis#1021](https://github.com/tharsis/ethermint/pull/1021) Fix fee market migration.

## [v0.12.1] - 2022-03-29

### Bug Fixes

* (evm) [tharsis#1016](https://github.com/tharsis/ethermint/pull/1016) Update validate basic check for storage state.

## [v0.12.0] - 2022-03-24

### Bug Fixes

* (rpc) [tharsis#1012](https://github.com/tharsis/ethermint/pull/1012) fix the tx hash in filter entries created by `eth_newPendingTransactionFilter`.
* (rpc) [tharsis#1006](https://github.com/tharsis/ethermint/pull/1006) Use `string` as the parameters type to correct ambiguous results.
* (ante) [tharsis#1004](https://github.com/tharsis/ethermint/pull/1004) make MaxTxGasWanted configurable.
* (rpc) [tharsis#1012](https://github.com/tharsis/ethermint/pull/1012) fix the tx hash in filter entries created by `eth_newPendingTransactionFilter`.
* (ante) [tharsis#1004](https://github.com/tharsis/ethermint/pull/1004) Make `MaxTxGasWanted` configurable.
* (ante) [tharsis#991](https://github.com/tharsis/ethermint/pull/991) Set an upper bound to gasWanted to prevent DoS attack.
* (rpc) [tharsis#990](https://github.com/tharsis/ethermint/pull/990) Calculate reward values from all `MsgEthereumTx` from a block in `eth_feeHistory`.

## [v0.11.0] - 2022-03-06

Expand All @@ -58,7 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

- (rpc) [tharsis#979](https://github.com/tharsis/ethermint/pull/979) Add configurable timeouts to http server
- (rpc) [tharsis#979](https://github.com/tharsis/ethermint/pull/979) Add configurable timeouts to http server
- (rpc) [tharsis#988](https://github.com/tharsis/ethermint/pull/988) json-rpc server always use local rpc client

## [v0.10.1] - 2022-03-04
Expand Down
3 changes: 0 additions & 3 deletions client/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func initTestnetFiles(
genBalIterator banktypes.GenesisBalancesIterator,
args initArgs,
) error {

if args.chainID == "" {
args.chainID = fmt.Sprintf("ethermint_%d-1", tmrand.Int63n(9999999999999)+1)
}
Expand Down Expand Up @@ -377,7 +376,6 @@ func initGenFiles(
genFiles []string,
numValidators int,
) error {

appGenState := mbm.DefaultGenesis(clientCtx.Codec)
// set the accounts in the genesis state
var authGenState authtypes.GenesisState
Expand Down Expand Up @@ -453,7 +451,6 @@ func collectGenFiles(
nodeIDs []string, valPubKeys []cryptotypes.PubKey, numValidators int,
outputDir, nodeDirPrefix, nodeDaemonHome string, genBalIterator banktypes.GenesisBalancesIterator,
) error {

var appState json.RawMessage
genTime := tmtime.Now()

Expand Down
3 changes: 2 additions & 1 deletion rpc/ethereum/backend/feebackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (e *EVMBackend) processBlock(
ethBlock *map[string]interface{},
rewardPercentiles []float64,
tendermintBlockResult *tmrpctypes.ResultBlockResults,
targetOneFeeHistory *rpctypes.OneFeeHistory) error {
targetOneFeeHistory *rpctypes.OneFeeHistory,
) error {
blockHeight := tendermintBlock.Block.Height
blockBaseFee, err := e.BaseFee(blockHeight)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion tests/importer/chain_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (cc *ChainContext) Finalize(
// consensus rules that happen at finalization (e.g. block rewards).
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) FinalizeAndAssemble(_ ethcons.ChainHeaderReader, _ *ethtypes.Header, _ *ethstate.StateDB, _ []*ethtypes.Transaction,
_ []*ethtypes.Header, _ []*ethtypes.Receipt) (*ethtypes.Block, error) {
_ []*ethtypes.Header, _ []*ethtypes.Receipt,
) (*ethtypes.Block, error) {
return nil, nil
}

Expand Down
17 changes: 4 additions & 13 deletions x/feemarket/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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}
v010 "github.com/tharsis/ethermint/x/feemarket/migrations/v010"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
Expand All @@ -21,13 +18,7 @@ func NewMigrator(keeper Keeper) Migrator {
}
}

// Migrate1to2 migrates the store from consensus version v1 to v2
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
return v010.MigrateStore(ctx, &m.keeper.paramSpace, m.keeper.storeKey)
}
6 changes: 4 additions & 2 deletions x/feemarket/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"math/big"

"github.com/tharsis/ethermint/x/feemarket/keeper"
v010 "github.com/tharsis/ethermint/x/feemarket/migrations/v010"
)

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())
store.Set(v010.KeyPrefixBaseFeeV1, baseFee.Bytes())
m := keeper.NewMigrator(suite.app.FeeMarketKeeper)
suite.Require().NoError(m.Migrate1to2(suite.ctx))
err := m.Migrate1to2(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(baseFee, suite.app.FeeMarketKeeper.GetBaseFee(suite.ctx))
}
74 changes: 74 additions & 0 deletions x/feemarket/migrations/v010/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package v010

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
v09types "github.com/tharsis/ethermint/x/feemarket/migrations/v09/types"
"github.com/tharsis/ethermint/x/feemarket/types"
)

// KeyPrefixBaseFeeV1 is the base fee key prefix used in version 1
var KeyPrefixBaseFeeV1 = []byte{2}

// MigrateStore migrates the BaseFee value from the store to the params for
// In-Place Store migration logic.
func MigrateStore(ctx sdk.Context, paramstore *paramtypes.Subspace, storeKey sdk.StoreKey) error {
baseFee := types.DefaultParams().BaseFee

store := ctx.KVStore(storeKey)

if !paramstore.HasKeyTable() {
ps := paramstore.WithKeyTable(types.ParamKeyTable())
paramstore = &ps
}

switch {
case store.Has(KeyPrefixBaseFeeV1):
bz := store.Get(KeyPrefixBaseFeeV1)
baseFee = sdk.NewIntFromBigInt(new(big.Int).SetBytes(bz))
case paramstore.Has(ctx, types.ParamStoreKeyNoBaseFee):
paramstore.GetIfExists(ctx, types.ParamStoreKeyBaseFee, &baseFee)
}

var (
noBaseFee bool
baseFeeChangeDenom, elasticityMultiplier uint32
enableHeight int64
)

paramstore.GetIfExists(ctx, types.ParamStoreKeyNoBaseFee, &noBaseFee)
paramstore.GetIfExists(ctx, types.ParamStoreKeyBaseFeeChangeDenominator, &baseFeeChangeDenom)
paramstore.GetIfExists(ctx, types.ParamStoreKeyElasticityMultiplier, &elasticityMultiplier)
paramstore.GetIfExists(ctx, types.ParamStoreKeyEnableHeight, &enableHeight)

params := types.Params{
NoBaseFee: noBaseFee,
BaseFeeChangeDenominator: baseFeeChangeDenom,
ElasticityMultiplier: elasticityMultiplier,
BaseFee: baseFee,
EnableHeight: enableHeight,
}

paramstore.SetParamSet(ctx, &params)
store.Delete(KeyPrefixBaseFeeV1)
return nil
}

// MigrateJSON accepts exported v0.9 x/feemarket genesis state and migrates it to
// v0.10 x/feemarket genesis state. The migration includes:
// - Migrate BaseFee to Params
func MigrateJSON(oldState v09types.GenesisState) types.GenesisState {
return types.GenesisState{
Params: types.Params{
NoBaseFee: oldState.Params.NoBaseFee,
BaseFeeChangeDenominator: oldState.Params.BaseFeeChangeDenominator,
ElasticityMultiplier: oldState.Params.ElasticityMultiplier,
EnableHeight: oldState.Params.EnableHeight,
BaseFee: oldState.BaseFee,
},
BlockGas: oldState.BlockGas,
}
}
46 changes: 46 additions & 0 deletions x/feemarket/migrations/v010/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v010_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/tharsis/ethermint/encoding"

"github.com/tharsis/ethermint/app"
feemarketkeeper "github.com/tharsis/ethermint/x/feemarket/keeper"
v010 "github.com/tharsis/ethermint/x/feemarket/migrations/v010"
"github.com/tharsis/ethermint/x/feemarket/types"
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"
)

func TestMigrateStore(t *testing.T) {
encCfg := encoding.MakeConfig(app.ModuleBasics)
feemarketKey := sdk.NewKVStoreKey(feemarkettypes.StoreKey)
tFeeMarketKey := sdk.NewTransientStoreKey(fmt.Sprintf("%s_test", feemarkettypes.StoreKey))
ctx := testutil.DefaultContext(feemarketKey, tFeeMarketKey)
paramstore := paramtypes.NewSubspace(
encCfg.Marshaler, encCfg.Amino, feemarketKey, tFeeMarketKey, "feemarket",
)
fmKeeper := feemarketkeeper.NewKeeper(encCfg.Marshaler, feemarketKey, paramstore)
fmKeeper.SetParams(ctx, types.DefaultParams())
require.True(t, paramstore.HasKeyTable())

// check that the fee market is not nil
err := v010.MigrateStore(ctx, &paramstore, feemarketKey)
require.NoError(t, err)
require.False(t, ctx.KVStore(feemarketKey).Has(v010.KeyPrefixBaseFeeV1))

params := fmKeeper.GetParams(ctx)
require.False(t, params.BaseFee.IsNil())

baseFee := fmKeeper.GetBaseFee(ctx)
require.NotNil(t, baseFee)

require.Equal(t, baseFee.Int64(), params.BaseFee.Int64())
}
Loading

0 comments on commit 3c8a95b

Please sign in to comment.