Skip to content

Commit ea56196

Browse files
committed
add max gas cap in eth_call
1 parent 434f46d commit ea56196

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

x/evm/types/tx_args.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,24 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*
133133
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
134134
}
135135

136+
// Ethereum block size is ~36000000
137+
MaxGasCap := uint64(100000000)
138+
136139
// Set sender address or use zero address if none specified.
137140
addr := args.GetFrom()
138141

139142
// Set default gas & gas price if none were set
140143
gas := globalGasCap
141144
if gas == 0 {
142-
gas = uint64(math.MaxUint64 / 2)
145+
gas = MaxGasCap
143146
}
144147
if args.Gas != nil {
145148
gas = uint64(*args.Gas)
146149
}
147150
if globalGasCap != 0 && globalGasCap < gas {
148151
gas = globalGasCap
149152
}
153+
150154
var (
151155
gasPrice *big.Int
152156
gasFeeCap *big.Int
@@ -197,6 +201,11 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*
197201
nonce = uint64(*args.Nonce)
198202
}
199203

204+
// Limit gas cap to avoid DOS during simulation
205+
if gas > MaxGasCap {
206+
gas = MaxGasCap
207+
}
208+
200209
msg := &core.Message{
201210
From: addr,
202211
To: args.To,

x/evm/types/tx_args_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"fmt"
5+
"math"
56
"math/big"
67

78
"github.com/ethereum/go-ethereum/common"
@@ -286,3 +287,45 @@ func (suite *TxDataTestSuite) TestGetData() {
286287
suite.Require().Equal(retrievedData, tc.expectedOutput)
287288
}
288289
}
290+
291+
func (suite *TxDataTestSuite) TestMaxGasCap() {
292+
testCases := []struct {
293+
name string
294+
globalGasCap uint64
295+
txArgs TransactionArgs
296+
expectedOutput uint64
297+
}{
298+
{
299+
"globalGasCap is below limit",
300+
25000000,
301+
TransactionArgs{
302+
Gas: nil,
303+
Input: nil,
304+
},
305+
25000000,
306+
},
307+
{
308+
"globalGasCap is above limit",
309+
math.MaxInt64,
310+
TransactionArgs{
311+
Gas: nil,
312+
Input: nil,
313+
},
314+
100000000,
315+
},
316+
{
317+
"globalGasCap is zero",
318+
0,
319+
TransactionArgs{
320+
Gas: nil,
321+
Input: nil,
322+
},
323+
100000000,
324+
},
325+
}
326+
for _, tc := range testCases {
327+
res, err := tc.txArgs.ToMessage(tc.globalGasCap, nil)
328+
suite.Require().Nil(err)
329+
suite.Require().Equal(res.GasLimit, tc.expectedOutput)
330+
}
331+
}

0 commit comments

Comments
 (0)