6 suggestions
Optional improvements
[suggestion] · Pending-unlink cancellation lacks regression coverage
The new failure test would still pass without m_files_to_unlink.erase(nFile) because it never writes into a file while that file is queued for deletion.
Location: src/node/blockstorage.cpp:967 (head).
Details and suggested fix
PruneOneBlockFile() resets file metadata without advancing its cursor, and selection excludes only the highest of the NORMAL and ASSUMED cursors. A storage-level two-cursor fixture can therefore exercise reuse of a lower file retained by an early WriteBlockStorage() failure. With pruning enabled, write a NORMAL block into file 0, configure a snapshot height, and write an ASSUMED block into a higher file. Set the pruned flag, select file 0, and make the higher cursor's flush fail after passing that selection to WriteBlockStorage(). Restore the file, write another valid block at a NORMAL height, and assert that it reuses file 0. Retry storage writing with an empty selection and check that file 0 survives and ReadBlock() succeeds. Without the cancellation guard, that retry would unlink the newly written data.
[suggestion] · The pruned flag remains a caller-managed prerequisite
The sole caller correctly sets prunedblockfiles, but the extracted storage API leaves this invariant dependent on a separate caller step that BlockManager can own.
Location: src/node/blockstorage.cpp:831 (head).
Details and suggested fix
Both selectors mark entries pruned and dirty without recording the persistent flag. WriteBlockStorage() only checks the prerequisite with Assume, which does not enforce it in production. Another caller omitting SetHavePruned() could therefore persist pruned entries and delete their files while leaving the flag false. After restart, the unpruned-mode -reindex check would be skipped and IsBlockPruned() would report false for those blocks. Move the flag update into the selectors when they select a nonempty set, make SetHavePruned() private, and remove the validation-side call. This also covers dirty selections persisted by a separate index write. Newly selected files already force a write, so the flag would still be recorded before the writer's disk-space and flush failure paths.
[suggestion] · Negative prune limits disable the upper-height filter
No current caller producing negative limits was found, but {0, -1} disables the public selectors' upper-height filter in production builds.
Location: src/node/blockstorage.cpp:317 (head).
Details and suggested fix
The nonnegative-range check uses only Assume. With min_height = 0 and max_height = -1, the unsigned cast produces UINT_MAX. In prune mode, manual selection would then mark every nonempty file below MaxBlockfileNum() pruned, including files inside the retention window. Target selection would likewise lose its height restriction when usage triggers pruning. The validation helpers currently obtain valid limits from GetPruneRange(), so this is internal API hardening. Use if (!Assume(limits.min_height >= 0 && limits.max_height >= 0)) return; in both methods to preserve debug detection while making an invalid call select nothing in production.
[suggestion] · Forced recovery does not test deferred unlink scheduling
The forced retry in prune_flush_failure cannot distinguish the intended deferred unlink scheduling from a regression that makes pending deletions trigger another write.
Location: src/test/chainstate_write_tests.cpp:210 (head).
Details and suggested fix
After RestoreFile(), reset the validation state and call FlushStateToDisk(state, FlushStateMode::NONE) without a manual prune height. Check that it succeeds, the database still reports pruned_size, and both pending files still exist before keeping the existing FORCE_FLUSH assertions. This fixture's -prune=1 target prevents automatic selection from requiring a write, and NONE enables neither cache-pressure nor periodic writes. The intermediate assertions would therefore catch pending unlinks incorrectly driving should_write. A short comment beside fFlushForPrune could also record that retained deletions deliberately wait for an otherwise required write.
[suggestion] · Prune selection lacks a caller-side cs_main contract
Current callers hold cs_main, but the public selectors do not require it, so thread-safety analysis cannot flag unlocked selection after callers compute chainstate-dependent limits.
Location: src/node/blockstorage.h:370 (head).
Details and suggested fix
The range calculation now happens before entering these methods. Their internal LOCK(::cs_main) protects storage accesses but cannot keep those externally computed limits current between calculation and selection. Add EXCLUSIVE_LOCKS_REQUIRED(::cs_main) to both declarations and replace their internal acquisitions with AssertLockHeld(::cs_main). The validation helpers already require the lock, and FlushStateToDisk() holds it across both stages. This preserves current behavior while expressing the surrounding-lock obligation at the extracted API boundary.
[suggestion] · The uncoordinated block-index writer remains public
WriteBlockStorage() is now the only caller of WriteBlockIndexDB(), but the public helper still permits bypassing the block-file flush and pending-unlink sequence.
Location: src/node/blockstorage.cpp:849 (head).
Details and suggested fix
WriteBlockIndexDB() drains the dirty entries directly, including entries marked pruned. Calling it separately would persist those entries outside the sequence that flushes block data and pairs the index write with pending-file deletion. Move its declaration into the private section now that validation no longer calls it directly. This preserves every tracked caller and makes the coordinated writer the required entry point for this helper. m_block_tree_db remains public, so this narrows the interface rather than fully encapsulating the database.