core/rawdb: use DeleteAllTxLookupEntries in PruneTransactionIndex

This commit is contained in:
Felix Lange 2025-03-21 11:43:02 +01:00
parent 36e5879647
commit 04d8208d43
3 changed files with 18 additions and 29 deletions

View file

@ -103,13 +103,14 @@ func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) {
// DeleteAllTxLookupEntries purges all the transaction indexes in the database. // DeleteAllTxLookupEntries purges all the transaction indexes in the database.
// If condition is specified, only the entry with condition as True will be // If condition is specified, only the entry with condition as True will be
// removed; If condition is not specified, the entry is deleted. // 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)) iter := NewKeyLengthIterator(db.NewIterator(txLookupPrefix, nil), common.HashLength+len(txLookupPrefix))
defer iter.Release() defer iter.Release()
batch := db.NewBatch() batch := db.NewBatch()
for iter.Next() { 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()) batch.Delete(iter.Key())
} }
if batch.ValueSize() >= ethdb.IdealBatchSize { if batch.ValueSize() >= ethdb.IdealBatchSize {

View file

@ -18,7 +18,6 @@ package rawdb
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"runtime" "runtime"
"sync/atomic" "sync/atomic"
"time" "time"
@ -370,38 +369,26 @@ func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) {
if tail == nil || *tail > pruneBlock { if tail == nil || *tail > pruneBlock {
return // no index, or index ends above pruneBlock return // no index, or index ends above pruneBlock
} }
// There are blocks below pruneBlock in the index. Iterate the entire index to remove // 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 // their entries. Note if this fails, the index is messed up, but tail still points to
// the old tail. // the old tail.
var ( var count, removed int
iter = db.NewIterator(txLookupPrefix, nil) DeleteAllTxLookupEntries(db, func(txhash common.Hash, v []byte) bool {
count int
removed int
)
defer iter.Release()
for iter.Next() {
count++ 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 { if count%10000000 == 0 {
log.Info("Pruning tx index", "count", count, "removed", removed) log.Info("Pruning tx index", "count", count, "removed", removed)
} }
if len(v) > 8 {
log.Error("Skipping legacy tx index entry", "hash", txhash)
return false
} }
if iter.Error() != nil { bn := decodeNumber(v)
log.Error("Tx index pruning iterator error", "err", iter.Error()) if bn < pruneBlock {
removed++
return true
} }
return false
})
WriteTxIndexTail(db, pruneBlock) WriteTxIndexTail(db, pruneBlock)
} }

View file

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -186,7 +187,7 @@ func (indexer *txIndexer) repair(head uint64) {
// potentially leaving dangling indexes in the database. // potentially leaving dangling indexes in the database.
// However, this is considered acceptable. // However, this is considered acceptable.
rawdb.WriteTxIndexTail(indexer.db, indexer.cutoff) 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) n := rawdb.DecodeTxLookupEntry(blob, indexer.db)
return n != nil && *n < indexer.cutoff return n != nil && *n < indexer.cutoff
}) })