Skip to content

Commit

Permalink
Merge pull request #20 from bianjieai/dreamer/add-migration
Browse files Browse the repository at this point in the history
add migration function for evm&feemarket modules
  • Loading branch information
towerkyoto authored Jul 19, 2023
2 parents 1d6b3c3 + 7dd4d0b commit 9a45f8c
Show file tree
Hide file tree
Showing 23 changed files with 10,578 additions and 6 deletions.
12 changes: 12 additions & 0 deletions x/evm/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/evmos/ethermint/x/evm/migrations/v2"
v3 "github.com/evmos/ethermint/x/evm/migrations/v3"
v4 "github.com/evmos/ethermint/x/evm/migrations/v4"
v5 "github.com/evmos/ethermint/x/evm/migrations/v5"
"github.com/evmos/ethermint/x/evm/types"
Expand All @@ -36,6 +38,16 @@ func NewMigrator(keeper Keeper, legacySubspace types.Subspace) Migrator {
}
}

// Migrate1to2 migrates the store from consensus version 1 to 2
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.legacySubspace)
}

// Migrate2to3 migrates the store from consensus version 2 to 3
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.legacySubspace)
}

// Migrate3to4 migrates the store from consensus version 3 to 4
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc)
Expand Down
24 changes: 24 additions & 0 deletions x/evm/migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v2

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/evmos/ethermint/x/evm/types"
)

// MigrateStore sets the default AllowUnprotectedTxs parameter.
func MigrateStore(ctx sdk.Context, legacySubspace types.Subspace) error {
paramstore, ok := legacySubspace.(*paramtypes.Subspace)
if !ok {
return fmt.Errorf("invalid legacySubspace type: %T", paramstore)
}
if !paramstore.HasKeyTable() {
ps := paramstore.WithKeyTable(types.ParamKeyTable())
paramstore = &ps
}
// add RejectUnprotected
paramstore.Set(ctx, types.ParamStoreKeyAllowUnprotectedTxs, types.DefaultAllowUnprotectedTxs)
return nil
}
47 changes: 47 additions & 0 deletions x/evm/migrations/v2/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package v2_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/evmos/ethermint/encoding"

"github.com/evmos/ethermint/app"
v2 "github.com/evmos/ethermint/x/evm/migrations/v2"
v2types "github.com/evmos/ethermint/x/evm/migrations/v2/types"
"github.com/evmos/ethermint/x/evm/types"
)

func TestMigrateStore(t *testing.T) {
encCfg := encoding.MakeConfig(app.ModuleBasics)
feemarketKey := sdk.NewKVStoreKey(types.StoreKey)
tFeeMarketKey := sdk.NewTransientStoreKey(fmt.Sprintf("%s_test", types.StoreKey))
ctx := testutil.DefaultContext(feemarketKey, tFeeMarketKey)
paramstore := paramtypes.NewSubspace(
encCfg.Codec, encCfg.Amino, feemarketKey, tFeeMarketKey, "evm",
).WithKeyTable(v2types.ParamKeyTable())

params := v2types.DefaultParams()
paramstore.SetParamSet(ctx, &params)

require.Panics(t, func() {
var result bool
paramstore.Get(ctx, types.ParamStoreKeyAllowUnprotectedTxs, &result)
})

paramstore = paramtypes.NewSubspace(
encCfg.Codec, encCfg.Amino, feemarketKey, tFeeMarketKey, "evm",
).WithKeyTable(types.ParamKeyTable())
err := v2.MigrateStore(ctx, &paramstore)
require.NoError(t, err)

var result bool
paramstore.Get(ctx, types.ParamStoreKeyAllowUnprotectedTxs, &result)
require.Equal(t, types.DefaultAllowUnprotectedTxs, result)
}
165 changes: 165 additions & 0 deletions x/evm/migrations/v2/types/chain_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package types

import (
"math/big"
"strings"

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

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"

"github.com/evmos/ethermint/x/evm/types"
)

// EthereumConfig returns an Ethereum ChainConfig for EVM state transitions.
// All the negative or nil values are converted to nil
func (cc ChainConfig) EthereumConfig(chainID *big.Int) *params.ChainConfig {
return &params.ChainConfig{
ChainID: chainID,
HomesteadBlock: getBlockValue(cc.HomesteadBlock),
DAOForkBlock: getBlockValue(cc.DAOForkBlock),
DAOForkSupport: cc.DAOForkSupport,
EIP150Block: getBlockValue(cc.EIP150Block),
EIP150Hash: common.HexToHash(cc.EIP150Hash),
EIP155Block: getBlockValue(cc.EIP155Block),
EIP158Block: getBlockValue(cc.EIP158Block),
ByzantiumBlock: getBlockValue(cc.ByzantiumBlock),
ConstantinopleBlock: getBlockValue(cc.ConstantinopleBlock),
PetersburgBlock: getBlockValue(cc.PetersburgBlock),
IstanbulBlock: getBlockValue(cc.IstanbulBlock),
MuirGlacierBlock: getBlockValue(cc.MuirGlacierBlock),
BerlinBlock: getBlockValue(cc.BerlinBlock),
LondonBlock: getBlockValue(cc.LondonBlock),
ArrowGlacierBlock: getBlockValue(cc.ArrowGlacierBlock),
MergeNetsplitBlock: getBlockValue(cc.MergeForkBlock),
TerminalTotalDifficulty: nil,
Ethash: nil,
Clique: nil,
}
}

