From 7c0d90c8c98d2542a91c1f2a2d9dc2b67e333f35 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 22 Aug 2023 08:58:20 +0800 Subject: [PATCH] core: ensure txindex will be triggered at least once (#27847) Currently, we trigger the logic to (un)index transactions when the node receives a new block. However, in some cases the node may not receive new blocks (eg, when the Geth node is configured without peer discovery, or when it acts as an RPC node for historical-only data). In these situations, the Geth node user may not have previously configured txlookuplimit (i.e. the default of around one year), but later realizes they need to index all historical blocks. However, adding txlookuplimit=0 and restarting geth has no effect. This change makes it check for required indexing work once, on startup, to fix the issue. Co-authored-by: Martin Holst Swende --- core/blockchain.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index cd072dd4a..b0ea1ac4d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2421,6 +2421,12 @@ func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool { func (bc *BlockChain) indexBlocks(tail *uint64, head uint64, done chan struct{}) { defer func() { close(done) }() + // If head is 0, it means the chain is just initialized and no blocks are inserted, + // so don't need to indexing anything. + if head == 0 { + return + } + // The tail flag is not existent, it means the node is just initialized // and all blocks(may from ancient store) are not indexed yet. if tail == nil { @@ -2480,6 +2486,14 @@ func (bc *BlockChain) maintainTxIndex() { defer sub.Unsubscribe() log.Info("Initialized transaction indexer", "limit", bc.TxLookupLimit()) + // Launch the initial processing if chain is not empty. This step is + // useful in these scenarios that chain has no progress and indexer + // is never triggered. + if head := rawdb.ReadHeadBlock(bc.db); head != nil { + done = make(chan struct{}) + go bc.indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.NumberU64(), done) + } + for { select { case head := <-headCh: