Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions core/txindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,30 @@ func (indexer *txIndexer) loop(chain *BlockChain) {

// Listening to chain events and manipulate the transaction indexes.
var (
stop chan struct{} // Non-nil if background routine is active.
done chan struct{} // Non-nil if background routine is active.
lastHead uint64 // The latest announced chain head (whose tx indexes are assumed created)
lastTail = rawdb.ReadTxIndexTail(indexer.db) // The oldest indexed block, nil means nothing indexed

headCh = make(chan ChainHeadEvent)
sub = chain.SubscribeChainHeadEvent(headCh)
stop chan struct{} // Non-nil if background routine is active.
done chan struct{} // Non-nil if background routine is active.
lastHead uint64 // The latest announced chain head (whose tx indexes are assumed created)
headCh = make(chan ChainHeadEvent)
sub = chain.SubscribeChainHeadEvent(headCh)
)

lastTail := rawdb.ReadTxIndexTail(indexer.db)
if lastTail != nil {
// NOTE: The "TransactionIndexTail" key may exist only in cold SST files.
// Without a recent write, the key won't be in the memtable or block cache,
// causing every Get to trigger expensive readBlock + CRC checks.
//
// This dummy write forces the key into the memtable (and later SST),
// ensuring future reads are fast (from memory or block cache).
batch := indexer.db.NewBatch()
rawdb.WriteTxIndexTail(batch, *lastTail)

if err := batch.Write(); err != nil {
log.Crit("Failed to write TransactionIndexTail warm-up", "error", err)
return
}
Comment on lines +137 to +151
Copy link

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the warm-up write logic into a separate function for improved readability and maintainability in case this behavior needs to be reused or extended.

Suggested change
lastTail := rawdb.ReadTxIndexTail(indexer.db)
if lastTail != nil {
// NOTE: The "TransactionIndexTail" key may exist only in cold SST files.
// Without a recent write, the key won't be in the memtable or block cache,
// causing every Get to trigger expensive readBlock + CRC checks.
//
// This dummy write forces the key into the memtable (and later SST),
// ensuring future reads are fast (from memory or block cache).
batch := indexer.db.NewBatch()
rawdb.WriteTxIndexTail(batch, *lastTail)
if err := batch.Write(); err != nil {
log.Crit("Failed to write TransactionIndexTail warm-up", "error", err)
return
}
lastTail := indexer.warmUpTxIndexTail()
if lastTail == nil {
return

Copilot uses AI. Check for mistakes.
}

defer sub.Unsubscribe()

// Launch the initial processing if chain is not empty (head != genesis).
Expand Down