cmd/workload: check if history prune error is returned beyond the pruning threshold. if so, it is an error

This commit is contained in:
Jared Wasinger 2025-03-13 09:45:41 +01:00 committed by Felix Lange
parent 04ca215ddd
commit a73100200a
3 changed files with 59 additions and 38 deletions

View file

@ -108,11 +108,9 @@ func (s *filterTestSuite) filterFullRange(t *utesting.T) {
} }
func (s *filterTestSuite) queryAndCheck(t *utesting.T, query *filterQuery) { func (s *filterTestSuite) queryAndCheck(t *utesting.T, query *filterQuery) {
query.run(s.cfg.client) query.run(s.cfg)
if query.Err != nil { if query.Err != nil {
if !errIsPrunedHistory(query.Err) {
t.Errorf("Filter query failed (fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v)", query.FromBlock, query.ToBlock, query.Address, query.Topics, query.Err) t.Errorf("Filter query failed (fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v)", query.FromBlock, query.ToBlock, query.Address, query.Topics, query.Err)
}
return return
} }
if *query.ResultHash != query.calculateHash() { if *query.ResultHash != query.calculateHash() {
@ -127,11 +125,9 @@ func (s *filterTestSuite) fullRangeQueryAndCheck(t *utesting.T, query *filterQue
Address: query.Address, Address: query.Address,
Topics: query.Topics, Topics: query.Topics,
} }
frQuery.run(s.cfg.client) frQuery.run(s.cfg)
if frQuery.Err != nil { if frQuery.Err != nil {
if !errIsPrunedHistory(frQuery.Err) {
t.Errorf("Full range filter query failed (addresses: %v topics: %v error: %v)", frQuery.Address, frQuery.Topics, frQuery.Err) t.Errorf("Full range filter query failed (addresses: %v topics: %v error: %v)", frQuery.Address, frQuery.Topics, frQuery.Err)
}
return return
} }
// filter out results outside the original query range // filter out results outside the original query range
@ -201,10 +197,10 @@ func (fq *filterQuery) calculateHash() common.Hash {
return crypto.Keccak256Hash(enc) return crypto.Keccak256Hash(enc)
} }
func (fq *filterQuery) run(client *client) { func (fq *filterQuery) run(cfg testConfig) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel() defer cancel()
logs, err := client.Eth.FilterLogs(ctx, ethereum.FilterQuery{ logs, err := cfg.client.Eth.FilterLogs(ctx, ethereum.FilterQuery{
FromBlock: big.NewInt(fq.FromBlock), FromBlock: big.NewInt(fq.FromBlock),
ToBlock: big.NewInt(fq.ToBlock), ToBlock: big.NewInt(fq.ToBlock),
Addresses: fq.Address, Addresses: fq.Address,
@ -212,12 +208,12 @@ func (fq *filterQuery) run(client *client) {
}) })
if err != nil { if err != nil {
fq.Err = err fq.Err = err
if errIsPrunedHistory(err) { err := validateHistoryPruneErr(fq.Err, uint64(fq.FromBlock), cfg)
return if err != nil {
}
fmt.Printf("Filter query failed: fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v\n", fmt.Printf("Filter query failed: fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v\n",
fq.FromBlock, fq.ToBlock, fq.Address, fq.Topics, err) fq.FromBlock, fq.ToBlock, fq.Address, fq.Topics, err)
return return
} }
}
fq.results = logs fq.results = logs
} }

View file

@ -25,7 +25,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/utesting" "github.com/ethereum/go-ethereum/internal/utesting"
"github.com/ethereum/go-ethereum/rpc"
) )
// historyTest is the content of a history test. // historyTest is the content of a history test.
@ -43,17 +42,6 @@ type historyTestSuite struct {
tests historyTest tests historyTest
} }
// errIsPrunedHistory returns true if an error is an rpc.Error with an error
// code of 4444.
func errIsPrunedHistory(err error) bool {
if err != nil {
if rpcErr, ok := err.(rpc.Error); ok && rpcErr.ErrorCode() == 4444 {
return true
}
}
return false
}
func newHistoryTestSuite(cfg testConfig) *historyTestSuite { func newHistoryTestSuite(cfg testConfig) *historyTestSuite {
s := &historyTestSuite{cfg: cfg} s := &historyTestSuite{cfg: cfg}
if err := s.loadTests(); err != nil { if err := s.loadTests(); err != nil {
@ -121,7 +109,8 @@ func (s *historyTestSuite) testGetBlockByHash(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
b, err := s.cfg.client.getBlockByHash(ctx, bhash, false) b, err := s.cfg.client.getBlockByHash(ctx, bhash, false)
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -143,7 +132,8 @@ func (s *historyTestSuite) testGetBlockByNumber(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
b, err := s.cfg.client.getBlockByNumber(ctx, num, false) b, err := s.cfg.client.getBlockByNumber(ctx, num, false)
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -165,7 +155,8 @@ func (s *historyTestSuite) testGetBlockTransactionCountByHash(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
count, err := s.cfg.client.getBlockTransactionCountByHash(ctx, bhash) count, err := s.cfg.client.getBlockTransactionCountByHash(ctx, bhash)
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -184,7 +175,8 @@ func (s *historyTestSuite) testGetBlockTransactionCountByNumber(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
count, err := s.cfg.client.getBlockTransactionCountByNumber(ctx, num) count, err := s.cfg.client.getBlockTransactionCountByNumber(ctx, num)
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -203,7 +195,8 @@ func (s *historyTestSuite) testGetBlockReceiptsByHash(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
receipts, err := s.cfg.client.getBlockReceipts(ctx, bhash) receipts, err := s.cfg.client.getBlockReceipts(ctx, bhash)
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -223,7 +216,8 @@ func (s *historyTestSuite) testGetBlockReceiptsByNumber(t *utesting.T) {
bhash := s.tests.BlockHashes[i] bhash := s.tests.BlockHashes[i]
receipts, err := s.cfg.client.getBlockReceipts(ctx, hexutil.Uint64(num)) receipts, err := s.cfg.client.getBlockReceipts(ctx, hexutil.Uint64(num))
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -249,7 +243,8 @@ func (s *historyTestSuite) testGetTransactionByBlockHashAndIndex(t *utesting.T)
tx, err := s.cfg.client.getTransactionByBlockHashAndIndex(ctx, bhash, uint64(txIndex)) tx, err := s.cfg.client.getTransactionByBlockHashAndIndex(ctx, bhash, uint64(txIndex))
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)
@ -277,7 +272,8 @@ func (s *historyTestSuite) testGetTransactionByBlockNumberAndIndex(t *utesting.T
tx, err := s.cfg.client.getTransactionByBlockNumberAndIndex(ctx, num, uint64(txIndex)) tx, err := s.cfg.client.getTransactionByBlockNumberAndIndex(ctx, num, uint64(txIndex))
if err != nil { if err != nil {
if errIsPrunedHistory(err) { err = validateHistoryPruneErr(err, num, s.cfg)
if err != nil {
continue continue
} }
t.Fatalf("block %d (hash %v): error %v", num, bhash, err) t.Fatalf("block %d (hash %v): error %v", num, bhash, err)

View file

@ -18,6 +18,8 @@ package main
import ( import (
"embed" "embed"
"fmt"
"github.com/ethereum/go-ethereum/rpc"
"io/fs" "io/fs"
"os" "os"
"slices" "slices"
@ -75,12 +77,35 @@ var (
} }
) )
const (
sepoliaMergeBlock uint64 = 1450409
mainnetMergeBlock uint64 = 15537393
)
// testConfig holds the parameters for testing. // testConfig holds the parameters for testing.
type testConfig struct { type testConfig struct {
client *client client *client
fsys fs.FS fsys fs.FS
filterQueryFile string filterQueryFile string
historyTestFile string historyTestFile string
historyPruneBlock *uint64
}
// validateHistoryPruneErr checks whether the given error is caused by access
// to history before the pruning threshold block (it is an rpc.Error with code 4444).
// If the error is of a different type, it is returned. If the error is a pruned
// history error occurring past the pruning threshold, an error is returned.
// Otherwise, nil is returned.
func validateHistoryPruneErr(err error, blockNum uint64, cfg testConfig) error {
if err != nil {
if rpcErr, ok := err.(rpc.Error); ok && rpcErr.ErrorCode() == 4444 {
if cfg.historyPruneBlock != nil && blockNum > *cfg.historyPruneBlock {
return fmt.Errorf("pruned history error returned after pruning threshold")
}
return nil
}
}
return err
} }
func testConfigFromCLI(ctx *cli.Context) (cfg testConfig) { func testConfigFromCLI(ctx *cli.Context) (cfg testConfig) {
@ -98,10 +123,14 @@ func testConfigFromCLI(ctx *cli.Context) (cfg testConfig) {
cfg.fsys = builtinTestFiles cfg.fsys = builtinTestFiles
cfg.filterQueryFile = "queries/filter_queries_mainnet.json" cfg.filterQueryFile = "queries/filter_queries_mainnet.json"
cfg.historyTestFile = "queries/history_mainnet.json" cfg.historyTestFile = "queries/history_mainnet.json"
cfg.historyPruneBlock = new(uint64)
*cfg.historyPruneBlock = mainnetMergeBlock
case ctx.Bool(testSepoliaFlag.Name): case ctx.Bool(testSepoliaFlag.Name):
cfg.fsys = builtinTestFiles cfg.fsys = builtinTestFiles
cfg.filterQueryFile = "queries/filter_queries_sepolia.json" cfg.filterQueryFile = "queries/filter_queries_sepolia.json"
cfg.historyTestFile = "queries/history_sepolia.json" cfg.historyTestFile = "queries/history_sepolia.json"
cfg.historyPruneBlock = new(uint64)
*cfg.historyPruneBlock = sepoliaMergeBlock
default: default:
cfg.fsys = os.DirFS(".") cfg.fsys = os.DirFS(".")
cfg.filterQueryFile = ctx.String(filterQueryFileFlag.Name) cfg.filterQueryFile = ctx.String(filterQueryFileFlag.Name)