WIP: kernel: add prune locks and target pruning #36857

Draft
pzafonte wants to merge 16 commits from kernel-prune-locks-and-target into master AGit
pzafonte commented 2026-11-13 15:37:53 +00:00 (Migrated from github.com)

This adds more of the pruning API discussed in #35627:

  • btck_block_manager_update_prune_lock and btck_block_manager_delete_prune_lock keep blocks at and above a height until the application deletes the lock. Like the node's prune locks, they are held in memory, so an application sets them again after a restart before it prunes.
  • btck_block_manager_prune_to_target deletes the oldest block files until usage, with room for the next allocation, is below a target. It keeps the same blocks as btck_block_manager_prune_up_to_height. Unlike -prune, the target has no minimum, and pruning doesn't wait for the chain to reach a minimum height.

Target pruning uses BeginManualPrune. BeginTargetPrune only proceeds after a new block file is allocated, and the periodic flush during block processing already consumes that, so it would never prune.

Names the buffer FindFilesToPrune keeps below a target. The kernel uses it to tell whether a flush would help.

Based on #36856. Draft until it is merged.

This adds more of the pruning API discussed in #35627: - `btck_block_manager_update_prune_lock` and `btck_block_manager_delete_prune_lock` keep blocks at and above a height until the application deletes the lock. Like the node's prune locks, they are held in memory, so an application sets them again after a restart before it prunes. - `btck_block_manager_prune_to_target` deletes the oldest block files until usage, with room for the next allocation, is below a target. It keeps the same blocks as `btck_block_manager_prune_up_to_height`. Unlike `-prune`, the target has no minimum, and pruning doesn't wait for the chain to reach a minimum height. Target pruning uses `BeginManualPrune`. `BeginTargetPrune` only proceeds after a new block file is allocated, and the periodic flush during block processing already consumes that, so it would never prune. Names the buffer `FindFilesToPrune` keeps below a target. The kernel uses it to tell whether a flush would help. Based on #36856. Draft until it is merged.
DrahtBot commented 2026-11-13 15:38:03 +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.

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 checking the expected message.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(ChainstateManagerOptions(context, ""), std::runtime_error) and BOOST_CHECK_THROW(ChainstateManagerOptions(context, {nullptr, 0}), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with predicates checking the expected messages.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(ChainMan(context, chainman_opts, blockman), std::runtime_error) (context mismatch and reused block manager) → Prefer BOOST_CHECK_EXCEPTION with predicates checking the respective expected messages.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(chain.GetByHeight(1)), std::runtime_error) (both occurrences) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected message.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(create_chainman(...), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected message.
  • src/test/kernel/test_kernel.cpp: BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(tip), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected message.

2026-11-13 15:38:38

<!--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/36857 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36857.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36857#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. <!--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 checking the expected message. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(ChainstateManagerOptions(context, ""), std::runtime_error)` and `BOOST_CHECK_THROW(ChainstateManagerOptions(context, {nullptr, 0}), std::runtime_error)` → Prefer `BOOST_CHECK_EXCEPTION` with predicates checking the expected messages. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(ChainMan(context, chainman_opts, blockman), std::runtime_error)` (context mismatch and reused block manager) → Prefer `BOOST_CHECK_EXCEPTION` with predicates checking the respective expected messages. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(chain.GetByHeight(1)), std::runtime_error)` (both occurrences) → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected message. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(create_chainman(...), std::runtime_error)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected message. - `src/test/kernel/test_kernel.cpp`: `BOOST_CHECK_THROW(blockman.ReadBlockSpentOutputs(tip), std::runtime_error)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected message. <sup>2026-11-13 15:38:38</sup>
First-time contributor

🟠 Minor · Kernel pruning lets storage exceptions escape the C API

Local database-write or filesystem-query failures can bypass the documented error returns from both kernel prune functions and may terminate an FFI caller, depending on its runtime.

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

🟠 Minor · Locks at height 0 or 1 still permit pruning those blocks

The new lock API can delete protected low-height blocks in test-only fast-prune layouts; the clamp is inherited, and production impact with normal block files is unconfirmed.

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

🔵 Suggestion · Target-pruning test misses stopping at the requested size

The kernel test's 16 MiB requests cannot reach their target because the buffer alone is 17 MiB, leaving target-based stopping untested.

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

Review coverage was incomplete.


Reviewed: 3a95f31c1ee2 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:464886e574fc96b3587e582688c29b6bd7a73bba755cfc1d2f031cc648c7ec97:a99dfe27a5d9c587aaf93be1a4842812699d6122d1d9babba7210869336cc02c --> <!-- ralph-base:9183edd25e60aa410bc3e4728989a1d75c309a09 --> <!-- ralph-head:3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a --> <!-- ralph-case:1e7a91a3f3163aacc061570e2330a8b02e6e3d393b528a7490e0feedf7177dce --> **🟠 Minor · Kernel pruning lets storage exceptions escape the C API** Local database-write or filesystem-query failures can bypass the documented error returns from both kernel prune functions and may terminate an FFI caller, depending on its runtime. Location: [`src/kernel/bitcoinkernel.cpp:1354`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a/src/kernel/bitcoinkernel.cpp#L1354>) (head). **🟠 Minor · Locks at height 0 or 1 still permit pruning those blocks** The new lock API can delete protected low-height blocks in test-only fast-prune layouts; the clamp is inherited, and production impact with normal block files is unconfirmed. Location: [`src/kernel/bitcoinkernel.cpp:1382`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a/src/kernel/bitcoinkernel.cpp#L1382>) (head). **🔵 Suggestion · Target-pruning test misses stopping at the requested size** The kernel test's 16 MiB requests cannot reach their target because the buffer alone is 17 MiB, leaving target-based stopping untested. Location: [`src/test/kernel/test_kernel.cpp:1496`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a/src/test/kernel/test_kernel.cpp#L1496>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`3a95f31c1ee2`](<https://git.fish.foo/bitcoin/bitcoin/commit/3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a>) · [Review report](<https://review.fish.foo/report/36857-3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a-2644-1.html>) · [Previous reports](<https://review.fish.foo/traces/36857/>) [permalink](<https://review.fish.foo/report/36857-3a95f31c1ee2085905aa1ae6dbbf39a9fd0dc71a-2644-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!36857
No description provided.