@@ -64,19 +64,9 @@ func newTxIndexer(limit uint64, chain *BlockChain) *txIndexer {
6464 chain : chain ,
6565 }
6666 chain .wg .Add (1 )
67- var (
68- headCh = make (chan ChainEvent , 1 )
69- sub = chain .SubscribeChainAcceptedEvent (headCh )
70- )
7167 go func () {
7268 defer chain .wg .Done ()
73- if sub == nil {
74- log .Warn ("could not create chain accepted subscription to unindex txs" )
75- return
76- }
77- defer sub .Unsubscribe ()
78-
79- indexer .loop (headCh )
69+ indexer .loop (chain )
8070 }()
8171
8272 var msg string
@@ -108,6 +98,11 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don
10898 return
10999 }
110100
101+ // Defensively ensure tail is not nil.
102+ if tail == nil {
103+ tail = new (uint64 )
104+ }
105+
111106 if head - indexer .limit + 1 >= * tail {
112107 // Unindex a part of stale indices and forward index tail to HEAD-limit
113108 rawdb .UnindexTransactions (indexer .db , * tail , head - indexer .limit + 1 , stop , false )
@@ -116,7 +111,7 @@ func (indexer *txIndexer) run(tail *uint64, head uint64, stop chan struct{}, don
116111
117112// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
118113// on the received chain event.
119- func (indexer * txIndexer ) loop (headCh <- chan ChainEvent ) {
114+ func (indexer * txIndexer ) loop (chain * BlockChain ) {
120115 defer close (indexer .closed )
121116
122117 // If the user just upgraded to a new version which supports transaction
@@ -131,7 +126,16 @@ func (indexer *txIndexer) loop(headCh <-chan ChainEvent) {
131126 done chan struct {} // Non-nil if background routine is active.
132127 lastHead uint64 // The latest announced chain head (whose tx indexes are assumed created)
133128 lastTail = rawdb .ReadTxIndexTail (indexer .db ) // The oldest indexed block, nil means nothing indexed
129+
130+ headCh = make (chan ChainEvent , 1 )
131+ sub = chain .SubscribeChainAcceptedEvent (headCh )
134132 )
133+ if sub == nil {
134+ log .Warn ("could not create chain accepted subscription to unindex txs" )
135+ return
136+ }
137+ defer sub .Unsubscribe ()
138+
135139 log .Info ("Initialized transaction unindexer" , "limit" , indexer .limit )
136140
137141 // Launch the initial processing if chain is not empty (head != genesis).
0 commit comments