rpc: Manual prune lock management (Take 2)

bitcoin/bitcoin: PR #34534

2 minor findings · 5 suggestions

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.

Findings

Link

[minor] · A tip disconnect lowers an unreached manual prune boundary

Future-height locks can be lowered by a reorg, causing excess retention as the recent-block window advances; no current caller setting such heights was identified.

Location: src/rpc/blockchain.cpp:1042 (head).

Details and suggested fix

setprunelock and -prunelock=id:H store the requested height without bounding it to the tip. With the tip at 1500, setprunelock app add 100000 followed by a one-block reorg or tip invalidateblock leaves user:app at 1499. The existing DisconnectTip loop rewinds every lock above the disconnected height minus one, including locks whose protected range has never been reached. Reconnection does not restore the request. With the current buffer, this leaves a lock-derived pruning ceiling of 1488. MIN_BLOCKS_TO_KEEP initially imposes a tighter ceiling, but subsequent chain growth exposes unnecessary retention below the requested boundary and can keep usage above the -prune target. Updating or removing the lock, or restarting, provides recovery.

In DisconnectTip, leave user locks with height_first > pindexDelete->nHeight unchanged while preserving the existing rewind for active locks. Add a disconnect/reconnect regression test that checks listprunelocks still reports the unreached requested height.


Link

[minor] · -prunelock is installed after IPC block submission becomes available

With -ipcbind and automatic pruning over target, an early valid IPC block can trigger pruning before -prunelock is installed; no production client submitting in this window was identified.

Location: src/init.cpp:2078 (head).

Details and suggested fix

The IPC listener is already bound when InitAndLoadChainstate calls setChainstateLoaded(true), releasing MakeMining waiters. Startup locks are registered later in Step 10, after index initialization and wallet loading, which can include a rescan. During this interval, a trusted IPC mining client can submit a valid-PoW block extending the tip through MinerImpl::submitBlock. If writing it allocates a chunk, m_check_for_pruning becomes true and AcceptBlock calls FlushStateToDisk(NONE). With eligible old files and usage over the automatic target, pruning uses only the locks registered so far. Installing the configured lock afterward cannot restore deleted data, which must be fetched again, potentially through reindexing. The early IPC window predates this option, but the new startup protection does not cover it.

Move registration into InitAndLoadChainstate before setChainstateLoaded(true), so the replacement manager on its reindex-retry path receives the locks too.


Optional improvements

Link

[suggestion] · listprunelocks IDs do not round-trip through setprunelock

User locks are listed as user:app, but setprunelock rejects that ID, requiring management clients to strip a documented internal prefix.

Location: src/rpc/blockchain.cpp:982 (head).

Details and suggested fix

The list handler writes the raw map key into id. IsValidPruneLockId rejects :, and the setter prepends user: to accepted input, so the listed identifier cannot be used directly to update or remove a user lock. Keep the internal namespace separation, but expose an additional unprefixed user_id field for user locks and document that it is accepted by setprunelock. Add a test that removes a lock using that returned field. This avoids requiring clients to interpret storage keys without making internal locks writable.


Link

[suggestion] · setprunelock gives no feedback about already-pruned data

The successful {} response confirms only retention of remaining data, not availability of the whole requested range, so missing blocks may surface only during a later wallet load or rescan.

Location: src/rpc/blockchain.cpp:1055 (head).

Details and suggested fix

add checks only that the height is nonnegative and calls UpdatePruneLock, which assigns the map entry. An already-pruned range therefore receives the same response as an available one, even though a wallet needing those missing blocks still fails its later load or rescan. Keep accepting lower locks because they protect whatever data remains. Under cs_main, use the adjacent GetPruneHeight helper to return the current prune boundary or a warning when the requested height overlaps pruned data. This provides availability feedback without rejecting useful retention constraints.


Link

[suggestion] · setprunelock help obscures the storage effect of its default height

The generated help shows default=0, but the description calls height required for add and does not explain its retain-all effect or warn that the -prune target may be exceeded.

Location: src/rpc/blockchain.cpp:1007 (head).

Details and suggested fix

An omitted height stores 0, and the new functional test intentionally exercises that behavior. Unlike the -prunelock help, the RPC text does not explain that this requests retention of all remaining block data or that a lock can prevent meeting the pruning target. Replace “only required for add” with wording that describes the argument as used for add, defaulting to 0 to preserve all remaining data, and carry over the target-size warning.


Link

[suggestion] · The -prunelock startup test never puts automatic pruning under pressure

The startup test uses -prune=1 and checks log ordering, so it never verifies that a startup lock preserves data when automatic pruning would otherwise delete it.

Location: test/functional/rpc_prunelock.py:219 (head).

Details and suggested fix

PruneAndFlush requests a pruning check, but -prune=1 supplies PRUNE_TARGET_MANUAL (UINT64_MAX), so this fixture cannot reach the deletion condition in FindFilesToPrune. The earlier pruneblockchain assertions exercise manual pruning. The current startup installation precedes PruneAndFlush; the gap is behavioral coverage of automatic startup retention. Add a restart with an automatic target and eligible old files over target, assert a protected block stays readable, then restart without the lock and verify the same data is pruned. Reuse the large-block fixture in feature_pruning.py to avoid adding the minimum 550 MiB workload to this lightweight test.


Link

[suggestion] · listprunelocks tests never mix index and user locks

The functional coverage never enables an index, leaving both the documented internal-lock listing and same-name namespace isolation unchecked.

Location: test/functional/rpc_prunelock.py:28 (head).

Details and suggested fix

Every lock exercised by rpc_prunelock.py has the user: prefix, although listprunelocks promises to include index locks registered under unprefixed names by BaseIndex::SetBestBlockIndex. On a fixture that still has the required historical blocks, enable a prune-compatible index such as -coinstatsindex and wait for it to sync. Add a user lock with the same name, check that both entries are listed, remove the user lock, and verify that the index lock remains and a second removal returns not-found. This covers mixed-namespace listing and removal rather than only user-lock bookkeeping.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Moving a lock backward was deliberately allowed, but was not treated as recovery of already-pruned data. Historical discussion rejected an exclusively forward-moving boundary as too restrictive; consumers were expected to handle missing-block errors safely when syncing or rewinding.

This bounds the retention guarantee for manually selected historical heights: installing or lowering a lock protects available data against subsequent pruning, but the inspected rationale does not promise to reconstruct data already deleted or make arbitrarily deep reorgs transparent.

Source 1 · Source 2 · Source 3

External and offline consumers were explicit motivations for user-managed locks, and operator override was intentional. #19463 proposed protecting unloaded Core wallets, external wallets, and temporarily disabled indexes, while allowing users to delete application locks so abandoned applications could not leave permanent retention obligations.

Manual add/update/remove management is a lifecycle mechanism as well as a retention mechanism. Its historical guarantee is conditional on the operator maintaining the lock; it is not an application entitlement that overrides the node owner's decision to release storage.

Source 1 · Source 2