Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

evm: state transitions enabled params #603

Merged
merged 8 commits into from
Nov 20, 2020
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [v0.3.0] - 2020-11-16

### Improvements

* (evm) [#603](https://github.com/cosmos/ethermint/pull/603) Add state transition params that enable or disable the EVM `Call` and `Create` operations.

## [v0.3.0] - 2020-11-16

### API Breaking

* (crypto) [\#559](https://github.com/cosmos/ethermint/pull/559) Refactored crypto package in preparation for the SDK's Stargate release:
Expand Down
6 changes: 6 additions & 0 deletions x/evm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
32 changes: 27 additions & 5 deletions x/evm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
}

Expand All @@ -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),
}
}

Expand All @@ -66,8 +80,16 @@ func (p Params) Validate() error {
func validateEVMDenom(i interface{}) error {
denom, ok := i.(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cheks and passes if the passed denom is a string but do need to check if it's the same as the one set in genesis?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just local validation. We don't have access to the SDK context unfortunately from the parameters in order to test against other variables

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
}
7 changes: 5 additions & 2 deletions x/evm/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestParamsValidate(t *testing.T) {
{"default", DefaultParams(), false},
{
"valid",
NewParams("ara"),
NewParams("ara", true, true),
false,
},
{
Expand Down Expand Up @@ -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())
}
13 changes: 11 additions & 2 deletions x/evm/types/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down