internal/ethapi: ethSendTransaction check baseFee (#27834)

If the EIP-1559 is activated, reject 0-priced transactions in the rpc level
This commit is contained in:
Delweng 2023-12-19 03:09:41 +08:00 committed by GitHub
parent 4410c1416a
commit 54a400ee71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 9 deletions

View File

@ -137,20 +137,35 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
} }
// If the tx has completely specified a fee mechanism, no default is needed. This allows users // If the tx has completely specified a fee mechanism, no default is needed.
// who are not yet synced past London to get defaults for other tx values. See // This allows users who are not yet synced past London to get defaults for
// https://github.com/ethereum/go-ethereum/pull/23274 for more information. // other tx values. See https://github.com/ethereum/go-ethereum/pull/23274
// for more information.
eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil
if (args.GasPrice != nil && !eip1559ParamsSet) || (args.GasPrice == nil && eip1559ParamsSet) {
// Sanity check the EIP-1559 fee parameters if present. // Sanity check the EIP-1559 fee parameters if present.
if args.GasPrice == nil && args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { if args.GasPrice == nil && eip1559ParamsSet {
if args.MaxFeePerGas.ToInt().Sign() == 0 {
return errors.New("maxFeePerGas must be non-zero")
}
if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 {
return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas)
} }
return nil return nil // No need to set anything, user already set MaxFeePerGas and MaxPriorityFeePerGas
} }
// Now attempt to fill in default value depending on whether London is active or not. // Sanity check the non-EIP-1559 fee parameters.
head := b.CurrentHeader() head := b.CurrentHeader()
if b.ChainConfig().IsLondon(head.Number) { isLondon := b.ChainConfig().IsLondon(head.Number)
if args.GasPrice != nil && !eip1559ParamsSet {
// Zero gas-price is not allowed after London fork
if args.GasPrice.ToInt().Sign() == 0 && isLondon {
return errors.New("gasPrice must be non-zero after london fork")
}
return nil // No need to set anything, user already set GasPrice
}
// Now attempt to fill in default value depending on whether London is active or not.
if isLondon {
// London is active, set maxPriorityFeePerGas and maxFeePerGas. // London is active, set maxPriorityFeePerGas and maxFeePerGas.
if err := args.setLondonFeeDefaults(ctx, head, b); err != nil { if err := args.setLondonFeeDefaults(ctx, head, b); err != nil {
return err return err

View File

@ -52,6 +52,7 @@ func TestSetFeeDefaults(t *testing.T) {
var ( var (
b = newBackendMock() b = newBackendMock()
zero = (*hexutil.Big)(big.NewInt(0))
fortytwo = (*hexutil.Big)(big.NewInt(42)) fortytwo = (*hexutil.Big)(big.NewInt(42))
maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt())) maxFee = (*hexutil.Big)(new(big.Int).Add(new(big.Int).Mul(b.current.BaseFee, big.NewInt(2)), fortytwo.ToInt()))
al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}} al = &types.AccessList{types.AccessTuple{Address: common.Address{0xaa}, StorageKeys: []common.Hash{{0x01}}}}
@ -66,6 +67,13 @@ func TestSetFeeDefaults(t *testing.T) {
&TransactionArgs{GasPrice: fortytwo}, &TransactionArgs{GasPrice: fortytwo},
nil, nil,
}, },
{
"legacy tx pre-London with zero price",
false,
&TransactionArgs{GasPrice: zero},
&TransactionArgs{GasPrice: zero},
nil,
},
{ {
"legacy tx post-London, explicit gas price", "legacy tx post-London, explicit gas price",
true, true,
@ -73,6 +81,13 @@ func TestSetFeeDefaults(t *testing.T) {
&TransactionArgs{GasPrice: fortytwo}, &TransactionArgs{GasPrice: fortytwo},
nil, nil,
}, },
{
"legacy tx post-London with zero price",
true,
&TransactionArgs{GasPrice: zero},
nil,
errors.New("gasPrice must be non-zero after london fork"),
},
// Access list txs // Access list txs
{ {
@ -161,6 +176,13 @@ func TestSetFeeDefaults(t *testing.T) {
nil, nil,
errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"), errors.New("maxFeePerGas (0x7) < maxPriorityFeePerGas (0x2a)"),
}, },
{
"dynamic fee tx post-London, explicit gas price",
true,
&TransactionArgs{MaxFeePerGas: zero, MaxPriorityFeePerGas: zero},
nil,
errors.New("maxFeePerGas must be non-zero"),
},
// Misc // Misc
{ {