Skip to content

Commit 0185ee0

Browse files
authored
core/rawdb: single point of maintenance for writing and deleting tx lookup indexes (#21480)
1 parent 4764b2f commit 0185ee0

5 files changed

Lines changed: 31 additions & 29 deletions

File tree

‎core/blockchain.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block) {
712712
// Add the block to the canonical chain number scheme and mark as the head
713713
batch := bc.db.NewBatch()
714714
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
715-
rawdb.WriteTxLookupEntries(batch, block)
715+
rawdb.WriteTxLookupEntriesByBlock(batch, block)
716716
rawdb.WriteHeadBlockHash(batch, block.Hash())
717717

718718
// If the block is better than our head or is on a different chain, force update heads
@@ -1217,9 +1217,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
12171217
// range. In this case, all tx indices of newly imported blocks should be
12181218
// generated.
12191219
if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit {
1220-
rawdb.WriteTxLookupEntries(batch, block)
1220+
rawdb.WriteTxLookupEntriesByBlock(batch, block)
12211221
} else if rawdb.ReadTxIndexTail(bc.db) != nil {
1222-
rawdb.WriteTxLookupEntries(batch, block)
1222+
rawdb.WriteTxLookupEntriesByBlock(batch, block)
12231223
}
12241224
stats.processed++
12251225
}
@@ -1303,7 +1303,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
13031303
// Write all the data out into the database
13041304
rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())
13051305
rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receiptChain[i])
1306-
rawdb.WriteTxLookupEntries(batch, block) // Always write tx indices for live blocks, we assume they are needed
1306+
rawdb.WriteTxLookupEntriesByBlock(batch, block) // Always write tx indices for live blocks, we assume they are needed
13071307

13081308
// Write everything belongs to the blocks into the database. So that
13091309
// we can ensure all components of body is completed(body, receipts,

‎core/rawdb/accessors_indexes.go‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,29 @@ func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
5353
return &entry.BlockIndex
5454
}
5555

56-
// WriteTxLookupEntries stores a positional metadata for every transaction from
57-
// a block, enabling hash based transaction and receipt lookups.
58-
func WriteTxLookupEntries(db ethdb.KeyValueWriter, block *types.Block) {
59-
number := block.Number().Bytes()
60-
for _, tx := range block.Transactions() {
61-
if err := db.Put(txLookupKey(tx.Hash()), number); err != nil {
62-
log.Crit("Failed to store transaction lookup entry", "err", err)
63-
}
56+
// writeTxLookupEntry stores a positional metadata for a transaction,
57+
// enabling hash based transaction and receipt lookups.
58+
func writeTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash, numberBytes []byte) {
59+
if err := db.Put(txLookupKey(hash), numberBytes); err != nil {
60+
log.Crit("Failed to store transaction lookup entry", "err", err)
6461
}
6562
}
6663

67-
// WriteTxLookupEntriesByHash is identical to WriteTxLookupEntries, but does not
68-
// require a full types.Block as input.
69-
func WriteTxLookupEntriesByHash(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) {
64+
// WriteTxLookupEntries is identical to WriteTxLookupEntry, but it works on
65+
// a list of hashes
66+
func WriteTxLookupEntries(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) {
7067
numberBytes := new(big.Int).SetUint64(number).Bytes()
7168
for _, hash := range hashes {
72-
if err := db.Put(txLookupKey(hash), numberBytes); err != nil {
73-
log.Crit("Failed to store transaction lookup entry", "err", err)
74-
}
69+
writeTxLookupEntry(db, hash, numberBytes)
70+
}
71+
}
72+
73+
// WriteTxLookupEntriesByBlock stores a positional metadata for every transaction from
74+
// a block, enabling hash based transaction and receipt lookups.
75+
func WriteTxLookupEntriesByBlock(db ethdb.KeyValueWriter, block *types.Block) {
76+
numberBytes := block.Number().Bytes()
77+
for _, tx := range block.Transactions() {
78+
writeTxLookupEntry(db, tx.Hash(), numberBytes)
7579
}
7680
}
7781

@@ -83,11 +87,9 @@ func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
8387
}
8488

8589
// DeleteTxLookupEntries removes all transaction lookups for a given block.
86-
func DeleteTxLookupEntriesByHash(db ethdb.KeyValueWriter, hashes []common.Hash) {
90+
func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) {
8791
for _, hash := range hashes {
88-
if err := db.Delete(txLookupKey(hash)); err != nil {
89-
log.Crit("Failed to delete transaction lookup entry", "err", err)
90-
}
92+
DeleteTxLookupEntry(db, hash)
9193
}
9294
}
9395

‎core/rawdb/accessors_indexes_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func (h *testHasher) Hash() common.Hash {
5757
// Tests that positional lookup metadata can be stored and retrieved.
5858
func TestLookupStorage(t *testing.T) {
5959
tests := []struct {
60-
name string
61-
writeTxLookupEntries func(ethdb.Writer, *types.Block)
60+
name string
61+
writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block)
6262
}{
6363
{
6464
"DatabaseV6",
6565
func(db ethdb.Writer, block *types.Block) {
66-
WriteTxLookupEntries(db, block)
66+
WriteTxLookupEntriesByBlock(db, block)
6767
},
6868
},
6969
{
@@ -110,7 +110,7 @@ func TestLookupStorage(t *testing.T) {
110110
// Insert all the transactions into the database, and verify contents
111111
WriteCanonicalHash(db, block.Hash(), block.NumberU64())
112112
WriteBlock(db, block)
113-
tc.writeTxLookupEntries(db, block)
113+
tc.writeTxLookupEntriesByBlock(db, block)
114114

115115
for i, tx := range txs {
116116
if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil {

‎core/rawdb/chain_iterator.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func IndexTransactions(db ethdb.Database, from uint64, to uint64) {
218218
// Next block available, pop it off and index it
219219
delivery := queue.PopItem().(*blockTxHashes)
220220
lastNum = delivery.number
221-
WriteTxLookupEntriesByHash(batch, delivery.number, delivery.hashes)
221+
WriteTxLookupEntries(batch, delivery.number, delivery.hashes)
222222
blocks++
223223
txs += len(delivery.hashes)
224224
// If enough data was accumulated in memory or we're at the last block, dump to disk
@@ -276,7 +276,7 @@ func UnindexTransactions(db ethdb.Database, from uint64, to uint64) {
276276
// Otherwise spin up the concurrent iterator and unindexer
277277
blocks, txs := 0, 0
278278
for delivery := range hashesCh {
279-
DeleteTxLookupEntriesByHash(batch, delivery.hashes)
279+
DeleteTxLookupEntries(batch, delivery.hashes)
280280
txs += len(delivery.hashes)
281281
blocks++
282282

‎light/txpool.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
185185
if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results
186186
return err
187187
}
188-
rawdb.WriteTxLookupEntries(pool.chainDb, block)
188+
rawdb.WriteTxLookupEntriesByBlock(pool.chainDb, block)
189189

190190
// Update the transaction pool's state
191191
for _, tx := range list {

0 commit comments

Comments
 (0)