Skip to content

Commit 54a400e

Browse files
authored
internal/ethapi: ethSendTransaction check baseFee (#27834)
If the EIP-1559 is activated, reject 0-priced transactions in the rpc level
1 parent 4410c14 commit 54a400e

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

internal/ethapi/transaction_args.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,35 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
137137
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
138138
return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
139139
}
140-
// If the tx has completely specified a fee mechanism, no default is needed. This allows users
141-
// who are not yet synced past London to get defaults for other tx values. See
142-
// https://github.com/ethereum/go-ethereum/pull/23274 for more information.
140+
// If the tx has completely specified a fee mechanism, no default is needed.
141+
// This allows users who are not yet synced past London to get defaults for
142+
// other tx values. See https://github.com/ethereum/go-ethereum/pull/23274
143+
// for more information.
143144
eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil
144-
if (args.GasPrice != nil && !eip1559ParamsSet) || (args.GasPrice == nil && eip1559ParamsSet) {
145-
// Sanity check the EIP-1559 fee parameters if present.
146-
if args.GasPrice == nil && args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 {
145+
146+
// Sanity check the EIP-1559 fee parameters if present.
147+
if args.GasPrice == nil && eip1559ParamsSet {
148+
if args.MaxFeePerGas.ToInt().Sign() == 0 {
149+
return errors.New("maxFeePerGas must be non-zero")
150+
}
151+
if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 {
147152
return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas)
148153
}
149-
return nil
154+
return nil // No need to set anything, user already set MaxFeePerGas and MaxPriorityFeePerGas
150155
}
151-
// Now attempt to fill in default value depending on whether London is active or not.
156+
// Sanity check the non-EIP-1559 fee parameters.
152157
head := b.CurrentHeader()
153-
if b.ChainConfig().IsLondon(head.Number) {
158+
isLondon := b.ChainConfig().IsLondon(head.Number)
159+
if args.GasPrice != nil && !eip1559ParamsSet {
160+
// Zero gas-price is not allowed after London fork
161+
if args.GasPrice.ToInt().Sign() == 0 && isLondon {
162+
return errors.New("gasPrice must be non-zero after london fork")
163+
}
164+
return nil // No need to set anything, user already set GasPrice
165+
}
166+
167+
// Now attempt to fill in default value depending on whether London is active or not.
168+
if isLondon {
154169
// London is active, set maxPriorityFeePerGas and maxFeePerGas.
155170
if err := args.setLondonFeeDefaults(ctx, head, b); err != nil {
156171
return err

internal/ethapi/transaction_args_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestSetFeeDefaults(t *testing.T) {
5252

5353
var (
5454
b = newBackendMock()
55+
zero = (*hexutil.Big)(big.NewInt(0))
5556
fortytwo = (*hexutil.Big)(big.NewInt(42))
5657
maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt()))
5758
al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}}
@@ -66,13 +67,27 @@ func TestSetFeeDefaults(t *testing.T) {
6667
&TransactionArgs{GasPrice: fortytwo},
6768
nil,
6869
},
70+
{
71+
"legacy tx pre-London with zero price",
72+
false,
73+
&TransactionArgs{GasPrice: zero},
74+
&TransactionArgs{GasPrice: zero},
75+
nil,
76+
},
6977
{
7078
"legacy tx post-London, explicit gas price",
7179
true,
7280
&TransactionArgs{GasPrice: fortytwo},
7381
&TransactionArgs{GasPrice: fortytwo},
7482
nil,
7583
},
84+
{
85+
"legacy tx post-London with zero price",
86+
true,
87+
&TransactionArgs{GasPrice: zero},
88+
nil,
89+
errors.New("gasPrice must be non-zero after london fork"),
90+
},
7691

7792
// Access list txs
7893
{
@@ -161,6 +176,13 @@ func TestSetFeeDefaults(t *testing.T) {
161176
nil,
162177
errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"),
163178
},
179+
{
180+
"dynamic fee tx post-London, explicit gas price",
181+
true,
182+
&TransactionArgs{MaxFeePerGas: zero, MaxPriorityFeePerGas: zero},
183+
nil,
184+
errors.New("maxFeePerGas must be non-zero"),
185+
},
164186

165187
// Misc
166188
{

0 commit comments

Comments
 (0)