2020-09-02 19:41:05 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2020-12-08 19:01:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/types"
|
2020-09-02 19:41:05 +00:00
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
var _ paramtypes.ParamSet = &Params{}
|
2020-09-02 19:41:05 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
const (
|
|
|
|
DefaultEVMDenom = types.AttoPhoton
|
|
|
|
)
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
// Parameter keys
|
|
|
|
var (
|
2020-11-20 19:25:11 +00:00
|
|
|
ParamStoreKeyEVMDenom = []byte("EVMDenom")
|
|
|
|
ParamStoreKeyEnableCreate = []byte("EnableCreate")
|
|
|
|
ParamStoreKeyEnableCall = []byte("EnableCall")
|
2020-12-08 19:01:28 +00:00
|
|
|
ParamStoreKeyExtraEIPs = []byte("EnableExtraEIPs")
|
2021-07-14 09:13:55 +00:00
|
|
|
ParamStoreKeyChainConfig = []byte("ChainConfig")
|
2021-07-14 11:10:31 +00:00
|
|
|
|
|
|
|
// AvailableExtraEIPs define the list of all EIPs that can be enabled by the EVM interpreter. These EIPs are applied in
|
|
|
|
// order and can override the instruction sets from the latest hard fork enabled by the ChainConfig. For more info
|
|
|
|
// check: https://github.com/ethereum/go-ethereum/blob/v1.10.4/core/vm/interpreter.go#L122
|
2021-10-05 15:38:20 +00:00
|
|
|
AvailableExtraEIPs = []int64{1344, 1884, 2200, 2929, 3198, 3529}
|
2020-09-02 19:41:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParamKeyTable returns the parameter key table.
|
2021-04-17 10:00:07 +00:00
|
|
|
func ParamKeyTable() paramtypes.KeyTable {
|
|
|
|
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParams creates a new Params instance
|
2021-08-26 10:08:11 +00:00
|
|
|
func NewParams(evmDenom string, enableCreate, enableCall bool, config ChainConfig, extraEIPs ...int64) Params {
|
2020-09-02 19:41:05 +00:00
|
|
|
return Params{
|
2020-11-20 19:25:11 +00:00
|
|
|
EvmDenom: evmDenom,
|
|
|
|
EnableCreate: enableCreate,
|
|
|
|
EnableCall: enableCall,
|
2020-12-08 19:01:28 +00:00
|
|
|
ExtraEIPs: extraEIPs,
|
2021-07-14 09:13:55 +00:00
|
|
|
ChainConfig: config,
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultParams returns default evm parameters
|
2021-07-14 11:10:31 +00:00
|
|
|
// ExtraEIPs is empty to prevent overriding the latest hard fork instruction set
|
2020-09-02 19:41:05 +00:00
|
|
|
func DefaultParams() Params {
|
|
|
|
return Params{
|
2021-05-31 09:05:32 +00:00
|
|
|
EvmDenom: DefaultEVMDenom,
|
2020-11-20 19:25:11 +00:00
|
|
|
EnableCreate: true,
|
|
|
|
EnableCall: true,
|
2021-07-14 09:13:55 +00:00
|
|
|
ChainConfig: DefaultChainConfig(),
|
2021-07-14 11:10:31 +00:00
|
|
|
ExtraEIPs: nil,
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParamSetPairs returns the parameter set pairs.
|
2021-04-17 10:00:07 +00:00
|
|
|
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
|
|
|
|
return paramtypes.ParamSetPairs{
|
|
|
|
paramtypes.NewParamSetPair(ParamStoreKeyEVMDenom, &p.EvmDenom, validateEVMDenom),
|
|
|
|
paramtypes.NewParamSetPair(ParamStoreKeyEnableCreate, &p.EnableCreate, validateBool),
|
|
|
|
paramtypes.NewParamSetPair(ParamStoreKeyEnableCall, &p.EnableCall, validateBool),
|
|
|
|
paramtypes.NewParamSetPair(ParamStoreKeyExtraEIPs, &p.ExtraEIPs, validateEIPs),
|
2021-07-14 09:13:55 +00:00
|
|
|
paramtypes.NewParamSetPair(ParamStoreKeyChainConfig, &p.ChainConfig, validateChainConfig),
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate performs basic validation on evm parameters.
|
|
|
|
func (p Params) Validate() error {
|
2020-12-08 19:01:28 +00:00
|
|
|
if err := sdk.ValidateDenom(p.EvmDenom); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:13:55 +00:00
|
|
|
if err := validateEIPs(p.ExtraEIPs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.ChainConfig.Validate()
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
// EIPs returns the ExtraEips as a int slice
|
|
|
|
func (p Params) EIPs() []int {
|
|
|
|
eips := make([]int, len(p.ExtraEIPs))
|
|
|
|
for i, eip := range p.ExtraEIPs {
|
|
|
|
eips[i] = int(eip)
|
|
|
|
}
|
|
|
|
return eips
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:41:05 +00:00
|
|
|
func validateEVMDenom(i interface{}) error {
|
|
|
|
denom, ok := i.(string)
|
|
|
|
if !ok {
|
2020-11-20 19:25:11 +00:00
|
|
|
return fmt.Errorf("invalid parameter EVM denom type: %T", i)
|
2020-09-02 19:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.ValidateDenom(denom)
|
|
|
|
}
|
2020-11-20 19:25:11 +00:00
|
|
|
|
|
|
|
func validateBool(i interface{}) error {
|
|
|
|
_, ok := i.(bool)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-08 19:01:28 +00:00
|
|
|
|
|
|
|
func validateEIPs(i interface{}) error {
|
2021-01-06 20:56:40 +00:00
|
|
|
eips, ok := i.([]int64)
|
2020-12-08 19:01:28 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid EIP slice type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, eip := range eips {
|
2021-01-06 20:56:40 +00:00
|
|
|
if !vm.ValidEip(int(eip)) {
|
2021-05-10 16:34:00 +00:00
|
|
|
return fmt.Errorf("EIP %d is not activateable, valid EIPS are: %s", eip, vm.ActivateableEips())
|
2020-12-08 19:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-14 09:13:55 +00:00
|
|
|
|
|
|
|
func validateChainConfig(i interface{}) error {
|
|
|
|
cfg, ok := i.(ChainConfig)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid chain config type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Validate()
|
|
|
|
}
|