go-ethereum/core/txpool/validation_test.go
Daniel Liu bbfa8d9255 core, internal/ethapi: reject tx value and gas price above uint256 max
Raw RLP transactions can decode values larger than uint256, so reject them during txpool validation before they fall through to state-dependent checks.

Also return ErrUint256Overflow from state transition and map it to an invalid params RPC error so raw transaction submission reports a consistent parameter validation failure.

Add focused regression tests for txpool validation and eth_sendRawTransaction error handling.
2026-04-16 18:03:36 +08:00

153 lines
3.9 KiB
Go

// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package txpool
import (
"crypto/ecdsa"
"errors"
"math"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
)
func TestValidateTransactionEIP2681(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatal(err)
}
head := &types.Header{
Number: big.NewInt(1),
GasLimit: 5000000,
Time: 1,
Difficulty: big.NewInt(1),
}
signer := types.LatestSigner(params.TestChainConfig)
// Create validation options
opts := &ValidationOptions{
Config: params.TestChainConfig,
Accept: 0xFF, // Accept all transaction types
MaxSize: 32 * 1024,
MaxBlobCount: 6,
MinTip: big.NewInt(0),
}
tests := []struct {
name string
nonce uint64
wantErr error
}{
{
name: "normal nonce",
nonce: 42,
wantErr: nil,
},
{
name: "max allowed nonce (2^64-2)",
nonce: math.MaxUint64 - 1,
wantErr: nil,
},
{
name: "EIP-2681 nonce overflow (2^64-1)",
nonce: math.MaxUint64,
wantErr: core.ErrNonceMax,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tx := createTestTransaction(key, tt.nonce)
err := ValidateTransaction(tx, head, signer, opts)
if tt.wantErr == nil {
if err != nil {
t.Errorf("ValidateTransaction() error = %v, wantErr nil", err)
}
} else {
if err == nil {
t.Errorf("ValidateTransaction() error = nil, wantErr %v", tt.wantErr)
} else if !errors.Is(err, tt.wantErr) {
t.Errorf("ValidateTransaction() error = %v, wantErr %v", err, tt.wantErr)
}
}
})
}
}
func TestValidateTransactionValueOverflow(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatal(err)
}
head := &types.Header{
Number: big.NewInt(1),
GasLimit: 5000000,
Time: 1,
Difficulty: big.NewInt(1),
}
signer := types.LatestSigner(params.TestChainConfig)
opts := &ValidationOptions{
Config: params.TestChainConfig,
Accept: 0xFF,
MaxSize: 32 * 1024,
MaxBlobCount: 6,
MinTip: big.NewInt(0),
}
to := common.HexToAddress("0x0000000000000000000000000000000000000001")
overflowValue := new(big.Int).Lsh(big.NewInt(1), 256)
tx := types.NewTx(&types.LegacyTx{
Nonce: 0,
To: &to,
Value: overflowValue,
Gas: 21000,
GasPrice: big.NewInt(1),
})
tx, err = types.SignTx(tx, types.HomesteadSigner{}, key)
if err != nil {
t.Fatal(err)
}
err = ValidateTransaction(tx, head, signer, opts)
if !errors.Is(err, types.ErrUint256Overflow) {
t.Fatalf("expected %v, got %v", types.ErrUint256Overflow, err)
}
}
// createTestTransaction creates a basic transaction for testing
func createTestTransaction(key *ecdsa.PrivateKey, nonce uint64) *types.Transaction {
to := common.HexToAddress("0x0000000000000000000000000000000000000001")
txdata := &types.LegacyTx{
Nonce: nonce,
To: &to,
Value: big.NewInt(1000),
Gas: 21000,
GasPrice: big.NewInt(1),
Data: nil,
}
tx := types.NewTx(txdata)
signedTx, _ := types.SignTx(tx, types.HomesteadSigner{}, key)
return signedTx
}