refactor: optimize AnteHandler gas consumption (#1388)

* refactor: antehandler order and params optimization

* removed additional individual params for the feemarket module

* typo fix

* Apply suggestions from code review - Fede

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* typo fix

* formatted using gofumpt

* typo fix - missed negate operator

* missed to negate conditions

* added unit tests for the new param getter methods

* updated changelog

* Apply suggestions from code review

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>

* moved to improvements

* Converted unit tests into table-driven tests

* added Require().Equal() to test case

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Vladislav Varadinov
2022-10-19 18:21:59 +02:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent 5879750b30
commit 83e509bc57
19 changed files with 258 additions and 66 deletions
+35
View File
@@ -21,3 +21,38 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
// GetChainConfig returns the chain configuration parameter.
func (k Keeper) GetChainConfig(ctx sdk.Context) types.ChainConfig {
chainCfg := types.ChainConfig{}
k.paramSpace.GetIfExists(ctx, types.ParamStoreKeyChainConfig, &chainCfg)
return chainCfg
}
// GetEVMDenom returns the EVM denom.
func (k Keeper) GetEVMDenom(ctx sdk.Context) string {
evmDenom := ""
k.paramSpace.GetIfExists(ctx, types.ParamStoreKeyEVMDenom, &evmDenom)
return evmDenom
}
// GetEnableCall returns true if the EVM Call operation is enabled.
func (k Keeper) GetEnableCall(ctx sdk.Context) bool {
enableCall := false
k.paramSpace.GetIfExists(ctx, types.ParamStoreKeyEnableCall, &enableCall)
return enableCall
}
// GetEnableCreate returns true if the EVM Create contract operation is enabled.
func (k Keeper) GetEnableCreate(ctx sdk.Context) bool {
enableCreate := false
k.paramSpace.GetIfExists(ctx, types.ParamStoreKeyEnableCreate, &enableCreate)
return enableCreate
}
// 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 {
allowUnprotectedTx := false
k.paramSpace.GetIfExists(ctx, types.ParamStoreKeyAllowUnprotectedTxs, &allowUnprotectedTx)
return allowUnprotectedTx
}
+85 -4
View File
@@ -2,13 +2,94 @@ package keeper_test
import (
"github.com/evmos/ethermint/x/evm/types"
"reflect"
)
func (suite *KeeperTestSuite) TestParams() {
params := suite.app.EvmKeeper.GetParams(suite.ctx)
suite.Require().Equal(types.DefaultParams(), params)
params.EvmDenom = "inj"
suite.app.EvmKeeper.SetParams(suite.ctx, params)
newParams := suite.app.EvmKeeper.GetParams(suite.ctx)
suite.Require().Equal(newParams, params)
testCases := []struct {
name string
paramsFun func() interface{}
getFun func() interface{}
expected bool
}{
{
"success - Checks if the default params are set correctly",
func() interface{} {
return types.DefaultParams()
},
func() interface{} {
return suite.app.EvmKeeper.GetParams(suite.ctx)
},
true,
},
{
"success - EvmDenom param is set to \"inj\" and can be retrieved correctly",
func() interface{} {
params.EvmDenom = "inj"
suite.app.EvmKeeper.SetParams(suite.ctx, params)
return params.EvmDenom
},
func() interface{} {
return suite.app.EvmKeeper.GetEVMDenom(suite.ctx)
},
true,
},
{
"success - Check EnableCreate param is set to false and can be retrieved correctly",
func() interface{} {
params.EnableCreate = false
suite.app.EvmKeeper.SetParams(suite.ctx, params)
return params.EnableCreate
},
func() interface{} {
return suite.app.EvmKeeper.GetEnableCreate(suite.ctx)
},
true,
},
{
"success - Check EnableCall param is set to false and can be retrieved correctly",
func() interface{} {
params.EnableCall = false
suite.app.EvmKeeper.SetParams(suite.ctx, params)
return params.EnableCall
},
func() interface{} {
return suite.app.EvmKeeper.GetEnableCall(suite.ctx)
},
true,
},
{
"success - Check AllowUnprotectedTxs param is set to false and can be retrieved correctly",
func() interface{} {
params.AllowUnprotectedTxs = false
suite.app.EvmKeeper.SetParams(suite.ctx, params)
return params.AllowUnprotectedTxs
},
func() interface{} {
return suite.app.EvmKeeper.GetAllowUnprotectedTxs(suite.ctx)
},
true,
},
{
"success - Check ChainConfig param is set to the default value and can be retrieved correctly",
func() interface{} {
params.ChainConfig = types.DefaultChainConfig()
suite.app.EvmKeeper.SetParams(suite.ctx, params)
return params.ChainConfig
},
func() interface{} {
return suite.app.EvmKeeper.GetChainConfig(suite.ctx)
},
true,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
outcome := reflect.DeepEqual(tc.paramsFun(), tc.getFun())
suite.Require().Equal(tc.expected, outcome)
})
}
}
+1 -1
View File
@@ -60,7 +60,7 @@ func (suite *TxDataTestSuite) TestAccessListTxGetGasFeeCap() {
func (suite *TxDataTestSuite) TestEmptyAccessList() {
testCases := []struct {
name string
tx AccessListTx
tx AccessListTx
}{
{
"empty access list tx",
+3 -3
View File
@@ -627,7 +627,7 @@ func (suite *TxDataTestSuite) TestDynamicFeeTxEffectiveFee() {
DynamicFeeTx{
GasTipCap: &suite.sdkInt,
GasFeeCap: &suite.sdkInt,
GasLimit: uint64(1),
GasLimit: uint64(1),
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
@@ -653,8 +653,8 @@ func (suite *TxDataTestSuite) TestDynamicFeeTxEffectiveCost() {
DynamicFeeTx{
GasTipCap: &suite.sdkInt,
GasFeeCap: &suite.sdkInt,
GasLimit: uint64(1),
Amount: &suite.sdkZeroInt,
GasLimit: uint64(1),
Amount: &suite.sdkZeroInt,
},
(&suite.sdkInt).BigInt(),
(&suite.sdkInt).BigInt(),
+2 -2
View File
@@ -116,9 +116,9 @@ func (suite *GenesisTestSuite) TestValidateGenesis() {
expPass: false,
},
{
name: "copied genesis",
name: "copied genesis",
genState: NewGenesisState(DefaultGenesisState().Params, DefaultGenesisState().Accounts),
expPass: true,
expPass: true,
},
{
name: "invalid genesis",
-1
View File
@@ -198,4 +198,3 @@ func TestConversionFunctions(t *testing.T) {
require.Nil(t, conversionErr)
require.Nil(t, copyErr)
}
+16 -16
View File
@@ -11,9 +11,9 @@ import (
func (suite *TxDataTestSuite) TestTxArgsString() {
testCases := []struct {
name string
txArgs TransactionArgs
expectedString string
name string
txArgs TransactionArgs
expectedString string
}{
{
"empty tx args",
@@ -32,25 +32,25 @@ func (suite *TxDataTestSuite) TestTxArgsString() {
AccessList: &ethtypes.AccessList{},
},
fmt.Sprintf("TransactionArgs{From:%v, To:%v, Gas:%v, Nonce:%v, Data:%v, Input:%v, AccessList:%v}",
&suite.addr,
&suite.addr,
&suite.hexUint64,
&suite.hexUint64,
&suite.hexDataBytes,
&suite.hexInputBytes,
&ethtypes.AccessList{}),
&suite.addr,
&suite.addr,
&suite.hexUint64,
&suite.hexUint64,
&suite.hexDataBytes,
&suite.hexInputBytes,
&ethtypes.AccessList{}),
},
}
for _, tc := range testCases {
outputString := tc.txArgs.String()
suite.Require().Equal(outputString, tc.expectedString)
suite.Require().Equal(outputString, tc.expectedString)
}
}
func (suite *TxDataTestSuite) TestConvertTxArgsEthTx() {
testCases := []struct {
name string
txArgs TransactionArgs
name string
txArgs TransactionArgs
}{
{
"empty tx args",
@@ -227,9 +227,9 @@ func (suite *TxDataTestSuite) TestToMessageEVM() {
func (suite *TxDataTestSuite) TestGetFrom() {
testCases := []struct {
name string
txArgs TransactionArgs
expAddress common.Address
name string
txArgs TransactionArgs
expAddress common.Address
}{
{
"empty from field",
+1 -1
View File
@@ -113,4 +113,4 @@ func TestTransactionLogsEncodeDecode(t *testing.T) {
txLogsEncodedDecoded, decodeErr := evmtypes.DecodeTransactionLogs(txLogsEncoded)
require.Nil(t, decodeErr)
require.Equal(t, txLogs, txLogsEncodedDecoded)
}
}