refactor: pass BlockManager to ChainstateManager #36739

Open
pzafonte wants to merge 3 commits from blockstorage-chainstate-separation into master AGit
pzafonte commented 2026-11-03 00:17:57 +00:00 (Migrated from github.com)

ChainstateManager creates and owns its BlockManager, which means block storage can't exist without chainstate management. #35627 discussed moving that ownership out of ChainstateManager, and the kernel needs it to configure, read and prune block storage on its own.

NodeContext and the kernel now create the BlockManager first, pass it to ChainstateManager by reference and destroy ChainstateManager first.

The first commit makes the kernel's ChainMan destroy its ChainstateManager explicitly. On master ChainMan declares its context last, which destroys the context before the ChainstateManager that refers into it. Nothing reads through those references during teardown, so this changes no behavior, but the order no longer depends on how the members are declared.

The last commit removes ChainstateLoadOptions::prune. It duplicated the BlockManager's prune mode, and the kernel never set it. Once the kernel can prune (#36856), that would make it refuse to reopen a data directory it had pruned.

The only behavior change to my knowledge is startup log order, because the block tree database opens before the script verification threads start.

An earlier version of this PR also moved ImportBlocks and replaced BlockManager's friend declarations. I've taken them out as they're not relevant to this, but I think together better suited for a change that removes the node/blockstorage -> validation circular dependency (#36858)

The kernel side is #36854.

`ChainstateManager` creates and owns its `BlockManager`, which means block storage can't exist without chainstate management. #35627 discussed moving that ownership out of `ChainstateManager`, and the kernel needs it to configure, read and prune block storage on its own. `NodeContext` and the kernel now create the `BlockManager` first, pass it to `ChainstateManager` by reference and destroy `ChainstateManager` first. The first commit makes the kernel's `ChainMan` destroy its `ChainstateManager` explicitly. On master `ChainMan` declares its context last, which destroys the context before the `ChainstateManager` that refers into it. Nothing reads through those references during teardown, so this changes no behavior, but the order no longer depends on how the members are declared. The last commit removes `ChainstateLoadOptions::prune`. It duplicated the `BlockManager`'s prune mode, and the kernel never set it. Once the kernel can prune (#36856), that would make it refuse to reopen a data directory it had pruned. The only behavior change to my knowledge is startup log order, because the block tree database opens before the script verification threads start. An earlier version of this PR also moved `ImportBlocks` and replaced `BlockManager`'s friend declarations. I've taken them out as they're not relevant to this, but I think together better suited for a change that removes the `node/blockstorage -> validation` circular dependency (#36858) The kernel side is #36854.
DrahtBot commented 2026-11-03 00:18:08 +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
ACK nervana21
Concept ACK musaHaruna

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)
  • #36858 (refactor: break the blockstorage/validation circular dependency by pzafonte)
  • #36856 (kernel: add manual pruning by pzafonte)
  • #36738 (refactor: move block writes and pruning into BlockManager by pzafonte)
  • #36735 (validation: delete pruned files after a failed flush by pzafonte)
  • #36621 (node: batch block file pruning in a single pass over block index by mutatrum)

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/36739 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36739.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36739#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 | | ---- | --------- | | ACK | [nervana21](https://github.com/bitcoin/bitcoin/pull/36739#issuecomment-7312918596) | | Concept ACK | [musaHaruna](https://github.com/bitcoin/bitcoin/pull/36739#pullrequestreview-6074688107) | 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) * [#36858](https://github.com/bitcoin/bitcoin/pull/36858) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte) * [#36856](https://github.com/bitcoin/bitcoin/pull/36856) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte) * [#36738](https://github.com/bitcoin/bitcoin/pull/36738) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36738.svg"></sub> (refactor: move block writes and pruning into BlockManager by pzafonte) * [#36735](https://github.com/bitcoin/bitcoin/pull/36735) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36735.svg"></sub> (validation: delete pruned files after a failed flush 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) 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 · Constructor permits inconsistent startup interrupts

No current caller supplies different interrupts, but the new constructor permits a mismatch that can delay cancellation inside block-index loading or report an interruption as a database-load failure.

Location: src/validation.cpp:6226 (head).

🔵 Suggestion · Description promises encapsulation changes absent from this revision

The description promises accessor and ImportBlocks changes absent from this revision, misrepresenting the scope of the ownership refactor.

Location: src/validation.h:1049 (head).

🔵 Suggestion · Kernel tests leave the final-owner Context lifetime uncovered

Existing kernel tests retain an external Context until after ChainMan destruction, so they do not exercise the final-owner lifetime affected by this refactor; internal context retention itself predates the PR.

Location: src/kernel/bitcoinkernel.cpp:488 (head).

🔵 Suggestion · Pruning tests miss the pruned-to-unpruned startup guard

Although this rewrite preserves node startup behavior, the existing manual-pruning test would not detect accidental acceptance of a pruned database with -prune=0.

Location: src/node/chainstate.cpp:57 (head).

Review coverage was incomplete.


Reviewed: bfb52b50f5b7 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:464886e574fc96b3587e582688c29b6bd7a73bba755cfc1d2f031cc648c7ec97:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:bfb52b50f5b7655967b95f0129cea8caf4c97f7b --> <!-- ralph-case:fad369d99d5a98e66d2ab7f8b02f3bfce841cafcc2c7e5a2386d430108bcbd03 --> **🔵 Suggestion · Constructor permits inconsistent startup interrupts** No current caller supplies different interrupts, but the new constructor permits a mismatch that can delay cancellation inside block-index loading or report an interruption as a database-load failure. Location: [`src/validation.cpp:6226`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bfb52b50f5b7655967b95f0129cea8caf4c97f7b/src/validation.cpp#L6226>) (head). **🔵 Suggestion · Description promises encapsulation changes absent from this revision** The description promises accessor and `ImportBlocks` changes absent from this revision, misrepresenting the scope of the ownership refactor. Location: [`src/validation.h:1049`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bfb52b50f5b7655967b95f0129cea8caf4c97f7b/src/validation.h#L1049>) (head). **🔵 Suggestion · Kernel tests leave the final-owner Context lifetime uncovered** Existing kernel tests retain an external `Context` until after `ChainMan` destruction, so they do not exercise the final-owner lifetime affected by this refactor; internal context retention itself predates the PR. Location: [`src/kernel/bitcoinkernel.cpp:488`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bfb52b50f5b7655967b95f0129cea8caf4c97f7b/src/kernel/bitcoinkernel.cpp#L488>) (head). **🔵 Suggestion · Pruning tests miss the pruned-to-unpruned startup guard** Although this rewrite preserves node startup behavior, the existing manual-pruning test would not detect accidental acceptance of a pruned database with `-prune=0`. Location: [`src/node/chainstate.cpp:57`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bfb52b50f5b7655967b95f0129cea8caf4c97f7b/src/node/chainstate.cpp#L57>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`bfb52b50f5b7`](<https://git.fish.foo/bitcoin/bitcoin/commit/bfb52b50f5b7655967b95f0129cea8caf4c97f7b>) · [Review report](<https://review.fish.foo/report/36739-bfb52b50f5b7655967b95f0129cea8caf4c97f7b-2639-2.html>) · [Previous reports](<https://review.fish.foo/traces/36739/>) [permalink](<https://review.fish.foo/report/36739-bfb52b50f5b7655967b95f0129cea8caf4c97f7b-2639-2.html>)
musaHaruna (Migrated from github.com) reviewed 2026-11-26 13:10:09 +00:00
musaHaruna (Migrated from github.com) left a comment

Concept ACK.

This follows the ownership split we discussed in #35627: the caller owns BlockManager, and ChainstateManager borrows it. I think this is a useful first step toward separating storage from validation.

Could we cover this in feature_pruning.py, at the end of manual_test?

-        # stop node, start back up with auto-prune at 550 MiB, make sure still runs
-        self.restart_node(node_number, extra_args=["-prune=550"])
+        self.stop_node(node_number)
+        node.assert_start_raises_init_error(
+            extra_args=["-prune=0"],
+            expected_msg="You need to rebuild the database using -reindex to go back to unpruned mode."
+            f"  This will redownload the entire blockchain.{os.linesep}"
+            "Please restart with -reindex or -reindex-chainstate to recover.",
+        )
+        self.start_node(node_number, extra_args=["-prune=550"])
Concept ACK. This follows the ownership split we discussed in [#35627](https://github.com/bitcoin/bitcoin/issues/35627): the caller owns `BlockManager`, and `ChainstateManager` borrows it. I think this is a useful first step toward separating storage from validation. Could we cover this in `feature_pruning.py`, at the end of `manual_test`? ```diff - # stop node, start back up with auto-prune at 550 MiB, make sure still runs - self.restart_node(node_number, extra_args=["-prune=550"]) + self.stop_node(node_number) + node.assert_start_raises_init_error( + extra_args=["-prune=0"], + expected_msg="You need to rebuild the database using -reindex to go back to unpruned mode." + f" This will redownload the entire blockchain.{os.linesep}" + "Please restart with -reindex or -reindex-chainstate to recover.", + ) + self.start_node(node_number, extra_args=["-prune=550"]) ```
musaHaruna (Migrated from github.com) reviewed 2026-11-26 13:10:24 +00:00
musaHaruna (Migrated from github.com) commented 2026-11-26 13:10:24 +00:00

Could we drop the separate interrupt argument and use blockman.m_interrupt? Current callers already share the same signal, but the constructor allows different ones.

For example, if only storage’s signal is interrupted, block-index loading can return false because it was asked to stop. The chainstate loader then checks chainman.m_interrupt. If that separate signal is still clear, it reports “Error loading block database”, even though the database is healthy.

Taking the signal from blockman would prevent this mismatch.

-ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager& blockman)
+ChainstateManager::ChainstateManager(Options options, node::BlockManager& blockman)
     : m_script_check_queue{/*batch_size=*/128, std::clamp(options.worker_threads_num, 0, MAX_SCRIPTCHECK_THREADS)},
-      m_interrupt{interrupt},
+      m_interrupt{blockman.m_interrupt},

The declaration and callers would also need the interrupt argument removed.

Could we drop the separate interrupt argument and use `blockman.m_interrupt`? Current callers already share the same signal, but the constructor allows different ones. For example, if only storage’s signal is interrupted, block-index loading can return `false` because it was asked to stop. The chainstate loader then checks `chainman.m_interrupt`. If that separate signal is still clear, it reports “Error loading block database”, even though the database is healthy. Taking the signal from `blockman` would prevent this mismatch. ```diff -ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager& blockman) +ChainstateManager::ChainstateManager(Options options, node::BlockManager& blockman) : m_script_check_queue{/*batch_size=*/128, std::clamp(options.worker_threads_num, 0, MAX_SCRIPTCHECK_THREADS)}, - m_interrupt{interrupt}, + m_interrupt{blockman.m_interrupt}, ``` The declaration and callers would also need the interrupt argument removed.
nervana21 commented 2026-11-28 17:34:06 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
nervana21 (Migrated from github.com) reviewed 2026-12-01 13:31:53 +00:00
nervana21 (Migrated from github.com) commented 2026-12-01 13:31:53 +00:00

b9a83c74bd: refactor: pass BlockManager to ChainstateManager

Consider annotating this.

    //! Caller owns BlockManager. It must outlive chainman, which holds a reference into it.
    std::unique_ptr<BlockManager> blockman;
b9a83c74bdb2f1bfe55aba4e565b796e0e62932c: refactor: pass BlockManager to ChainstateManager Consider annotating this. ```suggestion //! Caller owns BlockManager. It must outlive chainman, which holds a reference into it. std::unique_ptr<BlockManager> blockman; ```
nervana21 (Migrated from github.com) reviewed 2026-12-01 13:33:39 +00:00
nervana21 (Migrated from github.com) commented 2026-12-01 13:33:39 +00:00

b9a83c74bd: refactor: pass BlockManager to ChainstateManager

Consider making ordering concerns clear here too

        // Destroy chainman before blockman. ChainstateManager holds a
        // BlockManager reference, and both hold references into m_context.
b9a83c74bdb2f1bfe55aba4e565b796e0e62932c: refactor: pass BlockManager to ChainstateManager Consider making ordering concerns clear here too ```suggestion // Destroy chainman before blockman. ChainstateManager holds a // BlockManager reference, and both hold references into m_context. ```
nervana21 commented 2026-12-01 13:37:34 +00:00 (Migrated from github.com)

tACK bfb52b50f5

Agree that this is a nice first step towards decoupling storage and validation. Aside from logging order changes noted by OP, I did not find that this intended refactoring led to any behavior changes.

Left minor suggestions that are non-blocking.

tACK bfb52b50f5b7655967b95f0129cea8caf4c97f7b Agree that this is a nice first step towards decoupling storage and validation. Aside from logging order changes noted by OP, I did not find that this intended refactoring led to any behavior changes. Left minor suggestions that are non-blocking.
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!36739
No description provided.