Merge pull request #1411 from maticnetwork/manav/indexer-graceful-shutdown

core/rawdb: prevent ungraceful shutdown in tx indexing
This commit is contained in:
Manav Darji 2025-01-24 16:12:26 +05:30 committed by GitHub
parent a15366359e
commit 3a3e619d4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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