core: fix txindexer db access bottleneck

This commit is contained in:
Sina Mahmoodi 2025-05-01 14:49:52 +02:00
parent 701df4baad
commit 9796c1bda1

View file

@ -219,6 +219,7 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
stop chan struct{} // Non-nil if background routine is active stop chan struct{} // Non-nil if background routine is active
done chan struct{} // Non-nil if background routine is active done chan struct{} // Non-nil if background routine is active
head = indexer.resolveHead() // The latest announced chain head head = indexer.resolveHead() // The latest announced chain head
lastTail = rawdb.ReadTxIndexTail(indexer.db) // // The oldest indexed block, nil means nothing indexed
headCh = make(chan ChainHeadEvent) headCh = make(chan ChainHeadEvent)
sub = chain.SubscribeChainHeadEvent(headCh) sub = chain.SubscribeChainHeadEvent(headCh)
@ -247,8 +248,9 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
case <-done: case <-done:
stop = nil stop = nil
done = nil done = nil
lastTail = rawdb.ReadTxIndexTail(indexer.db)
case ch := <-indexer.progress: case ch := <-indexer.progress:
ch <- indexer.report(head) ch <- indexer.report(head, lastTail)
case ch := <-indexer.term: case ch := <-indexer.term:
if stop != nil { if stop != nil {
close(stop) close(stop)
@ -264,7 +266,7 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
} }
// report returns the tx indexing progress. // report returns the tx indexing progress.
func (indexer *txIndexer) report(head uint64) TxIndexProgress { func (indexer *txIndexer) report(head uint64, tail *uint64) TxIndexProgress {
// Special case if the head is even below the cutoff, // Special case if the head is even below the cutoff,
// nothing to index. // nothing to index.
if head < indexer.cutoff { if head < indexer.cutoff {
@ -284,7 +286,6 @@ func (indexer *txIndexer) report(head uint64) TxIndexProgress {
} }
// Compute how many blocks have been indexed // Compute how many blocks have been indexed
var indexed uint64 var indexed uint64
tail := rawdb.ReadTxIndexTail(indexer.db)
if tail != nil { if tail != nil {
indexed = head - *tail + 1 indexed = head - *tail + 1
} }