Skip to content

refactor: pass BlockManager to ChainstateManager - #36739

Open
pzafonte wants to merge 3 commits into
bitcoin:masterfrom
pzafonte:blockstorage-chainstate-separation
Open

pzafonte wants to merge 3 commits into
bitcoin:masterfrom
pzafonte:blockstorage-chainstate-separation

Conversation

@pzafonte

@pzafonte pzafonte commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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

DrahtBot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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.

ChainMan declares m_context last, which destroys it before the
ChainstateManager that refers into it. Reset the chainstate manager
explicitly, making the order independent of member declaration order.
Create the BlockManager first and pass it in by reference. NodeContext
and the kernel own both and destroy ChainstateManager first.

The block tree database opens before the script verification threads
start, which swaps their startup log lines.
The flag duplicated the BlockManager's prune mode. Init and the test
setup copied it, and the kernel left it false. Check the BlockManager
directly, keeping one source of truth before the kernel can enable
pruning.

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"])

Comment thread src/validation.cpp
@@ -6223,11 +6223,11 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
return std::move(opts);
}

ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager& blockman)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nervana21

Copy link
Copy Markdown
Contributor

Concept ACK

Comment thread src/node/context.h
std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man;
std::unique_ptr<PeerManager> peerman;
std::unique_ptr<TorController> tor_controller;
std::unique_ptr<BlockManager> blockman;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b9a83c7: refactor: pass BlockManager to ChainstateManager

Consider annotating this.

Suggested change
std::unique_ptr<BlockManager> blockman;
//! Caller owns BlockManager. It must outlive chainman, which holds a reference into it.
std::unique_ptr<BlockManager> blockman;


~ChainMan()
{
// Both hold references into m_context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b9a83c7: refactor: pass BlockManager to ChainstateManager

Consider making ordering concerns clear here too

Suggested change
// Both hold references into m_context
// Destroy chainman before blockman. ChainstateManager holds a
// BlockManager reference, and both hold references into m_context.

@nervana21

Copy link
Copy Markdown
Contributor

tACK bfb52b5

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants