From 5387bfc31f369987e83545772e47d4025259aaa1 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 1 Dec 2021 11:26:25 +0800 Subject: [PATCH 1/2] core, core/rawdb: fix transaction indexing --- core/blockchain.go | 9 ++++++++- core/rawdb/chain_iterator.go | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 68388ec40a61..f409dc3f1aa3 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2204,7 +2204,14 @@ func (bc *BlockChain) maintainTxIndex(ancients uint64) { // If a previous indexing existed, make sure that we fill in any missing entries if bc.txLookupLimit == 0 || head < bc.txLookupLimit { if *tail > 0 { - rawdb.IndexTransactions(bc.db, 0, *tail, bc.quit) + // It can happen when chain is rewound to a historical point which + // is even lower than the indexes tail, recap the indexing target + // to new head to avoid reading non-existent block bodies. + end := *tail + if end > head+1 { + end = head+1 + } + rawdb.IndexTransactions(bc.db, 0, end, bc.quit) } return } diff --git a/core/rawdb/chain_iterator.go b/core/rawdb/chain_iterator.go index daee721594e6..0d5a5ee6a790 100644 --- a/core/rawdb/chain_iterator.go +++ b/core/rawdb/chain_iterator.go @@ -247,7 +247,8 @@ func indexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan } } -// IndexTransactions creates txlookup indices of the specified block range. +// IndexTransactions creates txlookup indices of the specified block range. The from +// is included while to is excluded. // // This function iterates canonical chain in reverse order, it has one main advantage: // We can write tx index tail flag periodically even without the whole indexing @@ -339,6 +340,7 @@ func unindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt ch } // UnindexTransactions removes txlookup indices of the specified block range. +// The from is included while to is excluded. // // There is a passed channel, the whole procedure will be interrupted if any // signal received. From a55de1febbcc96916a3cc730c392821dac8efc2b Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 3 Dec 2021 13:42:30 +0100 Subject: [PATCH 2/2] core: go format --- core/blockchain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index f409dc3f1aa3..85591931ee42 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2209,7 +2209,7 @@ func (bc *BlockChain) maintainTxIndex(ancients uint64) { // to new head to avoid reading non-existent block bodies. end := *tail if end > head+1 { - end = head+1 + end = head + 1 } rawdb.IndexTransactions(bc.db, 0, end, bc.quit) }