core/rawdb: add test for HasCanonicalTransaction

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-11-28 15:41:26 +01:00
parent 8ae608bd16
commit 2c0f815ca9
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -89,6 +89,10 @@ func TestLookupStorage(t *testing.T) {
// Check that no transactions entries are in a pristine database // Check that no transactions entries are in a pristine database
for i, tx := range txs { 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 { if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil {
t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) 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) tc.writeTxLookupEntriesByBlock(db, block)
for i, tx := range txs { 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 { if txn, hash, number, index := ReadCanonicalTransaction(db, tx.Hash()); txn == nil {
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
} else { } else {
@ -113,10 +120,26 @@ func TestLookupStorage(t *testing.T) {
// Delete the transactions and check purge // Delete the transactions and check purge
for i, tx := range txs { for i, tx := range txs {
DeleteTxLookupEntry(db, tx.Hash()) 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 { if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil {
t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) 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)
}
}
}) })
} }
} }