From 6c7c1f4edbb6820325511daa64884b631523b9ee Mon Sep 17 00:00:00 2001 From: Pierre <974741468@qq.com> Date: Wed, 9 Dec 2020 14:43:46 +0800 Subject: [PATCH] Merge PR: add extra eip parm (#480) Co-authored-by: Zhong Qiu <36867992+zhongqiuwood@users.noreply.github.com> --- x/evm/types/params.go | 31 +++++++++++++++++++++++++++++-- x/evm/types/params_test.go | 13 ++++++++++++- x/evm/types/state_transition.go | 9 +++++++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/x/evm/types/params.go b/x/evm/types/params.go index 452e91f56d..49aede7a9c 100644 --- a/x/evm/types/params.go +++ b/x/evm/types/params.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "github.com/ethereum/go-ethereum/core/vm" "gopkg.in/yaml.v2" @@ -21,6 +22,7 @@ var ( ParamStoreKeyEVMDenom = []byte("EVMDenom") ParamStoreKeyEnableCreate = []byte("EnableCreate") ParamStoreKeyEnableCall = []byte("EnableCall") + ParamStoreKeyExtraEIPs = []byte("EnableExtraEIPs") ) // ParamKeyTable returns the parameter key table. @@ -37,14 +39,17 @@ type Params struct { EnableCreate bool `json:"enable_create" yaml:"enable_create"` // EnableCall toggles state transitions that use the vm.Call function EnableCall bool `json:"enable_call" yaml:"enable_call"` + // ExtraEIPs defines the additional EIPs for the vm.Config + ExtraEIPs []int `json:"extra_eips" yaml:"extra_eips"` } // NewParams creates a new Params instance -func NewParams(evmDenom string, enableCreate, enableCall bool) Params { +func NewParams(evmDenom string, enableCreate, enableCall bool, extraEIPs ...int) Params { return Params{ EvmDenom: evmDenom, EnableCreate: enableCreate, EnableCall: enableCall, + ExtraEIPs: extraEIPs, } } @@ -54,6 +59,7 @@ func DefaultParams() Params { EvmDenom: ethermint.NativeToken, EnableCreate: true, EnableCall: true, + ExtraEIPs: []int(nil), // TODO: define default values } } @@ -69,12 +75,17 @@ func (p *Params) ParamSetPairs() params.ParamSetPairs { params.NewParamSetPair(ParamStoreKeyEVMDenom, &p.EvmDenom, validateEVMDenom), params.NewParamSetPair(ParamStoreKeyEnableCreate, &p.EnableCreate, validateBool), params.NewParamSetPair(ParamStoreKeyEnableCall, &p.EnableCall, validateBool), + params.NewParamSetPair(ParamStoreKeyExtraEIPs, &p.ExtraEIPs, validateEIPs), } } // Validate performs basic validation on evm parameters. func (p Params) Validate() error { - return sdk.ValidateDenom(p.EvmDenom) + if err := sdk.ValidateDenom(p.EvmDenom); err != nil { + return err + } + + return validateEIPs(p.ExtraEIPs) } func validateEVMDenom(i interface{}) error { @@ -93,3 +104,19 @@ func validateBool(i interface{}) error { } return nil } + + +func validateEIPs(i interface{}) error { + eips, ok := i.([]int) + if !ok { + return fmt.Errorf("invalid EIP slice type: %T", i) + } + + for _, eip := range eips { + if !vm.ValidEip(eip) { + return fmt.Errorf("EIP %d is not activateable", eip) + } + } + + return nil +} \ No newline at end of file diff --git a/x/evm/types/params_test.go b/x/evm/types/params_test.go index 916eb5c954..880e129b9d 100644 --- a/x/evm/types/params_test.go +++ b/x/evm/types/params_test.go @@ -15,7 +15,7 @@ func TestParamsValidate(t *testing.T) { {"default", DefaultParams(), false}, { "valid", - NewParams("ara", true, true), + NewParams("ara", true, true, 2929, 1884, 1344), false, }, { @@ -30,6 +30,14 @@ func TestParamsValidate(t *testing.T) { }, true, }, + { + "invalid eip", + Params{ + EvmDenom: "stake", + ExtraEIPs: []int{1}, + }, + true, + }, } for _, tc := range testCases { @@ -48,4 +56,7 @@ func TestParamsValidatePriv(t *testing.T) { require.NoError(t, validateEVMDenom("aphoton")) require.Error(t, validateBool("")) require.NoError(t, validateBool(true)) + require.Error(t, validateEIPs("")) + require.NoError(t, validateEIPs([]int{1884})) } + diff --git a/x/evm/types/state_transition.go b/x/evm/types/state_transition.go index 3e8ece424f..b2853832c6 100644 --- a/x/evm/types/state_transition.go +++ b/x/evm/types/state_transition.go @@ -77,6 +77,7 @@ func (st StateTransition) newEVM( gasLimit uint64, gasPrice *big.Int, config ChainConfig, + extraEIPs []int, ) *vm.EVM { // Create context for evm context := vm.Context{ @@ -92,7 +93,11 @@ func (st StateTransition) newEVM( GasPrice: gasPrice, } - return vm.NewEVM(context, csdb, config.EthereumConfig(st.ChainID), vm.Config{}) + vmConfig := vm.Config{ + ExtraEips: extraEIPs, + } + + return vm.NewEVM(context, csdb, config.EthereumConfig(st.ChainID), vmConfig) } // TransitionDb will transition the state by applying the current transaction and @@ -139,7 +144,7 @@ func (st StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (*Ex return nil, errors.New("gas price cannot be nil") } - evm := st.newEVM(ctx, csdb, gasLimit, gasPrice.Int, config) + evm := st.newEVM(ctx, csdb, gasLimit, gasPrice.Int, config, params.ExtraEIPs) var ( ret []byte