diff --git a/core/rawdb/chain_iterator.go b/core/rawdb/chain_iterator.go index 6989e480b5..92d1c2e918 100644 --- a/core/rawdb/chain_iterator.go +++ b/core/rawdb/chain_iterator.go @@ -92,6 +92,16 @@ func InitDatabaseFromFreezer(db ethdb.Database) { log.Info("Initialized database from freezer", "blocks", frozen, "elapsed", common.PrettyDuration(time.Since(start))) } +// adjustRangeForBor updates the range to index transactions if the ancient data was pruned before. This is +// to avoid indexing/unindexing data which is already pruned (i.e. before the `offset` block number). +func adjustRangeForBor(db ethdb.Database, from uint64) uint64 { + if offset := db.AncientOffSet(); offset > from { + from = offset + } + + return from +} + type blockTxHashes struct { number uint64 hashes []common.Hash @@ -108,10 +118,6 @@ func iterateTransactions(db ethdb.Database, from uint64, to uint64, reverse bool rlp rlp.RawValue } - if offset := db.AncientOffSet(); offset > from { - from = offset - } - if to <= from { return nil } @@ -206,6 +212,9 @@ func iterateTransactions(db ethdb.Database, from uint64, to uint64, reverse bool // There is a passed channel, the whole procedure will be interrupted if any // signal received. func indexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool, report bool) { + // Adjust range if needed + from = adjustRangeForBor(db, from) + // short circuit for invalid range if from >= to { return @@ -309,6 +318,9 @@ func indexTransactionsForTesting(db ethdb.Database, from uint64, to uint64, inte // There is a passed channel, the whole procedure will be interrupted if any // signal received. func unindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool, report bool) { + // Adjust range if needed + from = adjustRangeForBor(db, from) + // short circuit for invalid range if from >= to { return