refactor: break the blockstorage/validation circular dependency #36858

pull pzafonte wants to merge 12 commits into bitcoin:master from pzafonte:blockstorage-drop-friends changing 11 files +511 −274
  1. pzafonte commented at 4:07 PM on November 13, 2026: contributor

    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.

  2. test: apply BlockManager args in test fixtures
    Without this, -prune and -fastprune in extra_args have no effect.
    24ad36d929
  3. test: check flush retry after failure
    Move the blk file aside instead of deleting it, so it can be restored.
    2334d27a24
  4. test: characterize a failed prune flush
    The pruned blk/rev files are not deleted, even after a later flush
    succeeds.
    108d7ff03c
  5. validation: delete pruned files on a later flush
    Files selected for pruning were kept in a local, so a failed flush left
    them on disk until the next startup.
    
    Keep them in BlockManager and delete them after the next block index
    write. Take them with the dirty entries, which WriteBlockIndexDB() drops
    even if the write fails. Drop a file from the set once a block is
    written to it.
    
    Pending files wait for the next periodic, forced or shutdown flush
    rather than forcing one, which would retry a failing write on every
    block.
    d224fb8721
  6. refactor: let BlockManager write block storage
    Move the block and undo file flush, block index write and pruned file
    deletion from FlushStateToDisk into BlockManager. Their bench log lines
    change from FlushStateToDisk to WriteBlockStorage.
    fb911aecde
  7. refactor: apply prune locks in BlockManager
    Validation read BlockManager's prune locks directly. Move that code and
    PRUNE_LOCK_BUFFER into BlockManager.
    14de6dfdf5
  8. refactor: pass prune limits to BlockManager
    BlockManager read the chainstate to decide what to prune. Decide in
    validation and pass the height range and sizes instead. An Assume checks
    the heights are non-negative.
    ae7d92d02e
  9. refactor: move prune bookkeeping into BlockManager
    Move FlushStateToDisk's handling of the prune request, prune locks,
    pruned flag, files to delete and blocks directory space check into
    BlockManager. The prune request is cleared before the prune locks are
    applied rather than after selection, which differs only if selection
    throws.
    
    An Assume checks that the pruned flag is set before files are queued
    for deletion, so a restart knows blocks were deleted.
    777b2f90de
  10. refactor: add BlockManager::SetBlockfilesIndexed
    ImportBlocks wrote the reindex flag through BlockManager's database and
    then set m_blockfiles_indexed. Let BlockManager do both.
    d59c161b35
  11. refactor: move ImportBlocks to node/chainstate
    ImportBlocks drives validation, so it does not belong in block storage.
    ffb03bfbae
  12. refactor: drop BlockManager's friend declarations
    Validation only used the prune check flag and the dirty block index
    entries. Add RequestPrune() and MarkBlockIndexDirty() for them.
    d216d55c65
  13. refactor: move FatalError out of validation
    node/blockstorage.cpp only included validation.h for cs_main and
    FatalError. Move FatalError to kernel/fatal_error.h and include
    kernel/cs_main.h instead. This removes the circular dependency
    between node/blockstorage and validation.
    552b3a46a0
  14. DrahtBot added the label Refactoring on Nov 13, 2026
  15. DrahtBot commented at 4:07 PM on November 13, 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. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36854 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte)
    • #36743 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36743.svg"></sub> ([RFC] build: build with -Wmissing-declarations/prototypes by fanquake)
    • #36739 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager 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)
    • #36548 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36548.svg"></sub> (net: serve MSG_BLOCK by stripping witnesses in place by l0rinc)
    • #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)
    • #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 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35731.svg"></sub> (Indexes: Harden the flush-error notification invariant by arejula27)
    • #35570 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35570.svg"></sub> (refactor: Change some validation.cpp methods to return BlockValidationState by optout21)
    • #35071 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35071.svg"></sub> (Reindex: save progress to continue after interruption by pinheadmz)
    • #34534 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34534.svg"></sub> (rpc: Manual prune lock management (Take 2) by fjahr)
    • #33324 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33324.svg"></sub> (blocks: add resumable reobfuscation for existing block files by l0rinc)
    • #31260 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/31260.svg"></sub> (scripted-diff: Type-safe settings retrieval by ryanofsky)
    • #26022 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/26022.svg"></sub> (Add util::ResultPtr class by ryanofsky)
    • #25665 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/25665.svg"></sub> (refactor: Add util::Result failure types and ability to merge result values by ryanofsky)

    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-->


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-19 19:51 UTC

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