diff --git a/CHANGELOG.md b/CHANGELOG.md index dc83c4f1fc..5e2d663460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (evm) [\#529](https://github.com/tharsis/ethermint/issues/529) support return value on trace tx response. * (rpc) [tharsis#1006](https://github.com/tharsis/ethermint/pull/1006) Use `string` as the parameters type to correct ambiguous results. * (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.7.2-cronos-6] - 2021-12-17 diff --git a/app/ante/ante.go b/app/ante/ante.go index 53e708682a..b25f2ee9cf 100644 --- a/app/ante/ante.go +++ b/app/ante/ante.go @@ -37,6 +37,7 @@ func NewAnteHandler( feeGrantKeeper authante.FeegrantKeeper, channelKeeper channelkeeper.Keeper, signModeHandler authsigning.SignModeHandler, + maxGasWanted uint64, ) sdk.AnteHandler { return func( ctx sdk.Context, tx sdk.Tx, sim bool, @@ -62,7 +63,7 @@ func NewAnteHandler( NewEthSigVerificationDecorator(evmKeeper), NewEthAccountVerificationDecorator(ak, bankKeeper, evmKeeper), NewEthNonceVerificationDecorator(ak), - NewEthGasConsumeDecorator(evmKeeper), + NewEthGasConsumeDecorator(evmKeeper, maxGasWanted), NewCanTransferDecorator(evmKeeper), NewEthIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator. ) diff --git a/app/ante/eth.go b/app/ante/eth.go index c9988c5d5f..a7dba7295c 100644 --- a/app/ante/eth.go +++ b/app/ante/eth.go @@ -237,13 +237,18 @@ func (nvd EthNonceVerificationDecorator) 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) EthGasConsumeDecorator { +func NewEthGasConsumeDecorator( + evmKeeper EVMKeeper, + maxGasWanted uint64, +) EthGasConsumeDecorator { return EthGasConsumeDecorator{ - evmKeeper: evmKeeper, + evmKeeper, + maxGasWanted, } } @@ -293,8 +298,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 cbae058a1b..6bfc763097 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" evmtypes "github.com/tharsis/ethermint/x/evm/types" @@ -195,7 +196,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/app.go b/app/app.go index 0563b0d958..a7326658e2 100644 --- a/app/app.go +++ b/app/app.go @@ -509,10 +509,11 @@ func NewEthermintApp( app.SetBeginBlocker(app.BeginBlocker) // use Ethermint's custom AnteHandler + maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)) app.SetAnteHandler( ante.NewAnteHandler( app.AccountKeeper, app.BankKeeper, app.EvmKeeper, app.FeeGrantKeeper, app.IBCKeeper.ChannelKeeper, - encodingConfig.TxConfig.SignModeHandler(), + encodingConfig.TxConfig.SignModeHandler(), maxGasWanted, ), ) diff --git a/server/config/config.go b/server/config/config.go index b2b152d49f..7b4475941b 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -27,6 +27,8 @@ const ( // DefaultEVMTracer is the default vm.Tracer type DefaultEVMTracer = "" + DefaultMaxTxGasWanted = 500000 + DefaultGasCap uint64 = 25000000 DefaultFilterCap int32 = 200 @@ -53,6 +55,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. @@ -131,7 +135,8 @@ func DefaultConfig() *Config { // DefaultEVMConfig returns the default EVM configuration func DefaultEVMConfig() *EVMConfig { return &EVMConfig{ - Tracer: DefaultEVMTracer, + Tracer: DefaultEVMTracer, + MaxTxGasWanted: DefaultMaxTxGasWanted, } } @@ -226,7 +231,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 5d63b1a549..3e047b0e34 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 653bf8af91..30edce6eb2 100644 --- a/server/flags/flags.go +++ b/server/flags/flags.go @@ -38,7 +38,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 863917e506..d536edf78b 100644 --- a/server/start.go +++ b/server/start.go @@ -161,6 +161,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")