Skip to content

Commit 29d719d

Browse files
committed
core/rawdb: add test for HasCanonicalTransaction
Signed-off-by: Csaba Kiraly <[email protected]>
1 parent b684d5f commit 29d719d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

core/rawdb/accessors_indexes_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ func TestLookupStorage(t *testing.T) {
8989

9090
// Check that no transactions entries are in a pristine database
9191
for i, tx := range txs {
92+
// Sometimes (but not always) check HasCanonicalTransaction as well
93+
if i%2 == 0 && HasCanonicalTransaction(db, tx.Hash()) {
94+
t.Fatalf("tx #%d [%x]: non existent transaction found", i, tx.Hash())
95+
}
9296
if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil {
9397
t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
9498
}
@@ -99,6 +103,9 @@ func TestLookupStorage(t *testing.T) {
99103
tc.writeTxLookupEntriesByBlock(db, block)
100104

101105
for i, tx := range txs {
106+
if i%2 == 0 && !HasCanonicalTransaction(db, tx.Hash()) {
107+
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
108+
}
102109
if txn, hash, number, index := ReadCanonicalTransaction(db, tx.Hash()); txn == nil {
103110
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
104111
} else {
@@ -113,10 +120,26 @@ func TestLookupStorage(t *testing.T) {
113120
// Delete the transactions and check purge
114121
for i, tx := range txs {
115122
DeleteTxLookupEntry(db, tx.Hash())
123+
if i%2 == 0 && HasCanonicalTransaction(db, tx.Hash()) {
124+
t.Fatalf("tx #%d [%x]: deleted transaction found", i, tx.Hash())
125+
}
116126
if txn, _, _, _ := ReadCanonicalTransaction(db, tx.Hash()); txn != nil {
117127
t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
118128
}
119129
}
130+
// Check some random hashes as well
131+
for i := 0; i < 10; i++ {
132+
var hash common.Hash
133+
for j := 0; j < len(hash); j++ {
134+
hash[j] = byte(i*13 + j*7)
135+
}
136+
if i%2 == 0 && HasCanonicalTransaction(db, hash) {
137+
t.Fatalf("random tx %d [%x]: non existent transaction found", i, hash)
138+
}
139+
if txn, _, _, _ := ReadCanonicalTransaction(db, hash); txn != nil {
140+
t.Fatalf("random tx %d [%x]: non existent transaction returned: %v", i, hash, txn)
141+
}
142+
}
120143
})
121144
}
122145
}

0 commit comments

Comments
 (0)