deduplicate error checking code into helper

This commit is contained in:
Jared Wasinger 2025-03-12 09:51:33 +01:00 committed by Felix Lange
parent 7e95cedf31
commit afe965df35
2 changed files with 21 additions and 10 deletions

View file

@ -110,7 +110,7 @@ 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.client)
if query.Err != nil { 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.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) 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) frQuery.run(s.cfg.client)
if frQuery.Err != nil { 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.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) 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 { if err != nil {
fq.Err = err fq.Err = err
if rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
return return
} }
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",

View file

@ -43,6 +43,17 @@ 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 := err.(rpc.Error); rpcErr != nil && 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 {
@ -133,7 +144,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -156,7 +167,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -176,7 +187,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -196,7 +207,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -217,7 +228,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -244,7 +255,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }
@ -273,7 +284,7 @@ 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 rpcErr := err.(rpc.Error); rpcErr != nil && rpcErr.ErrorCode() == 4444 { if errIsPrunedHistory(err) {
t.Logf("test case accessed pruned history") t.Logf("test case accessed pruned history")
continue continue
} }