WIP: kernel: add prune locks and target pruning

bitcoin/bitcoin: PR #36857

2 minor findings · 1 suggestion

Findings

Link

[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).

Details and suggested fix

Both pruning exports call PruneBlockFiles without an exception handler. Once an eligible file is selected, SetHavePruned() can throw dbwrapper_error through WriteFlag(). WriteBlockStorage() can also throw through the fs::space() query or WriteBlockIndexDB(). Its Expected result handles only LowDiskSpace and FlushFailed. Unlike Chainstate::FlushStateToDisk, this path does not convert those exceptions into a FatalError notification and an error result.

Catch const std::exception& around the storage operations, report FatalError through the chainstate manager's notifications, and return -1. Preserve the existing deletion ordering: WriteBlockIndexDB() clears dirty entries before writing, and WriteBlockStorage() takes the pending-unlink set before that call, so the handler must not blindly requeue those files after a failed index write. Add a fault-injection assertion through the exported API for that failure path.


Link

[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).

Details and suggested fix

The setter accepts both heights unchanged, but GetLastPrunableHeight() computes height_first - PRUNE_LOCK_BUFFER - 1 and raises the ceiling to at least 1. On a non-snapshot chain at height 289 or above, flushed past height 1, this produces the allowed range {0, 1} and passes the kernel's max_height < 1 guard. With fast-prune, a valid height-1 block of at least 64 KiB, using a large coinbase output, occupies its own file and leaves genesis alone in file 0. The following small block moves to another file. Both pruning selectors can then delete the closed low-height files while the lock remains registered.

For locks below PRUNE_LOCK_BUFFER + 2, represent the result as an empty pruning range rather than clamping upward, and skip file selection. Adjust the node manual caller as well, since its helper asserts a positive height. Add kernel cases holding locks at 0 and 1 across both height and target pruning with this early-file layout.


Optional improvements

Link

[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).

Details and suggested fix

The maximum-target calls exercise only the initial no-op gate. For 16_MiB, nCurrentUsage + nBuffer < limits.target cannot become true, so removing just the in-loop stopping check would leave these assertions unchanged. The blocks that remain readable are protected by the flush boundary, prune lock, or recent-block window, not by reaching the target. Add a flushed fast-prune case with at least two eligible closed files and a target above PRUNE_TARGET_BUFFER, chosen from logical nSize + nUndoSize usage so removing only the oldest file suffices. Assert that its blocks become unreadable, a later otherwise eligible file stays readable, and repeating the request deletes nothing more. Keep the existing below-buffer, lock, and NEEDS_FLUSH cases.


Coverage details