From 8bcdb2c0e540a6611cab59d5d2a43326c0c6382b Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 21 Mar 2022 17:01:21 +0800 Subject: [PATCH] make MaxTxGasWanted configurable (#1004) --- CHANGELOG.md | 1 + app/ante/eth.go | 11 +++++++---- app/ante/eth_test.go | 3 ++- app/ante/handler_options.go | 3 ++- app/app.go | 2 ++ server/config/config.go | 10 ++++++++-- server/config/toml.go | 3 +++ server/flags/flags.go | 3 ++- server/start.go | 1 + 9 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f6f4f37cd..c027e2f2d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [tharsis#990](https://github.com/tharsis/ethermint/pull/990) Calculate reward values from all `MsgEthereumTx` from a block in `eth_feeHistory`. * (ante) [tharsis#991](https://github.com/tharsis/ethermint/pull/991) Set an upper bound to gasWanted to prevent DoS attack. +* (ante) [tharsis#1004](https://github.com/tharsis/ethermint/pull/1004) make MaxTxGasWanted configurable. ## [v0.11.0] - 2022-03-06 diff --git a/app/ante/eth.go b/app/ante/eth.go index 8ac9a6d640..48f55b4c6b 100644 --- a/app/ante/eth.go +++ b/app/ante/eth.go @@ -136,15 +136,18 @@ func (avd EthAccountVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx // EthGasConsumeDecorator validates enough intrinsic gas for the transaction and // gas consumption. type EthGasConsumeDecorator struct { - evmKeeper EVMKeeper + evmKeeper EVMKeeper + maxGasWanted uint64 } // NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator func NewEthGasConsumeDecorator( evmKeeper EVMKeeper, + maxGasWanted uint64, ) EthGasConsumeDecorator { return EthGasConsumeDecorator{ - evmKeeper: evmKeeper, + evmKeeper, + maxGasWanted, } } @@ -188,8 +191,8 @@ func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula if ctx.IsCheckTx() { // We can't trust the tx gas limit, because we'll refund the unused gas. - if txData.GetGas() > MaxTxGasWanted { - gasWanted += MaxTxGasWanted + if txData.GetGas() > egcd.maxGasWanted { + gasWanted += egcd.maxGasWanted } else { gasWanted += txData.GetGas() } diff --git a/app/ante/eth_test.go b/app/ante/eth_test.go index 01a55734c5..e2fb65db6b 100644 --- a/app/ante/eth_test.go +++ b/app/ante/eth_test.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/tharsis/ethermint/app/ante" + "github.com/tharsis/ethermint/server/config" "github.com/tharsis/ethermint/tests" "github.com/tharsis/ethermint/x/evm/statedb" evmtypes "github.com/tharsis/ethermint/x/evm/types" @@ -201,7 +202,7 @@ func (suite AnteTestSuite) TestEthNonceVerificationDecorator() { } func (suite AnteTestSuite) TestEthGasConsumeDecorator() { - dec := ante.NewEthGasConsumeDecorator(suite.app.EvmKeeper) + dec := ante.NewEthGasConsumeDecorator(suite.app.EvmKeeper, config.DefaultMaxTxGasWanted) addr := tests.GenerateAddress() diff --git a/app/ante/handler_options.go b/app/ante/handler_options.go index f9bd9b9b21..1eb9baf9ef 100644 --- a/app/ante/handler_options.go +++ b/app/ante/handler_options.go @@ -25,6 +25,7 @@ type HandlerOptions struct { FeegrantKeeper ante.FeegrantKeeper SignModeHandler authsigning.SignModeHandler SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error + MaxTxGasWanted uint64 } func (options HandlerOptions) Validate() error { @@ -53,7 +54,7 @@ func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler { NewEthValidateBasicDecorator(options.EvmKeeper), NewEthSigVerificationDecorator(options.EvmKeeper), NewEthAccountVerificationDecorator(options.AccountKeeper, options.BankKeeper, options.EvmKeeper), - NewEthGasConsumeDecorator(options.EvmKeeper), + NewEthGasConsumeDecorator(options.EvmKeeper, options.MaxTxGasWanted), NewCanTransferDecorator(options.EvmKeeper), NewEthIncrementSenderSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator. ) diff --git a/app/app.go b/app/app.go index af4be4bcdd..3517ed4f7f 100644 --- a/app/app.go +++ b/app/app.go @@ -567,6 +567,7 @@ func NewEthermintApp( // use Ethermint's custom AnteHandler + maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)) options := ante.HandlerOptions{ AccountKeeper: app.AccountKeeper, BankKeeper: app.BankKeeper, @@ -576,6 +577,7 @@ func NewEthermintApp( FeeMarketKeeper: app.FeeMarketKeeper, SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + MaxTxGasWanted: maxGasWanted, } if err := options.Validate(); err != nil { diff --git a/server/config/config.go b/server/config/config.go index 4909d50708..023e5a62b7 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -28,6 +28,8 @@ const ( // DefaultEVMTracer is the default vm.Tracer type DefaultEVMTracer = "" + DefaultMaxTxGasWanted = 500000 + DefaultGasCap uint64 = 25000000 DefaultFilterCap int32 = 200 @@ -64,6 +66,8 @@ type EVMConfig struct { // Tracer defines vm.Tracer type that the EVM will use if the node is run in // trace mode. Default: 'json'. Tracer string `mapstructure:"tracer"` + // MaxTxGasWanted defines the gas wanted for each eth tx returned in ante handler in check tx mode. + MaxTxGasWanted uint64 `mapstructure:"max-tx-gas-wanted"` } // JSONRPCConfig defines configuration for the EVM RPC server. @@ -152,7 +156,8 @@ func DefaultConfig() *Config { // DefaultEVMConfig returns the default EVM configuration func DefaultEVMConfig() *EVMConfig { return &EVMConfig{ - Tracer: DefaultEVMTracer, + Tracer: DefaultEVMTracer, + MaxTxGasWanted: DefaultMaxTxGasWanted, } } @@ -277,7 +282,8 @@ func GetConfig(v *viper.Viper) Config { return Config{ Config: cfg, EVM: EVMConfig{ - Tracer: v.GetString("evm.tracer"), + Tracer: v.GetString("evm.tracer"), + MaxTxGasWanted: v.GetUint64("evm.max-tx-gas-wanted"), }, JSONRPC: JSONRPCConfig{ Enable: v.GetBool("json-rpc.enable"), diff --git a/server/config/toml.go b/server/config/toml.go index 76ae0c18d7..cd4258f06a 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -13,6 +13,9 @@ const DefaultConfigTemplate = ` # Valid types are: json|struct|access_list|markdown tracer = "{{ .EVM.Tracer }}" +# MaxTxGasWanted defines the gas wanted for each eth tx returned in ante handler in check tx mode. +max-tx-gas-wanted = {{ .EVM.MaxTxGasWanted }} + ############################################################################### ### JSON RPC Configuration ### ############################################################################### diff --git a/server/flags/flags.go b/server/flags/flags.go index 11cf302e97..53b7ced3a8 100644 --- a/server/flags/flags.go +++ b/server/flags/flags.go @@ -48,7 +48,8 @@ const ( // EVM flags const ( - EVMTracer = "evm.tracer" + EVMTracer = "evm.tracer" + EVMMaxTxGasWanted = "evm.max-tx-gas-wanted" ) // TLS flags diff --git a/server/start.go b/server/start.go index f0e1549883..1a7db03df2 100644 --- a/server/start.go +++ b/server/start.go @@ -166,6 +166,7 @@ which accepts a path for the resulting pprof file. cmd.Flags().Int32(srvflags.JSONRPCBlockRangeCap, config.DefaultBlockRangeCap, "Sets the max block range allowed for `eth_getLogs` query") cmd.Flags().String(srvflags.EVMTracer, config.DefaultEVMTracer, "the EVM tracer type to collect execution traces from the EVM transaction execution (json|struct|access_list|markdown)") + cmd.Flags().Uint64(srvflags.EVMMaxTxGasWanted, config.DefaultMaxTxGasWanted, "the gas wanted for each eth tx returned in ante handler in check tx mode") cmd.Flags().String(srvflags.TLSCertPath, "", "the cert.pem file path for the server TLS configuration") cmd.Flags().String(srvflags.TLSKeyPath, "", "the key.pem file path for the server TLS configuration")