refactor: pass BlockManager to ChainstateManager #36739

pull pzafonte wants to merge 3 commits into bitcoin:master from pzafonte:blockstorage-chainstate-separation changing 10 files +39 −20
  1. pzafonte commented at 12:17 AM on November 3, 2026: 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.

  2. DrahtBot added the label Refactoring on Nov 3, 2026
  3. DrahtBot commented at 12:18 AM on November 3, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK musaHaruna, nervana21

    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:

    • #36858 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte)
    • #36856 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte)
    • #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 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36735.svg"></sub> (validation: delete pruned files after a failed flush by pzafonte)
    • #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)
    • #36598 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO 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-->

  4. kernel: destroy chainstate manager 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.
    05aa0b7d38
  5. refactor: pass BlockManager to ChainstateManager
    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.
    b9a83c74bd
  6. refactor: remove ChainstateLoadOptions::prune
    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.
    bfb52b50f5
  7. pzafonte force-pushed on Nov 13, 2026
  8. pzafonte marked this as ready for review on Nov 13, 2026
  9. musaHaruna commented at 1:10 PM on November 26, 2026: contributor

    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"])
    
  10. in src/validation.cpp:6226 in bfb52b50f5
    6222 | @@ -6223,11 +6223,11 @@ static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
    6223 |      return std::move(opts);
    6224 |  }
    6225 |  
    6226 | -ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager::Options blockman_options)
    6227 | +ChainstateManager::ChainstateManager(const util::SignalInterrupt& interrupt, Options options, node::BlockManager& blockman)
    


    musaHaruna commented at 1:10 PM on November 26, 2026:

    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.

  11. nervana21 commented at 5:34 PM on November 28, 2026: contributor

    Concept ACK


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-30 03:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me