Skip to content

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

Draft
pzafonte wants to merge 12 commits into
bitcoin:masterfrom
pzafonte:blockstorage-drop-friends
Draft

pzafonte wants to merge 12 commits into
bitcoin:masterfrom
pzafonte:blockstorage-drop-friends

Conversation

@pzafonte

Copy link
Copy Markdown
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.

Without this, -prune and -fastprune in extra_args have no effect.
Move the blk file aside instead of deleting it, so it can be restored.
The pruned blk/rev files are not deleted, even after a later flush
succeeds.
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.
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.
Validation read BlockManager's prune locks directly. Move that code and
PRUNE_LOCK_BUFFER into 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.
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.
ImportBlocks wrote the reindex flag through BlockManager's database and
then set m_blockfiles_indexed. Let BlockManager do both.
ImportBlocks drives validation, so it does not belong in block storage.
Validation only used the prune check flag and the dirty block index
entries. Add RequestPrune() and MarkBlockIndexDirty() for them.
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.
@DrahtBot

DrahtBot commented Nov 13, 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
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.

@nervana21

Copy link
Copy Markdown
Contributor

Concept ACK

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.

3 participants