From 2c0f815ca9892274824f1edbeff3eb2ed1cf9cb6 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Fri, 28 Nov 2025 15:41:26 +0100 Subject: [PATCH] core/rawdb: add test for HasCanonicalTransaction Signed-off-by: Csaba Kiraly --- core/rawdb/accessors_indexes_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/rawdb/accessors_indexes_test.go b/core/rawdb/accessors_indexes_test.go index a812fefeaa..c47516ea84 100644 --- a/core/rawdb/accessors_indexes_test.go +++ b/core/rawdb/accessors_indexes_test.go @@ -89,6 +89,10 @@ func TestLookupStorage(t *testing.T) { // Check that no transactions entries are in a pristine database for i, tx := range txs { + // Sometimes (but not always) check HasCanonicalTransaction as well + if i%2 == 0 && HasCanonicalTransaction(db, tx.Hash()) { + t.Fatalf("tx #%d [%x]: non existent transaction found", i, tx.Hash()) + } if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil { t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) } @@ -99,6 +103,9 @@ func TestLookupStorage(t *testing.T) { tc.writeTxLookupEntriesByBlock(db, block) for i, tx := range txs { + if i%2 == 0 && !HasCanonicalTransaction(db, tx.Hash()) { + t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) + } if txn, hash, number, index := ReadCanonicalTransaction(db, tx.Hash()); txn == nil { t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) } else { @@ -113,10 +120,26 @@ func TestLookupStorage(t *testing.T) { // Delete the transactions and check purge for i, tx := range txs { DeleteTxLookupEntry(db, tx.Hash()) + if i%2 == 0 && HasCanonicalTransaction(db, tx.Hash()) { + t.Fatalf("tx #%d [%x]: deleted transaction found", i, tx.Hash()) + } if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil { t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) } } + // Check some random hashes as well + for i := 0; i < 10; i++ { + var hash common.Hash + for j := 0; j < len(hash); j++ { + hash[j] = byte(i*13 + j*7) + } + if i%2 == 0 && HasCanonicalTransaction(db, hash) { + t.Fatalf("random tx %d [%x]: non existent transaction found", i, hash) + } + if txn, _, _, _ := ReadCanonicalTransaction(db, hash); txn != nil { + t.Fatalf("random tx %d [%x]: non existent transaction returned: %v", i, hash, txn) + } + } }) } }