node: batch block file pruning in a single pass over block index

bitcoin/bitcoin: PR #36621

3 suggestions

Optional improvements

Link

[suggestion] · The long-outage rationale overstates dirty-index savings

The skip avoids dirtying data-less entries only during the first pruning of file 0, not after an outage on a node that already pruned it.

Location: src/node/blockstorage.cpp:294 (head).

Details and suggested fix

Header-only and already-pruned entries have nFile == 0, and the base only dirtied entries whose nFile matched the selected file. Both production selectors skip records with nSize == 0; pruning clears those records, and new block files use increasing numbers. This limits the dirty-set benefit to the first pruning of file 0 per block-index lifetime, such as when enabling pruning on a full node or when the prune target was not reached before an outage. Narrow the PR description accordingly and attribute the recurring backlog benefit to batching. Skipping the m_blocks_unlinked cleanup remains safe because its values must have BLOCK_HAVE_DATA.


Link

[suggestion] · The batching refactor also changes data-less entry handling

Separating the data-less skip from commit 38d6447063345059a74ffb439dec5b0bf77f86cd would let reviewers validate batching independently of the invariant-dependent dirty-index behavior change.

Location: src/node/blockstorage.cpp:294 at 38d644706334 (commit) (commit).

Details and suggested fix

Commit 38d6447063345059a74ffb439dec5b0bf77f86cd introduces both the std::vector<bool> traversal and the data-less guard. For valid production selections, batching alone preserves the base's final updates: the ascending per-file callers already process file 0 before entries from other files are reset to nFile = 0. The guard separately changes dirtying and unlinked-map cleanup, relying on the BLOCK_HAVE_DATA invariant.

Put the bitfield, range validation, interface and caller updates, and gap test in one commit. Follow it with the data-less guard, its rationale, the header-only setup and associated dirty-set/re-prune assertions. Keep the range check with the bitfield: otherwise an invalid file number would index std::vector<bool> out of bounds instead of reaching the base's throwing .at(). This preserves the final tree while separating the two correctness arguments.


Link

[suggestion] · Shared fixture option handling is coupled to the pruning commit

Separating the shared fixture prerequisite from commit 38d6447063345059a74ffb439dec5b0bf77f86cd would allow independent validation; existing fixtures are expected to retain their defaults.

Location: src/test/util/setup_common.cpp:332 at 38d644706334 (commit) (commit).

Details and suggested fix

In commit 38d6447063345059a74ffb439dec5b0bf77f86cd, ChainTestingSetup starts applying block manager arguments for every derived fixture, while the new PruneManualSetup needs this to enable -prune=1. Move the <node/blockmanager_args.h> include, mutable blockman_opts and ApplyArgsManOptions call into a preparatory commit before the pruning change. The parser already exists and does not depend on the new pruning API. This lets reviewers validate the shared setup change against existing chain-based tests first, with the new manual-pruning test depending on it in the following commit and no change to the final tree.


Coverage details

Background

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

Documented decisions

Current data availability is distinct from historical transaction validation. Pruning preserves nTx but clears BLOCK_HAVE_DATA, so nTx > 0 cannot establish that a block still has data on disk.

This distinction explains why pruning membership must be gated by availability flags rather than transaction counts or validation status. The pinned base also defines BLOCK_HAVE_DATA and BLOCK_HAVE_UNDO as availability in blk/rev files and asserts that HAVE_UNDO implies HAVE_DATA.

Source 1 · Source 2

The unlinked-block map tracks locally available blocks that cannot yet be linked because ancestor transactions are missing, including blocks received before their ancestors and chains obstructed by pruned ancestors. Its entries must themselves have block data.

Historical PR #35168 explicitly corrected startup reconstruction that admitted already-pruned stale-fork blocks based only on nTx. Its fix gated insertion on BLOCK_HAVE_DATA. Thus excluding data-less blocks from this bookkeeping is an established invariant, not a new assumption introduced by batching. The pinned base reinforces it with AddUnlinkedBlock's HAVE_DATA precondition and CheckBlockIndex's assertion that a block without HAVE_DATA cannot be an unlinked-map value.

Source 1 · Source 2

Ralph's inferences

nFile is a conditional location field, not an independent assertion of file membership. In particular, nFile == 0 does not distinguish an actual file-0 block from a header-only or already-pruned entry.

In the pinned base, nFile and both offsets default to zero, position accessors require their respective availability flags, and disk serialization includes nFile only when HAVE_DATA or HAVE_UNDO is set. Pruning clears both flags and resets all three location fields to zero. Consequently, an entry with neither flag has no represented file membership to remove; skipping it avoids marking an unchanged index record dirty merely because its default/reset file number aliases file 0.

Under the established invariant, skipping a data-less entry also skips no necessary unlinked-map removal: that entry cannot already be present as a map value.

The pruning cleanup removes only pairs whose value equals the block being pruned; it does not remove all children keyed by that block. A header-only parent may therefore still be a key for available children without requiring cleanup of the parent itself. For an available block that is actually pruned, the existing routine removes its value entry because reconsidering that block's chain requires downloading it again, when candidate/unlinked bookkeeping can be rebuilt.

Source 1