Skip to content

Commit a874ea6

Browse files
committed
core: resolve chain head number only
1 parent aaaea82 commit a874ea6

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

core/txindexer.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,34 +196,43 @@ func (indexer *txIndexer) repair(head uint64) {
196196
}
197197
}
198198

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+
199212
// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
200213
// on the received chain event.
201214
func (indexer *txIndexer) loop(chain *BlockChain) {
202215
defer close(indexer.closed)
203216

204217
// Listening to chain events and manipulate the transaction indexes.
205218
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
210222

211223
headCh = make(chan ChainHeadEvent)
212224
sub = chain.SubscribeChainHeadEvent(headCh)
213225
)
214226
defer sub.Unsubscribe()
215227

216228
// Validate the transaction indexes and repair if necessary
217-
if headBlock != nil {
218-
indexer.repair(headBlock.NumberU64())
219-
}
229+
indexer.repair(head)
220230

221231
// Launch the initial processing if chain is not empty (head != genesis).
222232
// This step is useful in these scenarios that chain has no progress.
223-
if headBlock != nil && headBlock.NumberU64() != 0 {
233+
if head != 0 {
224234
stop = make(chan struct{})
225235
done = make(chan struct{})
226-
head = headBlock.Number().Uint64()
227236
go indexer.run(head, stop, done)
228237
}
229238
for {

0 commit comments

Comments
 (0)