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.
// 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 {

View file

@ -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 len(v) > 8 {
log.Error("Skipping legacy tx index entry", "hash", txhash)
return false
}
if iter.Error() != nil {
log.Error("Tx index pruning iterator error", "err", iter.Error())
bn := decodeNumber(v)
if bn < pruneBlock {
removed++
return true
}
return false
})
WriteTxIndexTail(db, pruneBlock)
}

View file

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