mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
core: resolve chain head number only
This commit is contained in:
parent
aaaea82e65
commit
a874ea621f
1 changed files with 18 additions and 9 deletions
|
|
@ -196,6 +196,19 @@ func (indexer *txIndexer) repair(head uint64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveHead resolves the block number of the current chain head.
|
||||||
|
func (indexer *txIndexer) resolveHead() uint64 {
|
||||||
|
headBlockHash := rawdb.ReadHeadBlockHash(indexer.db)
|
||||||
|
if headBlockHash == (common.Hash{}) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
headBlockNumber := rawdb.ReadHeaderNumber(indexer.db, headBlockHash)
|
||||||
|
if headBlockNumber == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return *headBlockNumber
|
||||||
|
}
|
||||||
|
|
||||||
// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
|
// loop is the scheduler of the indexer, assigning indexing/unindexing tasks depending
|
||||||
// on the received chain event.
|
// on the received chain event.
|
||||||
func (indexer *txIndexer) loop(chain *BlockChain) {
|
func (indexer *txIndexer) loop(chain *BlockChain) {
|
||||||
|
|
@ -205,8 +218,7 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
|
||||||
var (
|
var (
|
||||||
stop chan struct{} // Non-nil if background routine is active
|
stop chan struct{} // Non-nil if background routine is active
|
||||||
done chan struct{} // Non-nil if background routine is active
|
done chan struct{} // Non-nil if background routine is active
|
||||||
headBlock = rawdb.ReadHeadBlock(indexer.db) // The latest announced chain head
|
head = indexer.resolveHead() // The latest announced chain head
|
||||||
head uint64
|
|
||||||
|
|
||||||
headCh = make(chan ChainHeadEvent)
|
headCh = make(chan ChainHeadEvent)
|
||||||
sub = chain.SubscribeChainHeadEvent(headCh)
|
sub = chain.SubscribeChainHeadEvent(headCh)
|
||||||
|
|
@ -214,16 +226,13 @@ func (indexer *txIndexer) loop(chain *BlockChain) {
|
||||||
defer sub.Unsubscribe()
|
defer sub.Unsubscribe()
|
||||||
|
|
||||||
// Validate the transaction indexes and repair if necessary
|
// Validate the transaction indexes and repair if necessary
|
||||||
if headBlock != nil {
|
indexer.repair(head)
|
||||||
indexer.repair(headBlock.NumberU64())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Launch the initial processing if chain is not empty (head != genesis).
|
// Launch the initial processing if chain is not empty (head != genesis).
|
||||||
// This step is useful in these scenarios that chain has no progress.
|
// This step is useful in these scenarios that chain has no progress.
|
||||||
if headBlock != nil && headBlock.NumberU64() != 0 {
|
if head != 0 {
|
||||||
stop = make(chan struct{})
|
stop = make(chan struct{})
|
||||||
done = make(chan struct{})
|
done = make(chan struct{})
|
||||||
head = headBlock.Number().Uint64()
|
|
||||||
go indexer.run(head, stop, done)
|
go indexer.run(head, stop, done)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue