WIP: refactor: move block writes and pruning into BlockManager #36738

Draft
pzafonte wants to merge 8 commits from blockstorage-prune-bookkeeping into master AGit
pzafonte commented 2026-11-02 23:58:37 +00:00 (Migrated from github.com)

FlushStateToDisk() handles block storage directly. It applies BlockManager's prune locks, writes its pruned flag, checks its blocks directory space and runs the flush, index write and file deletion, while BlockManager reads the chainstate to pick files. A pruning API on block storage (#35627) would have to go through validation for all of it.

This moves the storage side into BlockManager, as described in the same issue. Validation decides which heights may be pruned and passes them as limits. BlockManager applies its prune locks, selects the files, flushes, writes the index and deletes the files, in the same order as before. There is no behavior change to my knowledge.

The kernel pruning functions in #36856 and #36857 use this.

Based on #36735 Draft until it is merged.

Thanks to musaHaruna for his review on my fork, which led to grouping and checking the prune limits and to moving the prune bookkeeping into BlockManager.

`FlushStateToDisk()` handles block storage directly. It applies `BlockManager`'s prune locks, writes its pruned flag, checks its blocks directory space and runs the flush, index write and file deletion, while `BlockManager` reads the chainstate to pick files. A pruning API on block storage (#35627) would have to go through validation for all of it. This moves the storage side into `BlockManager`, as described in the same issue. Validation decides which heights may be pruned and passes them as limits. `BlockManager` applies its prune locks, selects the files, flushes, writes the index and deletes the files, in the same order as before. There is no behavior change to my knowledge. The kernel pruning functions in #36856 and #36857 use this. Based on #36735 Draft until it is merged. Thanks to musaHaruna for his [review on my fork](https://github.com/pzafonte/bitcoin/pull/2#pullrequestreview-5533305792), which led to grouping and checking the prune limits and to moving the prune bookkeeping into `BlockManager`.
DrahtBot commented 2026-11-02 23:58:49 +00:00 (Migrated from github.com)

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.

Type Reviewers
Concept ACK nervana21

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:

  • #37094 (validation: validate assumeutxo snapshots with a single chainstate by fjahr)
  • #37028 (validation: don't report unstored blocks as new by l0rinc)
  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36854 (kernel: let callers create the block manager by pzafonte)
  • #36739 (refactor: pass BlockManager to ChainstateManager by pzafonte)
  • #36621 (node: batch block file pruning in a single pass over block index by mutatrum)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #36049 (streams: handle buffered write failures outside the destructor by l0rinc)
  • #36033 ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko)
  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36738 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36738.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36738#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [nervana21](https://github.com/bitcoin/bitcoin/pull/36738#issuecomment-6866487532) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37094](https://github.com/bitcoin/bitcoin/pull/37094) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37094.svg"></sub> (validation: validate assumeutxo snapshots with a single chainstate by fjahr) * [#37028](https://github.com/bitcoin/bitcoin/pull/37028) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37028.svg"></sub> (validation: don't report unstored blocks as new by l0rinc) * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#36854](https://github.com/bitcoin/bitcoin/pull/36854) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte) * [#36739](https://github.com/bitcoin/bitcoin/pull/36739) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager by pzafonte) * [#36621](https://github.com/bitcoin/bitcoin/pull/36621) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36621.svg"></sub> (node: batch block file pruning in a single pass over block index by mutatrum) * [#36326](https://github.com/bitcoin/bitcoin/pull/36326) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36326.svg"></sub> (kernel: Use typed errors for fatal and flush error notifications by arejula27) * [#36049](https://github.com/bitcoin/bitcoin/pull/36049) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36049.svg"></sub> (streams: handle buffered write failures outside the destructor by l0rinc) * [#36033](https://github.com/bitcoin/bitcoin/pull/36033) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36033.svg"></sub> ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko) * [#34534](https://github.com/bitcoin/bitcoin/pull/34534) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34534.svg"></sub> (rpc: Manual prune lock management (Take 2) by fjahr) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Block-file reuse after a failed prune flush is untested

The new recovery test never rewrites a file awaiting deletion, so it would miss removal of the cancellation that keeps a later flush from unlinking that reused file.

Location: src/node/blockstorage.cpp:948 (head).

🔵 Suggestion · The pruned-flag invariant remains a caller prerequisite

The sole caller sets the flag correctly, but WriteBlockStorage() leaves future callers responsible for recording pruning before it can unlink files.

Location: src/node/blockstorage.cpp:831 (head).

🔵 Suggestion · The prune-lock test misses locks below the reorg cutoff

The new test would still pass if MovePruneLocksBack() advanced an already-lower lock, leaving its preservation guard without a regression assertion.

Location: src/test/blockmanager_tests.cpp:322 (head).

🔵 Suggestion · The prune-retry test does not check deferred flushing

The forced retry in prune_flush_failure cannot distinguish intended deferral from a change that makes pending deletions force a storage write.

Location: src/test/chainstate_write_tests.cpp:210 (head).

Review coverage was incomplete.


Reviewed: 777b2f90de97 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:ddea9196493d3b8fd841a36a6c53efd85dac7a05 --> <!-- ralph-head:777b2f90de97ef57eba64b2cd22058300ec74887 --> <!-- ralph-case:422bb46f43b42f0706cd9e5b0e0903d40e42c6352b18c016c31bdbe72430e415 --> **🔵 Suggestion · Block-file reuse after a failed prune flush is untested** The new recovery test never rewrites a file awaiting deletion, so it would miss removal of the cancellation that keeps a later flush from unlinking that reused file. Location: [`src/node/blockstorage.cpp:948`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/777b2f90de97ef57eba64b2cd22058300ec74887/src/node/blockstorage.cpp#L948>) (head). **🔵 Suggestion · The pruned-flag invariant remains a caller prerequisite** The sole caller sets the flag correctly, but WriteBlockStorage() leaves future callers responsible for recording pruning before it can unlink files. Location: [`src/node/blockstorage.cpp:831`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/777b2f90de97ef57eba64b2cd22058300ec74887/src/node/blockstorage.cpp#L831>) (head). **🔵 Suggestion · The prune-lock test misses locks below the reorg cutoff** The new test would still pass if MovePruneLocksBack() advanced an already-lower lock, leaving its preservation guard without a regression assertion. Location: [`src/test/blockmanager_tests.cpp:322`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/777b2f90de97ef57eba64b2cd22058300ec74887/src/test/blockmanager_tests.cpp#L322>) (head). **🔵 Suggestion · The prune-retry test does not check deferred flushing** The forced retry in prune_flush_failure cannot distinguish intended deferral from a change that makes pending deletions force a storage write. Location: [`src/test/chainstate_write_tests.cpp:210`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/777b2f90de97ef57eba64b2cd22058300ec74887/src/test/chainstate_write_tests.cpp#L210>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`777b2f90de97`](<https://git.fish.foo/bitcoin/bitcoin/commit/777b2f90de97ef57eba64b2cd22058300ec74887>) · [Full review report](<https://review.fish.foo/traces/36738-777b2f90de97ef57eba64b2cd22058300ec74887-2024-1.html>) · [Previous reports](<https://review.fish.foo/traces/36738/>) [permalink](<https://review.fish.foo/traces/36738-777b2f90de97ef57eba64b2cd22058300ec74887-2024-1.html>)
nervana21 commented 2026-11-13 22:27:59 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36738
No description provided.