Skip to content

Commit

Permalink
refactor(params): store all params under one key in evm module (#1617)
Browse files Browse the repository at this point in the history
* refactor(params): store all params under one key in evm module

* refactor(params): add changelog entry

* refactor(params): update based on review comments. Remove params getter functions

* refactor(params): refactor params store key

* refactor(params): remove unnecessary store keys

* refactor(params): add paramSetPairs for backwards compatibility

* Update CHANGELOG.md

* refactor(params): add license to params_legacy file

* Apply suggestions from code review

* fix(evm): handle RC1 params during migration (#1624)

* fix(evm): handle RC1 params during migration

* migration

* fix: test case updated for RC1

* v5 migration

* tests

* tests pt2

* comment

* execute make proto-all

Co-authored-by: Vladislav Varadinov <[email protected]>
Co-authored-by: MalteHerrmann <[email protected]>

* Apply suggestions from code review

* rm dup vars

Co-authored-by: Federico Kunze Küllmer <[email protected]>
Co-authored-by: Vladislav Varadinov <[email protected]>
Co-authored-by: MalteHerrmann <[email protected]>
  • Loading branch information
4 people committed Jan 23, 2023
1 parent f9d0e13 commit b549359
Show file tree
Hide file tree
Showing 29 changed files with 1,302 additions and 1,373 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* (rpc) [#1613](https://github.com/evmos/ethermint/pull/1613) Change the default json-rpc listen address to localhost.
* (upgrade) [#1617](https://github.com/evmos/ethermint/pull/1617) Refactor `evm` module's parameters to store them under a single store key
* (rpc) [#1611](https://github.com/evmos/ethermint/pull/1611) Add missing next fee in fee history, fix wrong oldestBlock and align earliest input as ethereum.

### Improvements
Expand Down
5 changes: 3 additions & 2 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
return next(newCtx, tx, simulate)
}

chainCfg := egcd.evmKeeper.GetChainConfig(ctx)
evmParams := egcd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(egcd.evmKeeper.ChainID())

blockHeight := big.NewInt(ctx.BlockHeight())
Expand Down Expand Up @@ -183,7 +184,7 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula
gasWanted += txData.GetGas()
}

evmDenom := egcd.evmKeeper.GetEVMDenom(ctx)
evmDenom := evmParams.GetEvmDenom()

fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, homestead, istanbul, ctx.IsCheckTx())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion app/ante/fee_market.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func NewGasWantedDecorator(
}

func (gwd GasWantedDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
chainCfg := gwd.evmKeeper.GetChainConfig(ctx)
evmParams := gwd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(gwd.evmKeeper.ChainID())

blockHeight := big.NewInt(ctx.BlockHeight())
Expand Down
12 changes: 7 additions & 5 deletions app/ante/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
if minGasPrice.IsZero() || simulate {
return next(ctx, tx, simulate)
}

evmDenom := mpd.evmKeeper.GetEVMDenom(ctx)
evmParams := mpd.evmKeeper.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()
minGasPrices := sdk.DecCoins{
{
Denom: evmDenom,
Expand Down Expand Up @@ -133,7 +133,8 @@ func (empd EthMinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
return next(ctx, tx, simulate)
}

chainCfg := empd.evmKeeper.GetChainConfig(ctx)
evmParams := empd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(empd.evmKeeper.ChainID())
baseFee := empd.evmKeeper.GetBaseFee(ctx, ethCfg)

Expand Down Expand Up @@ -191,7 +192,8 @@ func (mfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
if !ctx.IsCheckTx() || simulate {
return next(ctx, tx, simulate)
}
chainCfg := mfd.evmKeeper.GetChainConfig(ctx)
evmParams := mfd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(mfd.evmKeeper.ChainID())

baseFee := mfd.evmKeeper.GetBaseFee(ctx, ethCfg)
Expand All @@ -200,7 +202,7 @@ func (mfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulat
return next(ctx, tx, simulate)
}

evmDenom := mfd.evmKeeper.GetEVMDenom(ctx)
evmDenom := evmParams.GetEvmDenom()
minGasPrice := ctx.MinGasPrices().AmountOf(evmDenom)

for _, msg := range tx.GetMsgs() {
Expand Down
6 changes: 1 addition & 5 deletions app/ante/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ type EVMKeeper interface {
GetBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
GetTxIndexTransient(ctx sdk.Context) uint64
GetChainConfig(ctx sdk.Context) evmtypes.ChainConfig
GetAllowUnprotectedTxs(ctx sdk.Context) bool
GetEVMDenom(ctx sdk.Context) string
GetEnableCall(ctx sdk.Context) bool
GetEnableCreate(ctx sdk.Context) bool
GetParams(ctx sdk.Context) evmtypes.Params
}

type protoTxProvider interface {
Expand Down
9 changes: 5 additions & 4 deletions app/ante/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
txFee := sdk.Coins{}
txGasLimit := uint64(0)

chainCfg := vbd.evmKeeper.GetChainConfig(ctx)
evmParams := vbd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
chainID := vbd.evmKeeper.ChainID()
ethCfg := chainCfg.EthereumConfig(chainID)
baseFee := vbd.evmKeeper.GetBaseFee(ctx, ethCfg)
enableCreate := vbd.evmKeeper.GetEnableCreate(ctx)
enableCall := vbd.evmKeeper.GetEnableCall(ctx)
evmDenom := vbd.evmKeeper.GetEVMDenom(ctx)
enableCreate := evmParams.GetEnableCreate()
enableCall := evmParams.GetEnableCall()
evmDenom := evmParams.GetEvmDenom()

for _, msg := range protoTx.GetMsgs() {
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
Expand Down
5 changes: 3 additions & 2 deletions app/ante/sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func NewEthSigVerificationDecorator(ek EVMKeeper) EthSigVerificationDecorator {
// won't see the error message.
func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
chainID := esvd.evmKeeper.ChainID()
chainCfg := esvd.evmKeeper.GetChainConfig(ctx)
evmParams := esvd.evmKeeper.GetParams(ctx)
chainCfg := evmParams.GetChainConfig()
ethCfg := chainCfg.EthereumConfig(chainID)
blockNum := big.NewInt(ctx.BlockHeight())
signer := ethtypes.MakeSigner(ethCfg, blockNum)
Expand All @@ -55,7 +56,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
}

allowUnprotectedTxs := esvd.evmKeeper.GetAllowUnprotectedTxs(ctx)
allowUnprotectedTxs := evmParams.GetAllowUnprotectedTxs()
ethTx := msgEthTx.AsTransaction()
if !allowUnprotectedTxs && !ethTx.Protected() {
return ctx, errorsmod.Wrapf(
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
// ethermint subspaces
paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(evmtypes.ParamKeyTable())
paramsKeeper.Subspace(evmtypes.ModuleName).WithKeyTable(evmtypes.ParamKeyTable()) //nolint: staticcheck
paramsKeeper.Subspace(feemarkettypes.ModuleName).WithKeyTable(feemarkettypes.ParamKeyTable())
return paramsKeeper
}
12 changes: 1 addition & 11 deletions proto/ethermint/evm/v1/evm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@ message Params {
// enable_call toggles state transitions that use the vm.Call function
bool enable_call = 3 [(gogoproto.moretags) = "yaml:\"enable_call\""];
// extra_eips defines the additional EIPs for the vm.Config
ExtraEIPs extra_eips = 4 [
(gogoproto.customname) = "ExtraEIPs",
(gogoproto.moretags) = "yaml:\"extra_eips\"",
(gogoproto.nullable) = false
];
repeated int64 extra_eips = 4 [(gogoproto.customname) = "ExtraEIPs", (gogoproto.moretags) = "yaml:\"extra_eips\""];
// chain_config defines the EVM chain configuration parameters
ChainConfig chain_config = 5 [(gogoproto.moretags) = "yaml:\"chain_config\"", (gogoproto.nullable) = false];
// allow_unprotected_txs defines if replay-protected (i.e non EIP155
// signed) transactions can be executed on the state machine.
bool allow_unprotected_txs = 6;
}

// ExtraEIPs represents extra EIPs for the vm.Config
message ExtraEIPs {
// eips defines the additional EIPs for the vm.Config
repeated int64 eips = 1 [(gogoproto.customname) = "EIPs", (gogoproto.moretags) = "yaml:\"eips\""];
}

// ChainConfig defines the Ethereum ChainConfig parameters using *sdk.Int values
// instead of *big.Int.
message ChainConfig {
Expand Down
3 changes: 2 additions & 1 deletion x/evm/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ func (suite *EvmTestSuite) TestERC20TransferReverted() {

before := k.GetBalance(suite.ctx, suite.from)

ethCfg := suite.app.EvmKeeper.GetChainConfig(suite.ctx).EthereumConfig(nil)
evmParams := suite.app.EvmKeeper.GetParams(suite.ctx)
ethCfg := evmParams.GetChainConfig().EthereumConfig(nil)
baseFee := suite.app.EvmKeeper.GetBaseFee(suite.ctx, ethCfg)

txData, err := types.UnpackTxData(tx.Data)
Expand Down
3 changes: 2 additions & 1 deletion x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ func (k *Keeper) GetNonce(ctx sdk.Context, addr common.Address) uint64 {
// GetBalance load account's balance of gas token
func (k *Keeper) GetBalance(ctx sdk.Context, addr common.Address) *big.Int {
cosmosAddr := sdk.AccAddress(addr.Bytes())
evmDenom := k.GetEVMDenom(ctx)
evmParams := k.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()
// if node is pruned, params is empty. Return invalid value
if evmDenom == "" {
return big.NewInt(-1)
Expand Down
6 changes: 6 additions & 0 deletions x/evm/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v4 "github.com/evmos/ethermint/x/evm/migrations/v4"
v5 "github.com/evmos/ethermint/x/evm/migrations/v5"
"github.com/evmos/ethermint/x/evm/types"
)

Expand All @@ -39,3 +40,8 @@ func NewMigrator(keeper Keeper, legacySubspace types.Subspace) Migrator {
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.keeper.storeKey, m.legacySubspace, m.keeper.cdc)
}

// Migrate4to5 migrates the store from consensus version 4 to 5
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v5.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
141 changes: 13 additions & 128 deletions x/evm/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import (

// GetParams returns the total set of evm parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
evmDenom := k.GetEVMDenom(ctx)
allowUnprotectedTx := k.GetAllowUnprotectedTxs(ctx)
enableCreate := k.GetEnableCreate(ctx)
enableCall := k.GetEnableCall(ctx)
chainCfg := k.GetChainConfig(ctx)
extraEIPs := k.GetExtraEIPs(ctx)

return types.NewParams(evmDenom, allowUnprotectedTx, enableCreate, enableCall, chainCfg, extraEIPs)
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefixParams)
if len(bz) == 0 {
return k.GetLegacyParams(ctx)
}
k.cdc.MustUnmarshal(bz, &params)
return
}

// SetParams sets the EVM params each in their individual key for better get performance
Expand All @@ -38,13 +37,13 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
return err
}

k.setExtraEIPs(ctx, params.ExtraEIPs)
k.setChainConfig(ctx, params.ChainConfig)
k.setEvmDenom(ctx, params.EvmDenom)
k.setEnableCall(ctx, params.EnableCall)
k.setEnableCreate(ctx, params.EnableCreate)
k.setAllowUnprotectedTxs(ctx, params.AllowUnprotectedTxs)
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err
}

store.Set(types.KeyPrefixParams, bz)
return nil
}

Expand All @@ -54,117 +53,3 @@ func (k Keeper) GetLegacyParams(ctx sdk.Context) types.Params {
k.ss.GetParamSetIfExists(ctx, &params)
return params
}

// GetExtraEIPs returns the extra EIPs enabled on the chain.
func (k Keeper) GetExtraEIPs(ctx sdk.Context) types.ExtraEIPs {
var extraEIPs types.ExtraEIPs
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamStoreKeyExtraEIPs)
if len(bz) == 0 {
return k.GetLegacyParams(ctx).ExtraEIPs
}
k.cdc.MustUnmarshal(bz, &extraEIPs)
return extraEIPs
}

// GetChainConfig returns the chain configuration parameter.
func (k Keeper) GetChainConfig(ctx sdk.Context) types.ChainConfig {
var chainCfg types.ChainConfig
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamStoreKeyChainConfig)
if len(bz) == 0 {
return k.GetLegacyParams(ctx).ChainConfig
}
k.cdc.MustUnmarshal(bz, &chainCfg)
return chainCfg
}

// GetEVMDenom returns the EVM denom.
func (k Keeper) GetEVMDenom(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ParamStoreKeyEVMDenom)
if len(bz) == 0 {
return k.GetLegacyParams(ctx).EvmDenom
}
return string(bz)
}

// GetEnableCall returns true if the EVM Call operation is enabled.
func (k Keeper) GetEnableCall(ctx sdk.Context) bool {
store := ctx.KVStore(k.storeKey)
exist := store.Has(types.ParamStoreKeyEnableCall)
if !exist {
exist = k.GetLegacyParams(ctx).EnableCall
}
return exist
}

// GetEnableCreate returns true if the EVM Create contract operation is enabled.
func (k Keeper) GetEnableCreate(ctx sdk.Context) bool {
store := ctx.KVStore(k.storeKey)
exist := store.Has(types.ParamStoreKeyEnableCreate)
if !exist {
exist = k.GetLegacyParams(ctx).EnableCreate
}
return exist
}

// GetAllowUnprotectedTxs returns true if unprotected txs (i.e non-replay protected as per EIP-155) are supported by the chain.
func (k Keeper) GetAllowUnprotectedTxs(ctx sdk.Context) bool {
store := ctx.KVStore(k.storeKey)
exist := store.Has(types.ParamStoreKeyAllowUnprotectedTxs)
if !exist {
exist = k.GetLegacyParams(ctx).AllowUnprotectedTxs
}
return exist
}

// setChainConfig sets the ChainConfig in the store
func (k Keeper) setChainConfig(ctx sdk.Context, chainCfg types.ChainConfig) {
store := ctx.KVStore(k.storeKey)
chainCfgBz := k.cdc.MustMarshal(&chainCfg)
store.Set(types.ParamStoreKeyChainConfig, chainCfgBz)
}

// setExtraEIPs sets the ExtraEIPs in the store
func (k Keeper) setExtraEIPs(ctx sdk.Context, extraEIPs types.ExtraEIPs) {
extraEIPsBz := k.cdc.MustMarshal(&extraEIPs)
store := ctx.KVStore(k.storeKey)
store.Set(types.ParamStoreKeyExtraEIPs, extraEIPsBz)
}

// setEvmDenom sets the EVMDenom param in the store
func (k Keeper) setEvmDenom(ctx sdk.Context, evmDenom string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.ParamStoreKeyEVMDenom, []byte(evmDenom))
}

// setAllowUnprotectedTxs sets the AllowUnprotectedTxs param in the store
func (k Keeper) setAllowUnprotectedTxs(ctx sdk.Context, enable bool) {
store := ctx.KVStore(k.storeKey)
if enable {
store.Set(types.ParamStoreKeyAllowUnprotectedTxs, []byte{0x01})
return
}
store.Delete(types.ParamStoreKeyAllowUnprotectedTxs)
}

// setEnableCreate sets the EnableCreate param in the store
func (k Keeper) setEnableCreate(ctx sdk.Context, enable bool) {
store := ctx.KVStore(k.storeKey)
if enable {
store.Set(types.ParamStoreKeyEnableCreate, []byte{0x01})
return
}
store.Delete(types.ParamStoreKeyEnableCreate)
}

// setEnableCall sets the EnableCall param in the store
func (k Keeper) setEnableCall(ctx sdk.Context, enable bool) {
store := ctx.KVStore(k.storeKey)
if enable {
store.Set(types.ParamStoreKeyEnableCall, []byte{0x01})
return
}
store.Delete(types.ParamStoreKeyEnableCall)
}
Loading

0 comments on commit b549359

Please sign in to comment.