Skip to content

Commit 0b5d8d2

Browse files
authored
core: cache transaction indexing tail in memory (#28908)
1 parent 99e9c07 commit 0b5d8d2

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

core/txindexer.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
127127

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

134135
headCh = make(chan ChainHeadEvent)
135136
sub = chain.SubscribeChainHeadEvent(headCh)
@@ -156,8 +157,9 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
156157
case <-done:
157158
stop = nil
158159
done = nil
160+
lastTail = rawdb.ReadTxIndexTail(indexer.db)
159161
case ch := <-indexer.progress:
160-
ch <- indexer.report(lastHead)
162+
ch <- indexer.report(lastHead, lastTail)
161163
case ch := <-indexer.term:
162164
if stop != nil {
163165
close(stop)
@@ -173,11 +175,7 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
173175
}
174176

175177
// report returns the tx indexing progress.
176-
func (indexer *txIndexer) report(head uint64) TxIndexProgress {
177-
var (
178-
remaining uint64
179-
tail = rawdb.ReadTxIndexTail(indexer.db)
180-
)
178+
func (indexer *txIndexer) report(head uint64, tail *uint64) TxIndexProgress {
181179
total := indexer.limit
182180
if indexer.limit == 0 || total > head {
183181
total = head + 1 // genesis included
@@ -188,6 +186,7 @@ func (indexer *txIndexer) report(head uint64) TxIndexProgress {
188186
}
189187
// The value of indexed might be larger than total if some blocks need
190188
// to be unindexed, avoiding a negative remaining.
189+
var remaining uint64
191190
if indexed < total {
192191
remaining = total - indexed
193192
}

core/txindexer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestTxIndexer(t *testing.T) {
8585
for number := *tail; number <= chainHead; number += 1 {
8686
verifyIndexes(db, number, true)
8787
}
88-
progress := indexer.report(chainHead)
88+
progress := indexer.report(chainHead, tail)
8989
if !progress.Done() {
9090
t.Fatalf("Expect fully indexed")
9191
}

0 commit comments

Comments
 (0)