Skip to content

Commit f4606c5

Browse files
committed
make gas limit consistant
1 parent 9209206 commit f4606c5

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

x/evm/types/tx_args.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ package types
1818
import (
1919
"errors"
2020
"fmt"
21-
"math"
2221
"math/big"
2322

24-
"github.com/ethereum/go-ethereum/log"
25-
2623
"github.com/ethereum/go-ethereum/common"
2724
"github.com/ethereum/go-ethereum/common/hexutil"
2825
"github.com/ethereum/go-ethereum/core"
@@ -31,6 +28,9 @@ import (
3128
"github.com/holiman/uint256"
3229
)
3330

31+
// Ethereum block size is ~36000000, we use this value as default in case neither gas or global cap is specified, to protect against DOS
32+
const defaultMaxGas = uint64(100000000)
33+
3434
// TransactionArgs represents the arguments to construct a new transaction
3535
// or a message call using JSON-RPC.
3636
// Duplicate struct definition since geth struct is in internal package
@@ -158,15 +158,7 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*
158158

159159
// Set sender address or use zero address if none specified.
160160
addr := args.GetFrom()
161-
// Ethereum block size is ~36000000, we use this value as default in case neither gas or global cap is specified, to protect against DOS
162-
defaultGas := uint64(100000000)
163-
gas := defaultGas
164-
if args.Gas != nil {
165-
gas = uint64(*args.Gas)
166-
}
167-
if globalGasCap != 0 && globalGasCap < gas {
168-
gas = globalGasCap
169-
}
161+
gas := computeGasLimit(args.Gas, globalGasCap)
170162

171163
var (
172164
gasPrice *big.Int
@@ -269,16 +261,6 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
269261
return fmt.Errorf("chainId does not match node's (have=%v, want=%v)", have, chainID)
270262
}
271263
}
272-
if args.Gas == nil {
273-
gas := globalGasCap
274-
if gas == 0 {
275-
gas = uint64(math.MaxUint64 / 2)
276-
}
277-
args.Gas = (*hexutil.Uint64)(&gas)
278-
} else if globalGasCap > 0 && globalGasCap < uint64(*args.Gas) {
279-
log.Warn("Caller gas above allowance, capping", "requested", args.Gas, "cap", globalGasCap)
280-
args.Gas = (*hexutil.Uint64)(&globalGasCap)
281-
}
282264
if args.Nonce == nil {
283265
args.Nonce = new(hexutil.Uint64)
284266
}
@@ -299,6 +281,20 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
299281
args.MaxPriorityFeePerGas = new(hexutil.Big)
300282
}
301283
}
284+
gas := computeGasLimit(args.Gas, globalGasCap)
285+
args.Gas = (*hexutil.Uint64)(&gas)
302286

303287
return nil
304288
}
289+
290+
func computeGasLimit(argGas *hexutil.Uint64, globalGasCap uint64) uint64 {
291+
gas := defaultMaxGas
292+
if argGas != nil {
293+
gas = uint64(*argGas)
294+
}
295+
if globalGasCap != 0 && globalGasCap < gas {
296+
gas = globalGasCap
297+
}
298+
299+
return gas
300+
}

0 commit comments

Comments
 (0)