From ea561962dfa24cbf9e3f867adcd9491c6316c3bd Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Tue, 10 Jun 2025 18:05:17 +0900 Subject: [PATCH 1/2] add max gas cap in eth_call --- x/evm/types/tx_args.go | 11 +++++++++- x/evm/types/tx_args_test.go | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/x/evm/types/tx_args.go b/x/evm/types/tx_args.go index dd7b6475e4..00c15b4f2d 100644 --- a/x/evm/types/tx_args.go +++ b/x/evm/types/tx_args.go @@ -133,13 +133,16 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (* return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") } + // Ethereum block size is ~36000000 + MaxGasCap := uint64(100000000) + // Set sender address or use zero address if none specified. addr := args.GetFrom() // Set default gas & gas price if none were set gas := globalGasCap if gas == 0 { - gas = uint64(math.MaxUint64 / 2) + gas = MaxGasCap } if args.Gas != nil { gas = uint64(*args.Gas) @@ -147,6 +150,7 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (* if globalGasCap != 0 && globalGasCap < gas { gas = globalGasCap } + var ( gasPrice *big.Int gasFeeCap *big.Int @@ -197,6 +201,11 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (* nonce = uint64(*args.Nonce) } + // Limit gas cap to avoid DOS during simulation + if gas > MaxGasCap { + gas = MaxGasCap + } + msg := &core.Message{ From: addr, To: args.To, diff --git a/x/evm/types/tx_args_test.go b/x/evm/types/tx_args_test.go index 9e8c20aa0b..fc97682b86 100644 --- a/x/evm/types/tx_args_test.go +++ b/x/evm/types/tx_args_test.go @@ -2,6 +2,7 @@ package types import ( "fmt" + "math" "math/big" "github.com/ethereum/go-ethereum/common" @@ -286,3 +287,45 @@ func (suite *TxDataTestSuite) TestGetData() { suite.Require().Equal(retrievedData, tc.expectedOutput) } } + +func (suite *TxDataTestSuite) TestMaxGasCap() { + testCases := []struct { + name string + globalGasCap uint64 + txArgs TransactionArgs + expectedOutput uint64 + }{ + { + "globalGasCap is below limit", + 25000000, + TransactionArgs{ + Gas: nil, + Input: nil, + }, + 25000000, + }, + { + "globalGasCap is above limit", + math.MaxInt64, + TransactionArgs{ + Gas: nil, + Input: nil, + }, + 100000000, + }, + { + "globalGasCap is zero", + 0, + TransactionArgs{ + Gas: nil, + Input: nil, + }, + 100000000, + }, + } + for _, tc := range testCases { + res, err := tc.txArgs.ToMessage(tc.globalGasCap, nil) + suite.Require().Nil(err) + suite.Require().Equal(res.GasLimit, tc.expectedOutput) + } +} From bf350e8c45ec3a559086513a36e95e0f9e83d1d1 Mon Sep 17 00:00:00 2001 From: Thomas Nguy Date: Wed, 11 Jun 2025 20:18:29 +0900 Subject: [PATCH 2/2] move logic --- x/evm/types/tx_args.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/x/evm/types/tx_args.go b/x/evm/types/tx_args.go index 00c15b4f2d..a97b6f73c3 100644 --- a/x/evm/types/tx_args.go +++ b/x/evm/types/tx_args.go @@ -133,18 +133,13 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (* return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") } - // Ethereum block size is ~36000000 - MaxGasCap := uint64(100000000) - // Set sender address or use zero address if none specified. addr := args.GetFrom() - + // Ethereum block size is ~36000000, we limit this value to protect against DOS + MaxGasCap := uint64(100000000) // Set default gas & gas price if none were set - gas := globalGasCap - if gas == 0 { - gas = MaxGasCap - } - if args.Gas != nil { + gas := MaxGasCap + if args.Gas != nil && uint64(*args.Gas) < MaxGasCap { gas = uint64(*args.Gas) } if globalGasCap != 0 && globalGasCap < gas { @@ -201,11 +196,6 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (* nonce = uint64(*args.Nonce) } - // Limit gas cap to avoid DOS during simulation - if gas > MaxGasCap { - gas = MaxGasCap - } - msg := &core.Message{ From: addr, To: args.To,