// DefaultChainConfig returns default evm parameters.
func DefaultChainConfig() ChainConfig {
homesteadBlock := sdk.ZeroInt()
daoForkBlock := sdk.ZeroInt()
eip150Block := sdk.ZeroInt()
eip155Block := sdk.ZeroInt()
eip158Block := sdk.ZeroInt()
byzantiumBlock := sdk.ZeroInt()
constantinopleBlock := sdk.ZeroInt()
petersburgBlock := sdk.ZeroInt()
istanbulBlock := sdk.ZeroInt()
muirGlacierBlock := sdk.ZeroInt()
berlinBlock := sdk.ZeroInt()
londonBlock := sdk.ZeroInt()
arrowGlacierBlock := sdk.ZeroInt()
mergeForkBlock := sdk.ZeroInt()

return ChainConfig{
HomesteadBlock: &homesteadBlock,
DAOForkBlock: &daoForkBlock,
DAOForkSupport: true,
EIP150Block: &eip150Block,
EIP150Hash: common.Hash{}.String(),
EIP155Block: &eip155Block,
EIP158Block: &eip158Block,
ByzantiumBlock: &byzantiumBlock,
ConstantinopleBlock: &constantinopleBlock,
PetersburgBlock: &petersburgBlock,
IstanbulBlock: &istanbulBlock,
MuirGlacierBlock: &muirGlacierBlock,
BerlinBlock: &berlinBlock,
LondonBlock: &londonBlock,
ArrowGlacierBlock: &arrowGlacierBlock,
MergeForkBlock: &mergeForkBlock,
}
}

func getBlockValue(block *sdk.Int) *big.Int {
if block == nil || block.IsNegative() {
return nil
}

return block.BigInt()
}

// Validate performs a basic validation of the ChainConfig params. The function will return an error
// if any of the block values is uninitialized (i.e nil) or if the EIP150Hash is an invalid hash.
func (cc ChainConfig) Validate() error {
if err := validateBlock(cc.HomesteadBlock); err != nil {
return sdkerrors.Wrap(err, "homesteadBlock")
}
if err := validateBlock(cc.DAOForkBlock); err != nil {
return sdkerrors.Wrap(err, "daoForkBlock")
}
if err := validateBlock(cc.EIP150Block); err != nil {
return sdkerrors.Wrap(err, "eip150Block")
}
if err := validateHash(cc.EIP150Hash); err != nil {
return err
}
if err := validateBlock(cc.EIP155Block); err != nil {
return sdkerrors.Wrap(err, "eip155Block")
}
if err := validateBlock(cc.EIP158Block); err != nil {
return sdkerrors.Wrap(err, "eip158Block")
}
if err := validateBlock(cc.ByzantiumBlock); err != nil {
return sdkerrors.Wrap(err, "byzantiumBlock")
}
if err := validateBlock(cc.ConstantinopleBlock); err != nil {
return sdkerrors.Wrap(err, "constantinopleBlock")
}
if err := validateBlock(cc.PetersburgBlock); err != nil {
return sdkerrors.Wrap(err, "petersburgBlock")
}
if err := validateBlock(cc.IstanbulBlock); err != nil {
return sdkerrors.Wrap(err, "istanbulBlock")
}
if err := validateBlock(cc.MuirGlacierBlock); err != nil {
return sdkerrors.Wrap(err, "muirGlacierBlock")
}
if err := validateBlock(cc.BerlinBlock); err != nil {
return sdkerrors.Wrap(err, "berlinBlock")
}
if err := validateBlock(cc.LondonBlock); err != nil {
return sdkerrors.Wrap(err, "londonBlock")
}
if err := validateBlock(cc.ArrowGlacierBlock); err != nil {
return sdkerrors.Wrap(err, "arrowGlacierBlock")
}
if err := validateBlock(cc.MergeForkBlock); err != nil {
return sdkerrors.Wrap(err, "mergeForkBlock")
}

// NOTE: chain ID is not needed to check config order
if err := cc.EthereumConfig(nil).CheckConfigForkOrder(); err != nil {
return sdkerrors.Wrap(err, "invalid config fork order")
}
return nil
}

func validateHash(hex string) error {
if hex != "" && strings.TrimSpace(hex) == "" {
return sdkerrors.Wrap(types.ErrInvalidChainConfig, "hash cannot be blank")
}

return nil
}

func validateBlock(block *sdk.Int) error {
// nil value means that the fork has not yet been applied
if block == nil {
return nil
}

if block.IsNegative() {
return sdkerrors.Wrapf(
types.ErrInvalidChainConfig, "block value cannot be negative: %s", block,
)
}

return nil
}
Loading

0 comments on commit 9a45f8c

Please sign in to comment.