@@ -196,34 +196,43 @@ func (indexer *txIndexer) repair(head uint64) {
196
196
}
197
197
}
198
198
199
+ // resolveHead resolves the block number of the current chain head.
200
+ func (indexer * txIndexer ) resolveHead () uint64 {
201
+ headBlockHash := rawdb .ReadHeadBlockHash (indexer .db )
202
+ if headBlockHash == (common.Hash {}) {
203
+ return 0
204
+ }
205
+ headBlockNumber := rawdb .ReadHeaderNumber (indexer .db , headBlockHash )
206
+ if headBlockNumber == nil {
207
+ return 0
208
+ }
209
+ return * headBlockNumber
210
+ }
211
+
199
212
// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
200
213
// on the received chain event.
201
214
func (indexer * txIndexer ) loop (chain * BlockChain ) {
202
215
defer close (indexer .closed )
203
216
204
217
// Listening to chain events and manipulate the transaction indexes.
205
218
var (
206
- stop chan struct {} // Non-nil if background routine is active
207
- done chan struct {} // Non-nil if background routine is active
208
- headBlock = rawdb .ReadHeadBlock (indexer .db ) // The latest announced chain head
209
- head uint64
219
+ stop chan struct {} // Non-nil if background routine is active
220
+ done chan struct {} // Non-nil if background routine is active
221
+ head = indexer .resolveHead () // The latest announced chain head
210
222
211
223
headCh = make (chan ChainHeadEvent )
212
224
sub = chain .SubscribeChainHeadEvent (headCh )
213
225
)
214
226
defer sub .Unsubscribe ()
215
227
216
228
// Validate the transaction indexes and repair if necessary
217
- if headBlock != nil {
218
- indexer .repair (headBlock .NumberU64 ())
219
- }
229
+ indexer .repair (head )
220
230
221
231
// Launch the initial processing if chain is not empty (head != genesis).
222
232
// This step is useful in these scenarios that chain has no progress.
223
- if headBlock != nil && headBlock . NumberU64 () != 0 {
233
+ if head != 0 {
224
234
stop = make (chan struct {})
225
235
done = make (chan struct {})
226
- head = headBlock .Number ().Uint64 ()
227
236
go indexer .run (head , stop , done )
228
237
}
229
238
for {
0 commit comments