Review report

bitcoin/bitcoin: PR #36738

4 suggestions

Optional improvements

Link

[suggestion] · Block-file reuse after a failed prune flush is untested

The new recovery test never rewrites a file awaiting deletion, so it would miss removal of the cancellation that keeps a later flush from unlinking that reused file.

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

Details and suggested fix

prune_flush_failure repairs the tip file and immediately forces a flush, leaving m_files_to_unlink.erase(nFile) untested with a pending deletion. Add a focused prune-mode BlockManager test: set m_snapshot_height = H, write below H into NORMAL file 0, and write at H into ASSUMED file 1. Selection excludes only the larger cursor's file, so FindFilesToPruneManual() can select file 0 while its cursor still points there. Call SetHavePruned(), obstruct opening file 1, and check that WriteBlockStorage(H, {0}) fails. Write another block below H to reuse file 0, restore file 1, then retry WriteBlockStorage(H, {}) and read the new block back. Without the erase, the retry would unlink the reused file. Check read-back rather than only nonzero metadata, because unlinking does not reset that metadata.


Link

[suggestion] · The pruned-flag invariant remains a caller prerequisite

The sole caller sets the flag correctly, but WriteBlockStorage() leaves future callers responsible for recording pruning before it can unlink files.

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

Details and suggested fix

WriteBlockStorage() receives the complete files_to_prune set but only uses Assume() to check m_have_pruned. That check does not enforce the prerequisite in production. A future internal caller that omits SetHavePruned() could unlink files without recording prunedblockfiles, which startup uses to reject unpruned operation on a pruned datadir. Move the conditional SetHavePruned() call to the start of WriteBlockStorage(), before queuing files or checking disk space, and remove validation's separate call and the header prerequisite. A nonempty selection already forces should_write, so this preserves today's ordering before the disk-space check, flush, index write and unlink, including recording the flag when the flush subsequently fails.


Link

[suggestion] · The prune-lock test misses locks below the reorg cutoff

The new test would still pass if MovePruneLocksBack() advanced an already-lower lock, leaving its preservation guard without a regression assertion.

Location: src/test/blockmanager_tests.cpp:322 (head).

Details and suggested fix

The fixture's only lock is at 200 when MovePruneLocksBack(100) runs, so unconditionally assigning the cutoff gives the same result. BaseIndex::SetBestBlockIndex() sets each lock from that index's progress, which can lag a higher reorg cutoff. Advancing such a lock could allow pruning blocks the index still needs. After the existing check, call blockman.MovePruneLocksBack(150) and assert that GetLastPrunableHeight(1000) still equals last_prunable. This distinguishes preserving the lock at 100 from moving it forward and does not assume a particular prune buffer.


Link

[suggestion] · The prune-retry test does not check deferred flushing

The forced retry in prune_flush_failure cannot distinguish intended deferral from a change that makes pending deletions force a storage write.

Location: src/test/chainstate_write_tests.cpp:210 (head).

Details and suggested fix

After the failed prune flush, m_files_to_unlink retains file 0, but the test repairs the tip file and immediately uses FORCE_FLUSH. That writes whether pending deletions force a retry or wait for an otherwise scheduled flush. Before RestoreFile(tip_blk_file), reset state and call FlushStateToDisk(state, FlushStateMode::NONE) without a manual height. Assert success and a valid state, then check that persisted file 0's nSize remains pruned_size and both pruned files still exist. The fixture's manual-pruning target selects no new files, and NONE enables none of the cache, periodic or forced-write triggers. Keeping the open failure installed makes an unintended write fail. Then retain the existing repair and forced-retry checks.


Coverage details