Skip to content

Commit 04d8208

Browse files
committed
core/rawdb: use DeleteAllTxLookupEntries in PruneTransactionIndex
1 parent 36e5879 commit 04d8208

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

core/rawdb/accessors_indexes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) {
103103
// DeleteAllTxLookupEntries purges all the transaction indexes in the database.
104104
// If condition is specified, only the entry with condition as True will be
105105
// removed; If condition is not specified, the entry is deleted.
106-
func DeleteAllTxLookupEntries(db ethdb.KeyValueStore, condition func([]byte) bool) {
106+
func DeleteAllTxLookupEntries(db ethdb.KeyValueStore, condition func(common.Hash, []byte) bool) {
107107
iter := NewKeyLengthIterator(db.NewIterator(txLookupPrefix, nil), common.HashLength+len(txLookupPrefix))
108108
defer iter.Release()
109109

110110
batch := db.NewBatch()
111111
for iter.Next() {
112-
if condition == nil || condition(iter.Value()) {
112+
txhash := common.Hash(iter.Key()[1:])
113+
if condition == nil || condition(txhash, iter.Value()) {
113114
batch.Delete(iter.Key())
114115
}
115116
if batch.ValueSize() >= ethdb.IdealBatchSize {

core/rawdb/chain_iterator.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package rawdb
1818

1919
import (
2020
"encoding/binary"
21-
"fmt"
2221
"runtime"
2322
"sync/atomic"
2423
"time"
@@ -370,38 +369,26 @@ func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) {
370369
if tail == nil || *tail > pruneBlock {
371370
return // no index, or index ends above pruneBlock
372371
}
373-
374372
// There are blocks below pruneBlock in the index. Iterate the entire index to remove
375373
// their entries. Note if this fails, the index is messed up, but tail still points to
376374
// the old tail.
377-
var (
378-
iter = db.NewIterator(txLookupPrefix, nil)
379-
count int
380-
removed int
381-
)
382-
defer iter.Release()
383-
for iter.Next() {
375+
var count, removed int
376+
DeleteAllTxLookupEntries(db, func(txhash common.Hash, v []byte) bool {
384377
count++
385-
v := iter.Value()
378+
if count%10000000 == 0 {
379+
log.Info("Pruning tx index", "count", count, "removed", removed)
380+
}
386381
if len(v) > 8 {
387-
log.Error("Skipping legacy tx index entry", "hash", fmt.Sprintf("%x", iter.Key()))
388-
continue
382+
log.Error("Skipping legacy tx index entry", "hash", txhash)
383+
return false
389384
}
390385
bn := decodeNumber(v)
391386
if bn < pruneBlock {
392-
if err := db.Delete(iter.Key()); err != nil {
393-
log.Crit("Error deleting tx index entry", "hash", fmt.Sprintf("%x", iter.Key()))
394-
}
395387
removed++
388+
return true
396389
}
397-
if count%10000000 == 0 {
398-
log.Info("Pruning tx index", "count", count, "removed", removed)
399-
}
400-
}
401-
if iter.Error() != nil {
402-
log.Error("Tx index pruning iterator error", "err", iter.Error())
403-
}
404-
390+
return false
391+
})
405392
WriteTxIndexTail(db, pruneBlock)
406393
}
407394

core/txindexer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222

23+
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/core/rawdb"
2425
"github.com/ethereum/go-ethereum/ethdb"
2526
"github.com/ethereum/go-ethereum/log"
@@ -186,7 +187,7 @@ func (indexer *txIndexer) repair(head uint64) {
186187
// potentially leaving dangling indexes in the database.
187188
// However, this is considered acceptable.
188189
rawdb.WriteTxIndexTail(indexer.db, indexer.cutoff)
189-
rawdb.DeleteAllTxLookupEntries(indexer.db, func(blob []byte) bool {
190+
rawdb.DeleteAllTxLookupEntries(indexer.db, func(txhash common.Hash, blob []byte) bool {
190191
n := rawdb.DecodeTxLookupEntry(blob, indexer.db)
191192
return n != nil && *n < indexer.cutoff
192193
})

0 commit comments

Comments
 (0)