WIP: refactor: break the blockstorage/validation circular dependency #36858

Draft
pzafonte wants to merge 12 commits from blockstorage-drop-friends into master AGit
pzafonte commented 2026-11-13 16:07:28 +00:00 (Migrated from github.com)

node/blockstorage.cpp includes validation.h, and the lint lists node/blockstorage -> validation -> node/blockstorage as an expected circular dependency. This removes it, and the lint confirms the cycle is gone.

The largest user of validation in block storage is ImportBlocks, which drives validation. It moves to node/chainstate and sets the reindex flag through the new BlockManager::SetBlockfilesIndexed(), which writes the flag to the block tree database and sets the in-memory flag under one cs_main hold. The last commit moves FatalError to kernel/fatal_error.h, after which blockstorage.cpp no longer includes validation.h. validation.h still exports FatalError for its other users.

Validation also reaches into BlockManager's private members as a friend. RequestPrune() and MarkBlockIndexDirty() replace the friend declarations, keeping m_check_for_pruning and m_dirty_blockindex non-public. Both require cs_main, and the annotations let the compiler check it. After this, nothing outside BlockManager writes its private state or the block tree database.

There is no behavior change to my knowledge.

Based on #36738. Draft until it is merged.

Thanks to musaHaruna, who suggested RequestPrune() in his review on my fork. The flag marks a pending pruning check, so callers request it and storage decides whether anything qualifies, which keeps the flag private once the friend declarations are gone.

`node/blockstorage.cpp` includes `validation.h`, and the lint lists `node/blockstorage -> validation -> node/blockstorage` as an expected circular dependency. This removes it, and the lint confirms the cycle is gone. The largest user of validation in block storage is `ImportBlocks`, which drives validation. It moves to `node/chainstate` and sets the reindex flag through the new `BlockManager::SetBlockfilesIndexed()`, which writes the flag to the block tree database and sets the in-memory flag under one `cs_main` hold. The last commit moves `FatalError` to `kernel/fatal_error.h`, after which `blockstorage.cpp` no longer includes `validation.h`. `validation.h` still exports `FatalError` for its other users. Validation also reaches into `BlockManager`'s private members as a friend. `RequestPrune()` and `MarkBlockIndexDirty()` replace the friend declarations, keeping `m_check_for_pruning` and `m_dirty_blockindex` non-public. Both require `cs_main`, and the annotations let the compiler check it. After this, nothing outside `BlockManager` writes its private state or the block tree database. There is no behavior change to my knowledge. Based on #36738. Draft until it is merged. Thanks to musaHaruna, who [suggested `RequestPrune()`](https://github.com/pzafonte/bitcoin/pull/2#discussion_r4283138022) in his review on my fork. The flag marks a pending pruning check, so callers request it and storage decides whether anything qualifies, which keeps the flag private once the friend declarations are gone.
DrahtBot commented 2026-11-13 16:07:39 +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)
  • #36603 (Improve loadtxoutset performance by ajtowns)
  • #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)
  • #35731 (Indexes: Harden the flush-error notification invariant by arejula27)
  • #35071 (Reindex: save progress to continue after interruption by pinheadmz)
  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)
  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)
  • #33324 (blocks: add resumable reobfuscation for existing block files by l0rinc)

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/36858 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36858.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36858#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/36858#issuecomment-7351912325) | 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) * [#36603](https://github.com/bitcoin/bitcoin/pull/36603) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36603.svg"></sub> (Improve loadtxoutset performance by ajtowns) * [#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) * [#35731](https://github.com/bitcoin/bitcoin/pull/35731) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35731.svg"></sub> (Indexes: Harden the flush-error notification invariant by arejula27) * [#35071](https://github.com/bitcoin/bitcoin/pull/35071) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35071.svg"></sub> (Reindex: save progress to continue after interruption by pinheadmz) * [#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) * [#34132](https://github.com/bitcoin/bitcoin/pull/34132) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34132.svg"></sub> (coins,dbwrapper: make database point-read failures fatal by l0rinc) * [#33324](https://github.com/bitcoin/bitcoin/pull/33324) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33324.svg"></sub> (blocks: add resumable reobfuscation for existing block files by l0rinc) 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 · Pending-unlink cancellation lacks regression coverage

The new failure test would still pass without m_files_to_unlink.erase(nFile) because it never writes into a file while that file is queued for deletion.

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

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

The sole caller correctly sets prunedblockfiles, but the extracted storage API leaves this invariant dependent on a separate caller step that BlockManager can own.

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

🔵 Suggestion · Negative prune limits disable the upper-height filter

No current caller producing negative limits was found, but {0, -1} disables the public selectors' upper-height filter in production builds.

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

🔵 Suggestion · Forced recovery does not test deferred unlink scheduling

The forced retry in prune_flush_failure cannot distinguish the intended deferred unlink scheduling from a regression that makes pending deletions trigger another write.

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

🔵 Suggestion · Prune selection lacks a caller-side cs_main contract

Current callers hold cs_main, but the public selectors do not require it, so thread-safety analysis cannot flag unlocked selection after callers compute chainstate-dependent limits.

Location: src/node/blockstorage.h:370 (head).

🔵 Suggestion · The uncoordinated block-index writer remains public

WriteBlockStorage() is now the only caller of WriteBlockIndexDB(), but the public helper still permits bypassing the block-file flush and pending-unlink sequence.

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

Review coverage was incomplete.


Reviewed: 552b3a46a027 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:958be5f4bdca878a2804ba72de0c2dfde093e46d309983ce74930b6f8a450ddf:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:552b3a46a02718ed19ae3a9621e7ffd5a7356367 --> <!-- ralph-case:a4e14af4eb3cd59a7d8ed318d5179e4d5268c5e62934f366c2532e05b275c854 --> **🔵 Suggestion · Pending-unlink cancellation lacks regression coverage** The new failure test would still pass without `m_files_to_unlink.erase(nFile)` because it never writes into a file while that file is queued for deletion. Location: [`src/node/blockstorage.cpp:967`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/node/blockstorage.cpp#L967>) (head). **🔵 Suggestion · The pruned flag remains a caller-managed prerequisite** The sole caller correctly sets `prunedblockfiles`, but the extracted storage API leaves this invariant dependent on a separate caller step that `BlockManager` can own. Location: [`src/node/blockstorage.cpp:831`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/node/blockstorage.cpp#L831>) (head). **🔵 Suggestion · Negative prune limits disable the upper-height filter** No current caller producing negative limits was found, but `{0, -1}` disables the public selectors' upper-height filter in production builds. Location: [`src/node/blockstorage.cpp:317`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/node/blockstorage.cpp#L317>) (head). **🔵 Suggestion · Forced recovery does not test deferred unlink scheduling** The forced retry in `prune_flush_failure` cannot distinguish the intended deferred unlink scheduling from a regression that makes pending deletions trigger another write. Location: [`src/test/chainstate_write_tests.cpp:210`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/test/chainstate_write_tests.cpp#L210>) (head). **🔵 Suggestion · Prune selection lacks a caller-side `cs_main` contract** Current callers hold `cs_main`, but the public selectors do not require it, so thread-safety analysis cannot flag unlocked selection after callers compute chainstate-dependent limits. Location: [`src/node/blockstorage.h:370`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/node/blockstorage.h#L370>) (head). **🔵 Suggestion · The uncoordinated block-index writer remains public** `WriteBlockStorage()` is now the only caller of `WriteBlockIndexDB()`, but the public helper still permits bypassing the block-file flush and pending-unlink sequence. Location: [`src/node/blockstorage.cpp:849`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367/src/node/blockstorage.cpp#L849>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`552b3a46a027`](<https://git.fish.foo/bitcoin/bitcoin/commit/552b3a46a02718ed19ae3a9621e7ffd5a7356367>) · [Review report](<https://review.fish.foo/report/36858-552b3a46a02718ed19ae3a9621e7ffd5a7356367-2647-1.html>) · [Previous reports](<https://review.fish.foo/traces/36858/>) [permalink](<https://review.fish.foo/report/36858-552b3a46a02718ed19ae3a9621e7ffd5a7356367-2647-1.html>)
nervana21 commented 2026-12-02 16:25:01 +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!36858
No description provided.