mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
core/rawdb: improve PruneTransactionIndex
This commit is contained in:
parent
2d29bcfae1
commit
1100dba965
2 changed files with 16 additions and 3 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue