@@ -428,7 +428,20 @@ func NewBlockChain(
428428 // Start tx indexer/unindexer if required.
429429 if bc .cacheConfig .TxLookupLimit != 0 {
430430 bc .wg .Add (1 )
431- go bc .maintainTxIndex ()
431+ var (
432+ headCh = make (chan ChainEvent , 1 ) // Buffered to avoid locking up the event feed
433+ sub = bc .SubscribeChainAcceptedEvent (headCh )
434+ )
435+ go func () {
436+ defer bc .wg .Done ()
437+ if sub == nil {
438+ log .Warn ("could not create chain accepted subscription to unindex txs" )
439+ return
440+ }
441+ defer sub .Unsubscribe ()
442+
443+ bc .maintainTxIndex (headCh )
444+ }()
432445 }
433446 return bc , nil
434447}
@@ -442,6 +455,7 @@ func (bc *BlockChain) unindexBlocks(tail uint64, head uint64, done chan struct{}
442455 txUnindexTimer .Inc (time .Since (start ).Milliseconds ())
443456 bc .txIndexTailLock .Unlock ()
444457 close (done )
458+ bc .wg .Done ()
445459 }()
446460
447461 if head - txLookupLimit + 1 >= tail {
@@ -454,8 +468,7 @@ func (bc *BlockChain) unindexBlocks(tail uint64, head uint64, done chan struct{}
454468// transaction index. This does not support reconstruction of removed indexes.
455469// Invariant: If TxLookupLimit is 0, it means all tx indices will be preserved.
456470// Meaning that this function should never be called.
457- func (bc * BlockChain ) maintainTxIndex () {
458- defer bc .wg .Done ()
471+ func (bc * BlockChain ) maintainTxIndex (headCh <- chan ChainEvent ) {
459472 txLookupLimit := bc .cacheConfig .TxLookupLimit
460473
461474 // If the user just upgraded to a new version which supports transaction
@@ -466,15 +479,8 @@ func (bc *BlockChain) maintainTxIndex() {
466479
467480 // Any reindexing done, start listening to chain events and moving the index window
468481 var (
469- done chan struct {} // Non-nil if background unindexing or reindexing routine is active.
470- headCh = make (chan ChainEvent , 1 ) // Buffered to avoid locking up the event feed
482+ done chan struct {} // Non-nil if background unindexing or reindexing routine is active.
471483 )
472- sub := bc .SubscribeChainAcceptedEvent (headCh )
473- if sub == nil {
474- log .Warn ("could not create chain accepted subscription to unindex txs" )
475- return
476- }
477- defer sub .Unsubscribe ()
478484 log .Info ("Initialized transaction unindexer" , "limit" , txLookupLimit )
479485
480486 // Launch the initial processing if chain is not empty. This step is
@@ -483,6 +489,7 @@ func (bc *BlockChain) maintainTxIndex() {
483489 if head := bc .CurrentBlock (); head != nil && head .Number .Uint64 () > txLookupLimit {
484490 done = make (chan struct {})
485491 tail := rawdb .ReadTxIndexTail (bc .db )
492+ bc .wg .Add (1 )
486493 go bc .unindexBlocks (* tail , head .Number .Uint64 (), done )
487494 }
488495
@@ -498,6 +505,7 @@ func (bc *BlockChain) maintainTxIndex() {
498505 done = make (chan struct {})
499506 // Note: tail will not be nil since it is initialized in this function.
500507 tail := rawdb .ReadTxIndexTail (bc .db )
508+ bc .wg .Add (1 )
501509 go bc .unindexBlocks (* tail , headNum , done )
502510 }
503511 case <- done :
0 commit comments