@@ -47,6 +47,10 @@ type txIndexer struct {
47
47
// and all others shouldn't.
48
48
limit uint64
49
49
50
+ // The current head of blockchain for transaction indexing. This field
51
+ // is accessed by both the indexer and the indexing progress queries.
52
+ head atomic.Uint64
53
+
50
54
// The current tail of the indexed transactions, null indicates
51
55
// that no transactions have been indexed yet.
52
56
//
@@ -74,6 +78,7 @@ func newTxIndexer(limit uint64, chain *BlockChain) *txIndexer {
74
78
term : make (chan chan struct {}),
75
79
closed : make (chan struct {}),
76
80
}
81
+ indexer .head .Store (indexer .resolveHead ())
77
82
indexer .tail .Store (rawdb .ReadTxIndexTail (chain .db ))
78
83
79
84
go indexer .loop (chain )
@@ -228,16 +233,15 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
228
233
229
234
// Listening to chain events and manipulate the transaction indexes.
230
235
var (
231
- stop chan struct {} // Non-nil if background routine is active
232
- done chan struct {} // Non-nil if background routine is active
233
- head = indexer .resolveHead () // The latest announced chain head
234
-
236
+ stop chan struct {} // Non-nil if background routine is active
237
+ done chan struct {} // Non-nil if background routine is active
235
238
headCh = make (chan ChainHeadEvent )
236
239
sub = chain .SubscribeChainHeadEvent (headCh )
237
240
)
238
241
defer sub .Unsubscribe ()
239
242
240
243
// Validate the transaction indexes and repair if necessary
244
+ head := indexer .head .Load ()
241
245
indexer .repair (head )
242
246
243
247
// Launch the initial processing if chain is not empty (head != genesis).
@@ -250,15 +254,18 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
250
254
for {
251
255
select {
252
256
case h := <- headCh :
257
+ indexer .head .Store (h .Header .Number .Uint64 ())
253
258
if done == nil {
254
259
stop = make (chan struct {})
255
260
done = make (chan struct {})
256
261
go indexer .run (h .Header .Number .Uint64 (), stop , done )
257
262
}
263
+
258
264
case <- done :
259
265
stop = nil
260
266
done = nil
261
267
indexer .tail .Store (rawdb .ReadTxIndexTail (indexer .db ))
268
+
262
269
case ch := <- indexer .term :
263
270
if stop != nil {
264
271
close (stop )
@@ -314,7 +321,7 @@ func (indexer *txIndexer) report(head uint64, tail *uint64) TxIndexProgress {
314
321
// only updated at the end of each indexing operation. However, this delay is
315
322
// considered acceptable.
316
323
func (indexer * txIndexer ) txIndexProgress () TxIndexProgress {
317
- return indexer .report (indexer .resolveHead (), indexer .tail .Load ())
324
+ return indexer .report (indexer .head . Load (), indexer .tail .Load ())
318
325
}
319
326
320
327
// close shutdown the indexer. Safe to be called for multiple times.
0 commit comments