evm: state transitions enabled params (#603)

* changelog

* evm: state transitions enabled params

* changelog entry

* changelog
This commit is contained in:
Federico Kunze 2020-11-20 20:25:11 +01:00 committed by GitHub
parent 6f7634138a
commit ca476d0871
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 9 deletions

View File

@ -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

View File

@ -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")
)

View File

@ -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
}

View File

@ -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())
}

View File

@ -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)