From 1100dba9650d333a0b17ed3b17a60d7fe3bb03f0 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 18 Mar 2025 21:08:16 +0100 Subject: [PATCH] core/rawdb: improve PruneTransactionIndex --- core/rawdb/chain_iterator.go | 16 +++++++++++++--- core/rawdb/chain_iterator_test.go | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/rawdb/chain_iterator.go b/core/rawdb/chain_iterator.go index 9c62a63b66..64d252f31b 100644 --- a/core/rawdb/chain_iterator.go +++ b/core/rawdb/chain_iterator.go @@ -366,10 +366,19 @@ func unindexTransactionsForTesting(db ethdb.Database, from uint64, to uint64, in // PruneTransactionIndex removes all tx index entries below a certain block number. func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) { - db = NewTable(db, string(txLookupPrefix)) - iter := db.NewIterator(nil, nil) - defer iter.Release() + tail := ReadTxIndexTail(db) + if tail == nil { + return // no index + } + if *tail > pruneBlock { + return // index ends above pruneBlock + } + // There are transactions below pruneBlock in the index. Iterate the entire index to + // remove the entries. Note if this fails, the index is messed up, but tail still + // points to the old tail. + iter := db.NewIterator(txLookupPrefix, nil) + defer iter.Release() for iter.Next() { v := iter.Value() if len(v) > 8 { @@ -383,6 +392,7 @@ func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) { } } } + WriteTxIndexTail(db, pruneBlock) } func decodeNumber(b []byte) uint64 { diff --git a/core/rawdb/chain_iterator_test.go b/core/rawdb/chain_iterator_test.go index cb6119baaa..b6cc5388a4 100644 --- a/core/rawdb/chain_iterator_test.go +++ b/core/rawdb/chain_iterator_test.go @@ -245,6 +245,9 @@ func TestPruneTransactionIndex(t *testing.T) { if block.NumberU64() < pruneBlock && num != nil { t.Fatalf("TxLookup entry not removed: %x -> %v", tx.Hash(), num) } + if block.NumberU64() > pruneBlock && (num == nil || *num != block.NumberU64()) { + t.Fatalf("wrong TxLookup entry after pruning: %x -> %v", tx.Hash(), num) + } } } }