WIP: kernel: add manual pruning #36856

Draft
pzafonte wants to merge 13 commits from kernel-prune-up-to-height into master AGit
pzafonte commented 2026-11-13 15:25:04 +00:00 (Migrated from github.com)

Part of #35627 and kernel-node/kernel-node#111. The kernel can't open a data directory that a pruned node created, and it has no way to prune.

This adds a prune option on the block manager, btck_block_manager_prune_up_to_height and btck_chainstate_manager_flush. With the option set, the kernel opens pruned data directories. The chainstate manager created with the block manager decides what may be deleted, so pruning fails without one.

Pruning writes only block storage, never the chainstate. After an unclean shutdown the chainstate on disk is at the last flush, and recovering from there needs the blocks above it. Pruning therefore keeps every block above the fork point between the last flushed block and the active chain, and reports btck_PruneStatus_NEEDS_FLUSH when that bound limited what it could delete. The caller can flush and prune again.

The fork point matters when the flushed block was reorged out. Recovery then disconnects the stale blocks, which needs their block and undo data, and connects the new ones from their block data. btck_block_manager_prune_reorg_tests covers that case and fails if the bound uses the flushed block's height instead.

A fast prune option lets tests prune, since test chains never fill a 128 MiB block file.

Based on #36738 and #36854. Draft until both are merged.

Part of #35627 and kernel-node/kernel-node#111. The kernel can't open a data directory that a pruned node created, and it has no way to prune. This adds a prune option on the block manager, `btck_block_manager_prune_up_to_height` and `btck_chainstate_manager_flush`. With the option set, the kernel opens pruned data directories. The chainstate manager created with the block manager decides what may be deleted, so pruning fails without one. Pruning writes only block storage, never the chainstate. After an unclean shutdown the chainstate on disk is at the last flush, and recovering from there needs the blocks above it. Pruning therefore keeps every block above the fork point between the last flushed block and the active chain, and reports `btck_PruneStatus_NEEDS_FLUSH` when that bound limited what it could delete. The caller can flush and prune again. The fork point matters when the flushed block was reorged out. Recovery then disconnects the stale blocks, which needs their block and undo data, and connects the new ones from their block data. `btck_block_manager_prune_reorg_tests` covers that case and fails if the bound uses the flushed block's height instead. A fast prune option lets tests prune, since test chains never fill a 128 MiB block file. Based on #36738 and #36854. Draft until both are merged.
DrahtBot commented 2026-11-13 15:25:16 +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)
  • #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)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #36167 (build: Enable -Wunused by fanquake)
  • #36049 (streams: handle buffered write failures outside the destructor by l0rinc)
  • #36033 ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko)
  • #35557 (kernel, validation: Add btck_chainstate_manager_set_clock_time by ryanofsky)
  • #35187 (kernel: Block validation without a complete UTXO set by sedited)
  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)

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(..., std::runtime_error, HasReason{"..."}) to verify the failure reason.
  • src/test/kernel/test_kernel.cpp: Both BOOST_CHECK_THROW checks for empty ChainstateManagerOptions paths → Prefer BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."}).
  • src/test/kernel/test_kernel.cpp: The BOOST_CHECK_THROW checks for a different context and for reusing a block manager → Prefer BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."}) to check the specific failure message.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(create_chainman(...), std::runtime_error) when opening a pruned directory without pruning enabled → Prefer BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."}).
  • src/test/kernel/test_kernel.cpp: The two BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(...), std::runtime_error) checks after spent-output data is unavailable → Prefer BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."}).

2026-11-13 15:25:50

<!--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/36856 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36856.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36856#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) * [#37028](https://github.com/bitcoin/bitcoin/pull/37028) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37028.svg"></sub> (validation: don't report unstored blocks as new by l0rinc) * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#36854](https://github.com/bitcoin/bitcoin/pull/36854) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte) * [#36739](https://github.com/bitcoin/bitcoin/pull/36739) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager 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) * [#36326](https://github.com/bitcoin/bitcoin/pull/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) * [#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) * [#36049](https://github.com/bitcoin/bitcoin/pull/36049) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36049.svg"></sub> (streams: handle buffered write failures outside the destructor by l0rinc) * [#36033](https://github.com/bitcoin/bitcoin/pull/36033) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36033.svg"></sub> ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko) * [#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) * [#34534](https://github.com/bitcoin/bitcoin/pull/34534) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34534.svg"></sub> (rpc: Manual prune lock management (Take 2) by fjahr) 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(..., std::runtime_error, HasReason{"..."})` to verify the failure reason. - `src/test/kernel/test_kernel.cpp`: Both `BOOST_CHECK_THROW` checks for empty `ChainstateManagerOptions` paths → Prefer `BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."})`. - `src/test/kernel/test_kernel.cpp`: The `BOOST_CHECK_THROW` checks for a different context and for reusing a block manager → Prefer `BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."})` to check the specific failure message. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(create_chainman(...), std::runtime_error)` when opening a pruned directory without pruning enabled → Prefer `BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."})`. - `src/test/kernel/test_kernel.cpp`: The two `BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(...), std::runtime_error)` checks after spent-output data is unavailable → Prefer `BOOST_CHECK_EXCEPTION(..., std::runtime_error, HasReason{"..."})`. <sup>2026-11-13 15:25:50</sup>
First-time contributor

🟠 Minor · Storage exceptions escape the pruning C API

A local storage exception during an eligible prune bypasses the documented error return and fatalError; no current test or caller producing a block-tree write failure was found.

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

🔵 Suggestion · An in-memory flush satisfies the durable pruning bound

With an in-memory coins database and disk-backed block storage, pruning can remove blocks needed for rebuilding after restart; the API does not define whether this combination is disposable-only.

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

🔵 Suggestion · Wipe-plus-prune file deletion is missing from the API docs

Combining pruning and block-tree wiping deletes block and undo files during block-manager creation, before import or manual pruning, but the new option docs describe only database wiping.

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

🔵 Suggestion · NEEDS_FLUSH can be set when no file is held back

The height-only status can prompt a forced coins-cache sync even when flushing cannot make another file eligible for deletion.

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

🔵 Suggestion · Reorg recovery after a persisted prune is not tested

The reorg fixture checks in-process readability but never reloads a persisted prune while the coins database still points to the stale branch.

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

🔵 Suggestion · Deleting prunes do not test the no-chainstate-flush invariant

The kernel prune tests delete files only immediately after an explicit flush, so they do not detect a chainstate sync added only to the deleting path.

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

Review coverage was incomplete.


Reviewed: 2e21a991cbc3 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:464886e574fc96b3587e582688c29b6bd7a73bba755cfc1d2f031cc648c7ec97:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:2e21a991cbc30bb974188eb4ff96d1ca6b0906d6 --> <!-- ralph-case:3c8b720e1331f858ea0694f0ff76c93239ae4e6855376e18717db60dd0124ebe --> **🟠 Minor · Storage exceptions escape the pruning C API** A local storage exception during an eligible prune bypasses the documented error return and `fatalError`; no current test or caller producing a block-tree write failure was found. Location: [`src/kernel/bitcoinkernel.cpp:1345`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/kernel/bitcoinkernel.cpp#L1345>) (head). **🔵 Suggestion · An in-memory flush satisfies the durable pruning bound** With an in-memory coins database and disk-backed block storage, pruning can remove blocks needed for rebuilding after restart; the API does not define whether this combination is disposable-only. Location: [`src/kernel/bitcoinkernel.cpp:1336`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/kernel/bitcoinkernel.cpp#L1336>) (head). **🔵 Suggestion · Wipe-plus-prune file deletion is missing from the API docs** Combining pruning and block-tree wiping deletes block and undo files during block-manager creation, before import or manual pruning, but the new option docs describe only database wiping. Location: [`src/kernel/bitcoinkernel.h:1255`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/kernel/bitcoinkernel.h#L1255>) (head). **🔵 Suggestion · `NEEDS_FLUSH` can be set when no file is held back** The height-only status can prompt a forced coins-cache sync even when flushing cannot make another file eligible for deletion. Location: [`src/kernel/bitcoinkernel.cpp:1338`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/kernel/bitcoinkernel.cpp#L1338>) (head). **🔵 Suggestion · Reorg recovery after a persisted prune is not tested** The reorg fixture checks in-process readability but never reloads a persisted prune while the coins database still points to the stale branch. Location: [`src/test/kernel/test_kernel.cpp:1446`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/test/kernel/test_kernel.cpp#L1446>) (head). **🔵 Suggestion · Deleting prunes do not test the no-chainstate-flush invariant** The kernel prune tests delete files only immediately after an explicit flush, so they do not detect a chainstate sync added only to the deleting path. Location: [`src/test/kernel/test_kernel.cpp:1376`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6/src/test/kernel/test_kernel.cpp#L1376>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`2e21a991cbc3`](<https://git.fish.foo/bitcoin/bitcoin/commit/2e21a991cbc30bb974188eb4ff96d1ca6b0906d6>) · [Review report](<https://review.fish.foo/report/36856-2e21a991cbc30bb974188eb4ff96d1ca6b0906d6-2642-1.html>) · [Previous reports](<https://review.fish.foo/traces/36856/>) [permalink](<https://review.fish.foo/report/36856-2e21a991cbc30bb974188eb4ff96d1ca6b0906d6-2642-1.html>)
DrahtBot commented 2026-11-13 16:33:49 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/43452418783/job/131977732637
LLM reason (✨ experimental): CI failed because IWYU added missing includes in src/kernel/bitcoinkernel.cpp, leaving an uncommitted diff that the lint check rejects.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/43452418783/job/131977732637</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU added missing includes in `src/kernel/bitcoinkernel.cpp`, leaving an uncommitted diff that the lint check rejects.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
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!36856
No description provided.