Skip to content

kernel: let callers create the block manager - #36854

Draft
pzafonte wants to merge 4 commits into
bitcoin:masterfrom
pzafonte:kernel-create-block-manager
Draft

pzafonte wants to merge 4 commits into
bitcoin:masterfrom
pzafonte:kernel-create-block-manager

Conversation

@pzafonte

Copy link
Copy Markdown
Contributor

The kernel creates its block storage inside btck_chainstate_manager_create, configures it through the chainstate manager's options and reads blocks through the chainstate manager. Pruning (#35627) needs block storage that the caller can configure, keep and prune directly.

This adds btck_BlockManagerOptions and btck_BlockManager. btck_chainstate_manager_create takes a block manager and keeps a shared reference to it, so either handle can be destroyed first. A block manager can be passed to only one chainstate manager, and a failed creation still uses it up unless the contexts differ. btck_block_read and btck_block_spent_outputs_read take the block manager, and block tree entries are valid for its lifetime, which can be longer than the chainstate manager's.

Changes for callers:

  • btck_chainstate_manager_options_create no longer takes a blocks directory.
  • btck_chainstate_manager_options_set_wipe_dbs is replaced by btck_block_manager_options_update_wipe_block_tree_db and btck_chainstate_manager_options_update_wipe_chainstate_db. The block tree db is wiped when the block manager is created, and wiping it also wipes the chainstate db, which the old function required anyway.
  • btck_chainstate_manager_options_update_block_tree_db_in_memory moves to the block manager options.
  • Both options objects take the same total database cache, and each uses its own share.

Based on #36739. Draft until it is merged. Feedback on the API is welcome in the meantime.

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.
Block storage was configured and read through the chainstate manager.
Add btck_BlockManagerOptions and btck_BlockManager, and pass the block
manager to btck_chainstate_manager_create, which keeps a shared
reference, so either can be destroyed first. A block manager serves
one chainstate manager.

btck_block_read and btck_block_spent_outputs_read take the block
manager. Each options object takes the total database cache and uses
its part. Wiping the block tree db also wipes the chainstate db.
@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.
A summary of reviews will appear here.

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)
  • #36167 (build: Enable -Wunused by fanquake)
  • #35557 (kernel, validation: Add btck_chainstate_manager_set_clock_time by ryanofsky)
  • #35187 (kernel: Block validation without a complete UTXO set by sedited)

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.

LLM Linter (✨ experimental)

Possible places where comparison-specific test macros should replace generic comparisons:

  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(BlockManagerOptions(context, data_dir, blocks_dir), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a HasReason predicate that checks the failure message.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(ChainstateManagerOptions(context, ""), std::runtime_error) and the null-path equivalent → Prefer BOOST_CHECK_EXCEPTION with a HasReason predicate.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(ChainMan(context, chainman_opts, blockman), std::runtime_error) for the context mismatch and already-used block manager → Prefer BOOST_CHECK_EXCEPTION with a HasReason predicate.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(tip), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a HasReason predicate.

2026-11-13 15:09:15

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.

2 participants