WIP: kernel: let callers create the block manager #36854

Draft
pzafonte wants to merge 4 commits from kernel-create-block-manager into master AGit
pzafonte commented 2026-11-13 15:08:37 +00:00 (Migrated from github.com)

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.

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.
DrahtBot commented 2026-11-13 15:08:48 +00:00 (Migrated from github.com)

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

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36854 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36854.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36854#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37094](https://github.com/bitcoin/bitcoin/pull/37094) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37094.svg"></sub> (validation: validate assumeutxo snapshots with a single chainstate by fjahr) * [#36858](https://github.com/bitcoin/bitcoin/pull/36858) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte) * [#36856](https://github.com/bitcoin/bitcoin/pull/36856) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36856.svg"></sub> (kernel: add manual pruning by pzafonte) * [#36738](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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) * [#36167](https://github.com/bitcoin/bitcoin/pull/36167) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36167.svg"></sub> (build: Enable `-Wunused` by fanquake) * [#35557](https://github.com/bitcoin/bitcoin/pull/35557) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35557.svg"></sub> (kernel, validation: Add btck_chainstate_manager_set_clock_time by ryanofsky) * [#35187](https://github.com/bitcoin/bitcoin/pull/35187) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35187.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### 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. <sup>2026-11-13 15:09:15</sup>
First-time contributor

🟠 Minor · Discarding a wiping block manager loses the pending chainstate wipe

Discarding a disk-backed wiping block manager before attachment makes a populated datadir fail to reopen with defaults; no current in-tree caller producing this sequence was found.

Location: src/kernel/bitcoinkernel.cpp:1361 (head).

🔵 Suggestion · One-use tests can pass through unrelated DB-lock failures

The new lifecycle assertions do not distinguish the documented consumption rules from context rejection or a live coins-database lock.

Location: src/test/kernel/test_kernel.cpp:912 (head).

🔵 Suggestion · Independent data directories lack a persisted-state compatibility contract

The split options permit incompatible index and chainstate pairings without documenting the requirement; no current in-tree caller was found doing this, and the loader's unchecked snapshot assumption predates this PR.

Location: src/kernel/bitcoinkernel.h:1392 (head).

🔵 Suggestion · Independent cache setters do not enforce a shared total

Setting only the chainstate total to 4 MiB leaves 5.5 MiB of combined configured caches, despite the header describing shares of the same total.

Location: src/kernel/bitcoinkernel.cpp:1256 (head).

🔵 Suggestion · Lifetime reads retain an extra context owner

Neither lifetime test leaves BlockMan as the sole context owner, so the new post-chainstate read coverage does not isolate BlockMan's context ownership.

Location: src/test/kernel/test_kernel.cpp:1246 (head).

🔵 Suggestion · Pruning restarts do not cover rejection with pruning disabled

The rewritten prune-mode guard retains a pre-existing coverage gap: pruning tests restart with pruning enabled, so they do not verify rejection of a pruned datadir reopened without pruning.

Location: src/node/chainstate.cpp:57 (head).

Review coverage was incomplete.


Reviewed: d26ba9d2e242 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:464886e574fc96b3587e582688c29b6bd7a73bba755cfc1d2f031cc648c7ec97:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:d26ba9d2e242104c6db9cc09172ac81bf7ce99d8 --> <!-- ralph-case:73e7a97bf15f1088f72634d32d2d2f2b79301bc3efd68bcbeb2f6f0a619ccf01 --> **🟠 Minor · Discarding a wiping block manager loses the pending chainstate wipe** Discarding a disk-backed wiping block manager before attachment makes a populated datadir fail to reopen with defaults; no current in-tree caller producing this sequence was found. Location: [`src/kernel/bitcoinkernel.cpp:1361`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/kernel/bitcoinkernel.cpp#L1361>) (head). **🔵 Suggestion · One-use tests can pass through unrelated DB-lock failures** The new lifecycle assertions do not distinguish the documented consumption rules from context rejection or a live coins-database lock. Location: [`src/test/kernel/test_kernel.cpp:912`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/test/kernel/test_kernel.cpp#L912>) (head). **🔵 Suggestion · Independent data directories lack a persisted-state compatibility contract** The split options permit incompatible index and chainstate pairings without documenting the requirement; no current in-tree caller was found doing this, and the loader's unchecked snapshot assumption predates this PR. Location: [`src/kernel/bitcoinkernel.h:1392`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/kernel/bitcoinkernel.h#L1392>) (head). **🔵 Suggestion · Independent cache setters do not enforce a shared total** Setting only the chainstate total to 4 MiB leaves 5.5 MiB of combined configured caches, despite the header describing shares of the same total. Location: [`src/kernel/bitcoinkernel.cpp:1256`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/kernel/bitcoinkernel.cpp#L1256>) (head). **🔵 Suggestion · Lifetime reads retain an extra context owner** Neither lifetime test leaves BlockMan as the sole context owner, so the new post-chainstate read coverage does not isolate BlockMan's context ownership. Location: [`src/test/kernel/test_kernel.cpp:1246`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/test/kernel/test_kernel.cpp#L1246>) (head). **🔵 Suggestion · Pruning restarts do not cover rejection with pruning disabled** The rewritten prune-mode guard retains a pre-existing coverage gap: pruning tests restart with pruning enabled, so they do not verify rejection of a pruned datadir reopened without pruning. Location: [`src/node/chainstate.cpp:57`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8/src/node/chainstate.cpp#L57>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d26ba9d2e242`](<https://git.fish.foo/bitcoin/bitcoin/commit/d26ba9d2e242104c6db9cc09172ac81bf7ce99d8>) · [Review report](<https://review.fish.foo/report/36854-d26ba9d2e242104c6db9cc09172ac81bf7ce99d8-2640-1.html>) · [Previous reports](<https://review.fish.foo/traces/36854/>) [permalink](<https://review.fish.foo/report/36854-d26ba9d2e242104c6db9cc09172ac81bf7ce99d8-2640-1.html>)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36854
No description provided.