-
Notifications
You must be signed in to change notification settings - Fork 21.9k
cmd/geth: add prune history command #31384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a48789a
0a916cf
e933ed4
6f8293d
ef5015d
55a4a39
2e470f1
403e64c
a223ee6
45c8fe9
ba74095
2d29bcf
1100dba
bbedddb
8ba23d5
b147658
94b7b69
bcd46db
a20cdbc
36cb9a7
36e5879
04d8208
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package rawdb | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "runtime" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
@@ -361,3 +362,38 @@ func UnindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt ch | |
| func unindexTransactionsForTesting(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool) { | ||
| unindexTransactions(db, from, to, interrupt, hook, false) | ||
| } | ||
|
|
||
| // PruneTransactionIndex removes all tx index entries below a certain block number. | ||
| func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason to unindex the transactions by traversing the entire tx lookups in db? Why not reuse the UnindexTransaction with block range? e.g. // PruneTransactionIndex removes all tx index entries below a certain block number.
func PruneTransactionIndex(db ethdb.Database, pruneBlock uint64) {
tail := ReadTxIndexTail(db)
if tail == nil || *tail > pruneBlock {
return // no index, or index ends above pruneBlock
}
UnindexTransactions(db, *tail, pruneBlock, nil, true)
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We decided to implement it like this because unindexing everything takes 45 minutes, while this scan takes only 1.5min (on Sepolia).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh really, interesting |
||
| tail := ReadTxIndexTail(db) | ||
| 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 count, removed int | ||
| DeleteAllTxLookupEntries(db, func(txhash common.Hash, v []byte) bool { | ||
| count++ | ||
| 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 | ||
| } | ||
| bn := decodeNumber(v) | ||
| if bn < pruneBlock { | ||
| removed++ | ||
| return true | ||
| } | ||
| return false | ||
| }) | ||
| WriteTxIndexTail(db, pruneBlock) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's dangerous to delete the transaction indexes first and then update the tail. Anyway, I think we can reuse the one here https://github.com/s1na/go-ethereum/blob/txlookup-cutoff/core/rawdb/accessors_indexes.go#L106 |
||
| } | ||
|
|
||
| func decodeNumber(b []byte) uint64 { | ||
| var numBuffer [8]byte | ||
| copy(numBuffer[8-len(b):], b) | ||
| return binary.BigEndian.Uint64(numBuffer[:]) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.