Skip to content

Commit 3f86ba1

Browse files
jenikdwsodsong
authored andcommitted
RPC gas capping (#391)
* Fix rpc gas capping * Add test for gas capping when creating tx message
1 parent b9545c6 commit 3f86ba1

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

ethapi/transaction_args.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,22 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*
174174
// Set sender address or use zero address if none specified.
175175
addr := args.from()
176176

177-
// Set default gas & gas price if none were set
178-
gas := globalGasCap
179-
if gas == 0 {
180-
gas = uint64(math.MaxUint64 / 2)
177+
// Tosca uses int64 for gas so it needs to be limited by MaxInt64
178+
if globalGasCap == 0 || globalGasCap > math.MaxInt64 {
179+
globalGasCap = math.MaxInt64
181180
}
181+
182+
gas := globalGasCap
183+
182184
if args.Gas != nil {
183-
gas = uint64(*args.Gas)
184-
}
185-
if globalGasCap != 0 && globalGasCap < gas {
186-
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
187-
gas = globalGasCap
185+
argsGas := uint64(*args.Gas)
186+
if argsGas > gas {
187+
log.Warn("Caller gas above allowance, capping", "requested", argsGas, "cap", gas)
188+
} else {
189+
gas = argsGas
190+
}
188191
}
192+
189193
var (
190194
gasPrice *big.Int
191195
gasFeeCap *big.Int

ethapi/transaction_args_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package ethapi
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
"github.com/ethereum/go-ethereum/common/hexutil"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestGasCap(t *testing.T) {
12+
13+
var (
14+
gas10M = hexutil.Uint64(10_000_000)
15+
gasMaxUint64 = hexutil.Uint64(math.MaxUint64)
16+
)
17+
18+
tests := []struct {
19+
name string
20+
argGas *hexutil.Uint64
21+
globalGasCap uint64
22+
expectedGas uint64
23+
}{
24+
{
25+
name: "gas cap 0 and arg gas nil",
26+
globalGasCap: 0,
27+
argGas: nil,
28+
expectedGas: math.MaxInt64,
29+
}, {
30+
name: "gas cap 0 and arg gas 10M",
31+
globalGasCap: 0,
32+
argGas: &gas10M,
33+
expectedGas: 10_000_000,
34+
}, {
35+
name: "gas cap 0 and arg gas maxUint64",
36+
globalGasCap: 0,
37+
argGas: &gasMaxUint64,
38+
expectedGas: math.MaxInt64,
39+
}, {
40+
name: "gas cap 50M and arg gas nil",
41+
globalGasCap: 50_000_000,
42+
argGas: nil,
43+
expectedGas: 50_000_000,
44+
}, {
45+
name: "gas cap 50M and arg gas 10M",
46+
globalGasCap: 50_000_000,
47+
argGas: &gas10M,
48+
expectedGas: 10_000_000,
49+
}, {
50+
name: "gas cap 50M and arg gas maxUint64",
51+
globalGasCap: 50_000_000,
52+
argGas: &gasMaxUint64,
53+
expectedGas: 50_000_000,
54+
}, {
55+
name: "gas cap maxUint64 and arg gas 10M",
56+
globalGasCap: math.MaxUint64,
57+
argGas: &gas10M,
58+
expectedGas: 10_000_000,
59+
}, {
60+
name: "gas cap maxUint64 and arg gas maxUint64",
61+
globalGasCap: math.MaxUint64,
62+
argGas: &gasMaxUint64,
63+
expectedGas: math.MaxInt64,
64+
},
65+
}
66+
67+
for _, test := range tests {
68+
args := TransactionArgs{Gas: test.argGas}
69+
70+
msg, err := args.ToMessage(test.globalGasCap, nil)
71+
72+
require.Nil(t, err)
73+
require.Equal(t, test.expectedGas, msg.GasLimit, test.name)
74+
}
75+
}

0 commit comments

Comments
 (0)