core/rawdb: improve PruneTransactionIndex

This commit is contained in:
Felix Lange 2025-03-18 21:08:16 +01:00
parent 2d29bcfae1
commit 1100dba965
2 changed files with 16 additions and 3 deletions

View file

@ -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. // PruneTransactionIndex removes all tx index entries below a certain block number.
func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) { func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) {
db = NewTable(db, string(txLookupPrefix)) tail := ReadTxIndexTail(db)
iter := db.NewIterator(nil, nil) if tail == nil {
defer iter.Release() 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() { for iter.Next() {
v := iter.Value() v := iter.Value()
if len(v) > 8 { if len(v) > 8 {
@ -383,6 +392,7 @@ func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) {
} }
} }
} }
WriteTxIndexTail(db, pruneBlock)
} }
func decodeNumber(b []byte) uint64 { func decodeNumber(b []byte) uint64 {

View file

@ -245,6 +245,9 @@ func TestPruneTransactionIndex(t *testing.T) {
if block.NumberU64() < pruneBlock && num != nil { if block.NumberU64() < pruneBlock && num != nil {
t.Fatalf("TxLookup entry not removed: %x -> %v", tx.Hash(), num) 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)
}
} }
} }
} }