From 2d148444701efecfba20f809f8f47bf18a057afe Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Fri, 30 Aug 2019 14:19:27 +0800 Subject: [PATCH] core: generate indices if txIndexTail is not nil --- cmd/geth/chaincmd.go | 2 ++ core/blockchain.go | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 27ce170694..20fb5aa995 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -69,6 +69,7 @@ It expects the genesis file as argument.`, utils.GCModeFlag, utils.CacheDatabaseFlag, utils.CacheGCFlag, + utils.TxLookupLimitFlag, }, Category: "BLOCKCHAIN COMMANDS", Description: ` @@ -136,6 +137,7 @@ The export-preimages command export hash preimages to an RLP encoded stream`, utils.FakePoWFlag, utils.TestnetFlag, utils.RinkebyFlag, + utils.TxLookupLimitFlag, }, Category: "BLOCKCHAIN COMMANDS", Description: ` diff --git a/core/blockchain.go b/core/blockchain.go index 8bfdaf98b6..81d4f53a16 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -141,7 +141,7 @@ type BlockChain struct { // txLookupLimit is the maximum number of blocks from head whose tx indices // are reserved: // * 0: means no limit and regenerate any missing indexes - // * N: means N block limit [HEAD-N, HEAD] and delete extra indexes + // * N: means N block limit [HEAD-N+1, HEAD] and delete extra indexes // * nil: disable tx reindexer/deleter, but still index new blocks txLookupLimit uint64 @@ -1112,8 +1112,16 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ // * If all ancient tx indices are required to be reserved(txlookuplimit is even higher than ancientlimit) // * If block number is large enough to be regarded as a recent block // It means blocks below the ancientLimit-txlookupLimit won't be indexed. + // + // But if the `TxIndexTail` is not nil, e.g. Geth is initialized with + // an external ancient database, during the setup, blockchain will start + // a background routine to re-indexed all indices in [ancients - txlookupLimit, ancients) + // range. In this case, all tx indices of newly imported blocks should be + // generated. if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit { rawdb.WriteTxLookupEntries(batch, block) + } else if rawdb.ReadTxIndexTail(bc.db) != nil { + rawdb.WriteTxLookupEntries(batch, block) } stats.processed++ }