From 04d8208d43e7db89337d3b5758d81416b35967aa Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 21 Mar 2025 11:43:02 +0100 Subject: [PATCH] core/rawdb: use DeleteAllTxLookupEntries in PruneTransactionIndex --- core/rawdb/accessors_indexes.go | 5 +++-- core/rawdb/chain_iterator.go | 39 +++++++++++---------------------- core/txindexer.go | 3 ++- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index 342aedd8dc..7bb96b1fa1 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -103,13 +103,14 @@ func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) { // DeleteAllTxLookupEntries purges all the transaction indexes in the database. // If condition is specified, only the entry with condition as True will be // removed; If condition is not specified, the entry is deleted. -func DeleteAllTxLookupEntries(db ethdb.KeyValueStore, condition func([]byte) bool) { +func DeleteAllTxLookupEntries(db ethdb.KeyValueStore, condition func(common.Hash, []byte) bool) { iter := NewKeyLengthIterator(db.NewIterator(txLookupPrefix, nil), common.HashLength+len(txLookupPrefix)) defer iter.Release() batch := db.NewBatch() for iter.Next() { - if condition == nil || condition(iter.Value()) { + txhash := common.Hash(iter.Key()[1:]) + if condition == nil || condition(txhash, iter.Value()) { batch.Delete(iter.Key()) } if batch.ValueSize() >= ethdb.IdealBatchSize { diff --git a/core/rawdb/chain_iterator.go b/core/rawdb/chain_iterator.go index 3371669eab..ecbc44e1f1 100644 --- a/core/rawdb/chain_iterator.go +++ b/core/rawdb/chain_iterator.go @@ -18,7 +18,6 @@ package rawdb import ( "encoding/binary" - "fmt" "runtime" "sync/atomic" "time" @@ -370,38 +369,26 @@ func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) { if tail == nil || *tail > pruneBlock { return // no index, or index ends above pruneBlock } - // There are blocks below pruneBlock in the index. Iterate the entire index to remove // their entries. Note if this fails, the index is messed up, but tail still points to // the old tail. - var ( - iter = db.NewIterator(txLookupPrefix, nil) - count int - removed int - ) - defer iter.Release() - for iter.Next() { + var count, removed int + DeleteAllTxLookupEntries(db, func(txhash common.Hash, v []byte) bool { count++ - v := iter.Value() - if len(v) > 8 { - log.Error("Skipping legacy tx index entry", "hash", fmt.Sprintf("%x", iter.Key())) - continue - } - bn := decodeNumber(v) - if bn < pruneBlock { - if err := db.Delete(iter.Key()); err != nil { - log.Crit("Error deleting tx index entry", "hash", fmt.Sprintf("%x", iter.Key())) - } - removed++ - } if count%10000000 == 0 { log.Info("Pruning tx index", "count", count, "removed", removed) } - } - if iter.Error() != nil { - log.Error("Tx index pruning iterator error", "err", iter.Error()) - } - + if len(v) > 8 { + log.Error("Skipping legacy tx index entry", "hash", txhash) + return false + } + bn := decodeNumber(v) + if bn < pruneBlock { + removed++ + return true + } + return false + }) WriteTxIndexTail(db, pruneBlock) } diff --git a/core/txindexer.go b/core/txindexer.go index 29e87905d5..d0fce302f3 100644 --- a/core/txindexer.go +++ b/core/txindexer.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" @@ -186,7 +187,7 @@ func (indexer *txIndexer) repair(head uint64) { // potentially leaving dangling indexes in the database. // However, this is considered acceptable. rawdb.WriteTxIndexTail(indexer.db, indexer.cutoff) - rawdb.DeleteAllTxLookupEntries(indexer.db, func(blob []byte) bool { + rawdb.DeleteAllTxLookupEntries(indexer.db, func(txhash common.Hash, blob []byte) bool { n := rawdb.DecodeTxLookupEntry(blob, indexer.db) return n != nil && *n < indexer.cutoff })