mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:29:40 +00:00
node: batch block file pruning in a single pass over block index #36621
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36621
Loading…
Reference in a new issue
No description provided.
Delete branch "pr-batch-pruning"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
When enabling pruning on a full chain (e.g.
-prune=550) or pruning a large backlog after downtime,FindFilesToPruneandFindFilesToPruneManualpreviously calledPruneOneBlockFileonce per file. BecausePruneOneBlockFilescanned all ofm_block_indexon every call, pruningMfiles acrossNblocks tookO(M \times N)work (~5.6B iterations for ~5,770 files and ~972k blocks, stalling startup while holdingcs_main).This PR splits the changes into three commits:
BlockManageroptions inChainTestingSetupand addblockmanager_find_files_to_prune_manual, covering file selection throughFlushStateToDiskon both sides of the prune height cutoff.PruneOneBlockFilewithPruneBlockFiles(const std::set<int>&)to prune all selected files in a single pass overm_block_indexusing astd::vector<bool>bitmap (sized toMaxBlockfileNum() + 1, independent of how many files are selected). Out-of-range file numbers are rejected up front before any index entry is modified. Addsblockmanager_prune_unlinked_siblings, and updates existing caller tests.CBlockIndexentries withoutBLOCK_HAVE_DATA | BLOCK_HAVE_UNDOinPruneBlockFiles. Because header-only and already-pruned entries also havenFile == 0, this avoids dirtying them when file 0 is selected (after file 0 is pruned, both selectors skip its zero-sized record, so batching is what accounts for the speedup on later backlogs). Addsblockmanager_prune_skips_dataless_entries.Benchmarked with ~972k blocks and 5,770 files to prune:
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
MSG_BLOCKby stripping witnesses in place by l0rinc)m_blocks_unlinked, fix anothernSequenceIdissue by mzumsande)If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
LLM Linter (✨ experimental)
Possible places where comparison-specific test macros should replace generic comparisons:
src/test/blockmanager_tests.cpp:BOOST_CHECK_THROW(blockman.PruneBlockFiles({-1}), std::out_of_range)→ PreferBOOST_CHECK_EXCEPTION(..., std::out_of_range, HasReason{"PruneBlockFiles: file number out of range"}).src/test/blockmanager_tests.cpp:BOOST_CHECK_THROW(blockman.PruneBlockFiles({1, 4}), std::out_of_range)→ PreferBOOST_CHECK_EXCEPTION(..., std::out_of_range, HasReason{"PruneBlockFiles: file number out of range"}).2026-12-03 10:53:12
🔵 Suggestion · Invalid-batch test misses block-index changes before rejection
The invalid-batch test can miss premature changes to sibling 1's availability, disk position or unlinked membership because the following successful prune clears the same state.
Location:
src/test/blockmanager_tests.cpp:144(head).🔵 Suggestion · Manual-pruning test conflates the requested and retention cutoffs
The manual-pruning test uses identical requested and retention cutoffs, so it would still pass if
GetPruneRangeignored the requested height.Location:
src/test/blockmanager_tests.cpp:199(head).🔵 Suggestion · PR description names nonexistent pruning tests
The PR description points to two nonexistent test cases instead of
blockmanager_prune_unlinked_siblings, which contains their described checks.Location:
src/test/blockmanager_tests.cpp:120(head).Reviewed:
e7346a974631· Review report · Previous reportspermalink
🚧 At least one of the CI tasks failed.
Task
macOS-cross to x86_64: https://github.com/bitcoin/bitcoin/actions/runs/39947671739/job/120394208316LLM reason (✨ experimental): CI failed compiling
blockmanager_tests.cpp: Clang’s thread-safety checks report accesses tonFileandnStatuswithout holdingcs_main, and-Werrortreats them as errors.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
🚧 At least one of the CI tasks failed.
Task
ASan + LSan + UBSan + integer: https://github.com/bitcoin/bitcoin/actions/runs/41544539018/job/125616778379LLM reason (✨ experimental): CI failed because
blockmanager_teststriggered UBSan on an implicit signed-to-unsigned conversion inBlockManager::PruneBlockFiles(blockstorage.cpp:280).Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
Concept ACK
The speedup holds here too.
PruneBlockFilestakes 0.65s on my i9 laptop for a first prune, and 0.09s, one pass over the index, when the entries are already pruned.nit: the commit message still says out-of-range file numbers are rejected with
vector::at(). Maybe the message could name the explicit range check instead.Concept ACK
I also had this in my backlog, solved it slightly differently, I did a quick pass over the diff.
I’d split the batching from the change to which entries get marked dirty so each is easier to review. Ideally commits are telling a story, leading the reviewers through the changes. Besides splitting the commits, also check if you can simplify the added tests. As usual, please make sure you understand every line you're submitting so that reviewers not the first humans that will try to understand it fully. Human review is the bottleneck so please make sure it's easy to review.
The description is also quite verbose, a few details to fix in the description, quick AI summary of the suggestions:
The single index scan and the change to which entries get marked dirty are in the same commit, so it’s harder to review what batching does on its own. I’d put the
ChainTestingSetupchange and the manual pruning test first, then put batching and the file 0 change in separate commits.PruneBlockFileswalks the selected files once to set the bits and again to clear their metadata. Both updates fit in the same loop.Patch
The batch test would still pass if pruning erased every unlinked child under a parent, including one in a file it should keep. A separate test would check that pruning files 1 and 3 leaves the child in file 2 in
m_blocks_unlinked.Patch
The test repeats the same checks for file 1 after pruning and after each range error. One helper would keep those assertions in sync across all three calls.
See what else you can simplify since the tests are quite verbose.
Patch
File 2 is still active, so the prune loop skips it without checking its height. I’d close it and set the cutoff at file 1’s last block so the test covers both sides of the cutoff.
Patch
Thanks for the review, will process the remarks. I though single commits were preferred, but I can definitely split them as there are indeed 2 separate issues, the O(n²) scan issue (the main performance culprit) and the file 0 dirty marking.
Thanks for the review! I've added all proposed patches, split the PR into 3 commits and condensed the PR description.
I am aware of this, I'm not submitting PRs lightly. I do understand the code changes, but I'm not familiar in the codebase. This was not a random AI find, I was thoroughly confused by the long pause at startup caused by this bug, and wanted to prevent other people from running into it as well. I will take your remarks into account for the future.
🚧 At least one of the CI tasks failed.
Task
MSan: https://github.com/bitcoin/bitcoin/actions/runs/46905709854/job/143547183789LLM reason (✨ experimental): intermittent_network: Docker image metadata requests to mirror.gcr.io returned HTTP 429 Too Many Requests, preventing the CI container image from building.
Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening.
A functional or integration test failure may be intermittent or unrelated to this pull request.
If no dedicated issue thread exists, create a new one to track it.
Leave a comment here, if you need help tracking down a confusing failure.
The split makes the batching and file-zero changes a bit easier to follow, but the implementation is still very verbose, please simplify it more - left some hints.
The functional case already checks that one
pruneblockchaincall unlinks two files. One extra RPC assertion can check that the block index marks a block as pruned too, so the new unit test and its shared fixture change can go.Example patch
Ralph pointed out that all the children in this setup have a data-bearing genesis parent, so
CheckBlockIndexwould reject the unlinked entries the test adds. A header-only parent letsReceivedBlockTransactionsput them there through the ordinary path.Example patch
The sibling and RPC cases cover pruning several files and preserving the file we skip. The mixed valid and out-of-range check is the part I'd keep from the larger unit test. It catches a rejected batch clearing a valid file's metadata.
Example patch
The sibling test already has a header-only parent and produces pruned index entries. Pruning file 0 next lets the same fixture check that both stay clean while the genesis entry gets marked dirty. The test peer keeps dirty-index access private. The example builds on the other test suggestions.
Example patch
That's clever, makes it a lot more concise to cover multiple test cases.
Done.
Also done, much cleaner indeed.