From afe965df355099c6cb1a670ec4697202fd6bad44 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Wed, 12 Mar 2025 09:51:33 +0100 Subject: [PATCH] deduplicate error checking code into helper --- cmd/workload/filtertest.go | 6 +++--- cmd/workload/historytest.go | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/workload/filtertest.go b/cmd/workload/filtertest.go index 79ed670b4c..f8ed29f3db 100644 --- a/cmd/workload/filtertest.go +++ b/cmd/workload/filtertest.go @@ -110,7 +110,7 @@ func (s *filterTestSuite) filterFullRange(t *utesting.T) { func (s *filterTestSuite) queryAndCheck(t *utesting.T, query *filterQuery) { query.run(s.cfg.client) if query.Err != nil { - if rpcErr := query.Err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(query.Err) { t.Logf("test case accessed pruned history") } 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) @@ -130,7 +130,7 @@ func (s *filterTestSuite) fullRangeQueryAndCheck(t *utesting.T, query *filterQue } frQuery.run(s.cfg.client) if frQuery.Err != nil { - if rpcErr := frQuery.Err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(frQuery.Err) { t.Logf("test case accessed pruned history") } t.Errorf("Full range filter query failed (addresses: %v topics: %v error: %v)", frQuery.Address, frQuery.Topics, frQuery.Err) @@ -214,7 +214,7 @@ func (fq *filterQuery) run(client *client) { }) if err != nil { fq.Err = err - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { return } fmt.Printf("Filter query failed: fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v\n", diff --git a/cmd/workload/historytest.go b/cmd/workload/historytest.go index e37bd78f27..63a9eee1f5 100644 --- a/cmd/workload/historytest.go +++ b/cmd/workload/historytest.go @@ -43,6 +43,17 @@ type historyTestSuite struct { 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 := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + return true + } + } + return false +} + func newHistoryTestSuite(cfg testConfig) *historyTestSuite { s := &historyTestSuite{cfg: cfg} if err := s.loadTests(); err != nil { @@ -133,7 +144,7 @@ func (s *historyTestSuite) testGetBlockByNumber(t *utesting.T) { bhash := s.tests.BlockHashes[i] b, err := s.cfg.client.getBlockByNumber(ctx, num, false) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -156,7 +167,7 @@ func (s *historyTestSuite) testGetBlockTransactionCountByHash(t *utesting.T) { bhash := s.tests.BlockHashes[i] count, err := s.cfg.client.getBlockTransactionCountByHash(ctx, bhash) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -176,7 +187,7 @@ func (s *historyTestSuite) testGetBlockTransactionCountByNumber(t *utesting.T) { bhash := s.tests.BlockHashes[i] count, err := s.cfg.client.getBlockTransactionCountByNumber(ctx, num) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -196,7 +207,7 @@ func (s *historyTestSuite) testGetBlockReceiptsByHash(t *utesting.T) { bhash := s.tests.BlockHashes[i] receipts, err := s.cfg.client.getBlockReceipts(ctx, bhash) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -217,7 +228,7 @@ func (s *historyTestSuite) testGetBlockReceiptsByNumber(t *utesting.T) { bhash := s.tests.BlockHashes[i] receipts, err := s.cfg.client.getBlockReceipts(ctx, hexutil.Uint64(num)) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -244,7 +255,7 @@ func (s *historyTestSuite) testGetTransactionByBlockHashAndIndex(t *utesting.T) tx, err := s.cfg.client.getTransactionByBlockHashAndIndex(ctx, bhash, uint64(txIndex)) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue } @@ -273,7 +284,7 @@ func (s *historyTestSuite) testGetTransactionByBlockNumberAndIndex(t *utesting.T tx, err := s.cfg.client.getTransactionByBlockNumberAndIndex(ctx, num, uint64(txIndex)) if err != nil { - if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { + if errIsPrunedHistory(err) { t.Logf("test case accessed pruned history") continue }