From ca476d087165c90f0ca06ab37c182f6103a164e5 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Fri, 20 Nov 2020 20:25:11 +0100 Subject: [PATCH] evm: state transitions enabled params (#603) * changelog * evm: state transitions enabled params * changelog entry * changelog --- CHANGELOG.md | 9 +++++++++ x/evm/types/errors.go | 6 ++++++ x/evm/types/params.go | 32 +++++++++++++++++++++++++++----- x/evm/types/params_test.go | 7 +++++-- x/evm/types/state_transition.go | 13 +++++++++++-- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc7223ef..d26215ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,15 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## Unreleased + +### Improvements + +* (deps) [\#610](https://github.com/cosmos/ethermint/pull/610) Update Go dependency to 1.15+. +* (deps) [\#608](https://github.com/cosmos/ethermint/pull/608) Bump Cosmos SDK version to [v0.39.2](https://github.com/cosmos/cosmos-sdk/tag/v0.39.2) +* (evm) [#603](https://github.com/cosmos/ethermint/pull/603) Add state transition params that enable or disable the EVM `Call` and `Create` operations. +* (deps) [\#602](https://github.com/cosmos/ethermint/pull/602) Bump tendermint version to [v0.33.9](https://github.com/tendermint/tendermint/releases/tag/v0.33.9) + ## [v0.3.0] - 2020-11-16 ### API Breaking diff --git a/x/evm/types/errors.go b/x/evm/types/errors.go index 7754f555..f52cf8ec 100644 --- a/x/evm/types/errors.go +++ b/x/evm/types/errors.go @@ -15,4 +15,10 @@ var ( // ErrInvalidChainConfig returns an error resulting from an invalid ChainConfig. ErrInvalidChainConfig = sdkerrors.Register(ModuleName, 4, "invalid chain configuration") + + // ErrCreateDisabled returns an error if the EnableCreate parameter is false. + ErrCreateDisabled = sdkerrors.Register(ModuleName, 5, "EVM Create operation is disabled") + + // ErrCallDisabled returns an error if the EnableCall parameter is false. + ErrCallDisabled = sdkerrors.Register(ModuleName, 6, "EVM Call operation is disabled") ) diff --git a/x/evm/types/params.go b/x/evm/types/params.go index a6c5ad3d..af7be03d 100644 --- a/x/evm/types/params.go +++ b/x/evm/types/params.go @@ -18,7 +18,9 @@ const ( // Parameter keys var ( - ParamStoreKeyEVMDenom = []byte("EVMDenom") + ParamStoreKeyEVMDenom = []byte("EVMDenom") + ParamStoreKeyEnableCreate = []byte("EnableCreate") + ParamStoreKeyEnableCall = []byte("EnableCall") ) // ParamKeyTable returns the parameter key table. @@ -28,20 +30,30 @@ func ParamKeyTable() params.KeyTable { // Params defines the EVM module parameters type Params struct { + // EVMDenom defines the token denomination used for state transitions on the + // EVM module. EvmDenom string `json:"evm_denom" yaml:"evm_denom"` + // EnableCreate toggles state transitions that use the vm.Create function + 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"` } // NewParams creates a new Params instance -func NewParams(evmDenom string) Params { +func NewParams(evmDenom string, enableCreate, enableCall bool) Params { return Params{ - EvmDenom: evmDenom, + EvmDenom: evmDenom, + EnableCreate: enableCreate, + EnableCall: enableCall, } } // DefaultParams returns default evm parameters func DefaultParams() Params { return Params{ - EvmDenom: ethermint.AttoPhoton, + EvmDenom: ethermint.AttoPhoton, + EnableCreate: true, + EnableCall: true, } } @@ -55,6 +67,8 @@ func (p Params) String() string { func (p *Params) ParamSetPairs() params.ParamSetPairs { return params.ParamSetPairs{ params.NewParamSetPair(ParamStoreKeyEVMDenom, &p.EvmDenom, validateEVMDenom), + params.NewParamSetPair(ParamStoreKeyEnableCreate, &p.EnableCreate, validateBool), + params.NewParamSetPair(ParamStoreKeyEnableCall, &p.EnableCall, validateBool), } } @@ -66,8 +80,16 @@ func (p Params) Validate() error { func validateEVMDenom(i interface{}) error { denom, ok := i.(string) if !ok { - return fmt.Errorf("invalid parameter type: %T", i) + return fmt.Errorf("invalid parameter EVM denom type: %T", i) } return sdk.ValidateDenom(denom) } + +func validateBool(i interface{}) error { + _, ok := i.(bool) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + return nil +} diff --git a/x/evm/types/params_test.go b/x/evm/types/params_test.go index 244e360c..044876eb 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"), + NewParams("ara", true, true), false, }, { @@ -45,8 +45,11 @@ func TestParamsValidate(t *testing.T) { func TestParamsValidatePriv(t *testing.T) { require.Error(t, validateEVMDenom(false)) + require.NoError(t, validateEVMDenom("aphoton")) + require.Error(t, validateBool("")) + require.NoError(t, validateBool(true)) } func TestParams_String(t *testing.T) { - require.Equal(t, "evm_denom: aphoton\n", DefaultParams().String()) + require.Equal(t, "evm_denom: aphoton\nenable_create: true\nenable_call: true\n", DefaultParams().String()) } diff --git a/x/evm/types/state_transition.go b/x/evm/types/state_transition.go index 9c203717..8e0c5acf 100644 --- a/x/evm/types/state_transition.go +++ b/x/evm/types/state_transition.go @@ -101,8 +101,9 @@ func (st StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (*Ex // Clear cache of accounts to handle changes outside of the EVM csdb.UpdateAccounts() - evmDenom := csdb.GetParams().EvmDenom - gasPrice := ctx.MinGasPrices().AmountOf(evmDenom) + params := csdb.GetParams() + + gasPrice := ctx.MinGasPrices().AmountOf(params.EvmDenom) if gasPrice.IsNil() { return nil, errors.New("gas price cannot be nil") } @@ -125,9 +126,17 @@ func (st StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (*Ex // create contract or execute call switch contractCreation { case true: + if !params.EnableCreate { + return nil, ErrCreateDisabled + } + ret, contractAddress, leftOverGas, err = evm.Create(senderRef, st.Payload, gasLimit, st.Amount) recipientLog = fmt.Sprintf("contract address %s", contractAddress.String()) default: + if !params.EnableCall { + return nil, ErrCreateDisabled + } + // Increment the nonce for the next transaction (just for evm state transition) csdb.SetNonce(st.Sender, csdb.GetNonce(st.Sender)+1) ret, leftOverGas, err = evm.Call(senderRef, *st.Recipient, st.Payload, gasLimit, st.Amount)