utxo split/1 durability

willcl-ark/bitcoin: PR #39

3 minor findings · 4 suggestions

Findings

Link

[minor] · Latched flush failure can invalidate a valid snapshot for kernel callers

MaybeValidateSnapshot() can invalidate a valid snapshot after a latched flush failure for non-interrupting kernel callers; no current caller combining these conditions was found, and bitcoind interrupts hashing instead.

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

Details and suggested fix

An older-file flush can fail inside WriteBlockUndo() without failing the undo write. If background ConnectTip() then reaches the snapshot base without requiring an IF_NEEDED write-out, it still calls MaybeValidateSnapshot(). At base, a successful cursor-file flush could write those coins; now m_sync_failed prevents every write-out. ForceFlushStateToDisk() only logs that failure, so validation hashes the earlier CoinsDB() contents against the snapshot-base hash. The mismatch marks the snapshot invalid, closes its coins views, attempts to rename its directory with an _INVALID suffix, and emits fatalError, preventing reuse of the valid snapshot on restart. The C adapter only forwards flush_error, and btck_chainstate_manager_create() can load an existing snapshot chainstate. Propagate the forced-flush result to MaybeValidateSnapshot() and return without invalidating the snapshot when that flush fails.


Link

[minor] · Kernel callers lack the new stop/restart contract after flush_error

Kernel callers receive no public stop/restart requirement after flush_error, although continuing after the new permanent write-out failure can leave CoinsTip() ahead of the chain tip and make a retry abort.

Location: src/kernel/notifications_interface.h:56 (head).

Details and suggested fix

notifications_interface.h now requires applications to stop processing blocks, but bitcoinkernel.h describes flush_error only as a disk-flush notification. The adapter only forwards it, FlushErrorHandler() defaults to a no-op, and bitcoin-chainstate prints it then continues its ProcessBlock() loop. Continued processing can fill the coins cache: ConnectTip() merges the next block into CoinsTip() before the permanently failing IF_NEEDED write-out and returns before m_chain.SetTip(). Retrying that block reaches the best-block assertion. This failure boundary existed before, but an earlier deferred flush failure now prevents recovery even after the I/O problem clears. Later write-outs also retry cursor syncs before checking the latch. Expose the stop/restart requirement in the public callback and wrapper documentation, and make the example record the notification and leave its processing loop before submitting another block.


Link

[minor] · Cursor retries contradict the permanent Unsynced invariant

Successful cursor retries can report Synced after a failed sync, contrary to the documented permanent Unsynced state; m_sync_failed still blocks persistence.

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

Details and suggested fix

FlushStateToDisk() calls FlushChainstateBlockFile() before checking the latch through SyncWrittenFiles(). A successful retry erases the failed file from its unsynced set, and DataDurability() and UndoDurability() consult only set membership. The retry fixture already follows this path but checks only write-out refusal and unchanged persisted metadata. Thus the per-entry Record check does not express the permanent-failure invariant promised in durability_machine.h and blockstorage.h. Short-circuit FlushBlockFile() and FlushUndoFile() after a latched failure so retries cannot clear the failed file's unsynced state, and extend the restored-file test to assert that state.


Optional improvements

Link

[suggestion] · Durability tests omit the other cursor's dirty block file

The new durability tests never leave a lower-numbered block file dirty under the other cursor, so they do not distinguish syncing every written block file from syncing only MaxBlockfileNum().

Location: src/test/durability_machine_tests.cpp:49 (head).

Details and suggested fix

The fixtures use a single normal chain, and undo_in_a_left_file_is_synced flushes the old block file at rollover before leaving its undo file dirty. Add a storage-only two-cursor case by setting m_snapshot_height between two subsequently written heights and keeping both files dirty. Flush the higher-numbered cursor, then assert that the lower file's block is Unsynced before SyncWrittenFiles() and Synced afterward. This covers the other-chainstate block-file path without constructing a snapshot chainstate.


Link

[suggestion] · Failure tests omit the ignored rollover flush result

The failure tests never exercise FindNextBlockPos() continuing after an ignored rollover flush error, so they do not pin that the failure remains latched after the cursor moves.

Location: src/test/durability_machine_tests.cpp:108 (head).

Details and suggested fix

Use FastPruneChain100Setup to persist a baseline, append an unflushed block, and replace its current block file with a directory. Accept a ChildBlock() large enough to force rollover and assert that allocation succeeds in a new file despite the old-file flush failure. After RemoveOpenFailure(), require FORCE_FLUSH to fail and check that GetLastFlushedBlock() and the persisted ReadBlockFileInfo() still match the baseline. Existing faults occur only at write-out or direct SyncWrittenFiles() calls; this case would cover the ignored-return boundary and latch survival across cursor replacement.


Link

[suggestion] · Durability fix and table instrumentation share one commit

231b2c3 couples the durability behavior fix to assertion instrumentation and table-based test oracles, requiring those dependencies to be disentangled before the fix can be reviewed or backported independently.

Location: src/validation.cpp:2882 at 231b2c335ecc (commit) (commit).

Details and suggested fix

231b2c335ecc70607ca8bb4f6794cd3c73a74474 combines independently useful unsynced-file tracking, the failure latch and the SyncWrittenFiles() barrier with DURABILITY_TABLE and FLUSH_TABLE instrumentation. Split out the behavior fix with outcome-based tests first, then add the tables, accessors and Assume checks separately. The fix could precede 1ca5beff3eb61557ad8fb72a6f2595476eb66dd6, which introduces util/machine.h and the block-storage machine. Before reordering, replace the tests' DurabilityState queries with return-value and persisted-state checks, and move the BlockStoreState assertions out of the standalone fix. This preserves an independently testable durability change without requiring the table infrastructure.


Link

[suggestion] · Candidate-set extractions are mixed with transition checks

1ca5bef mixes two behavior-preserving extractions with the new machines; a preparatory extraction commit would make the unchanged candidate-set and validity logic easier to verify.

Location: src/validation.cpp:3253 at 1ca5beff3eb6 (commit) (commit).

Details and suggested fix

In 1ca5beff3eb61557ad8fb72a6f2595476eb66dd6, DropUnusableCandidateChain() moves the m_best_invalid, m_blocks_unlinked and candidate-removal logic with parameter renames, while MarkBlockScriptsValid() moves the validity raise. Neither body uses BlockStoreStateOf() or the transition table. Put their call replacements, definitions and validation.h declarations together in a preceding extraction-only commit, leaving this commit to introduce util/machine.h, the storage machine and its checks. Reviewers could then verify the candidate-set move separately with git diff --color-moved.


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

Block-file order and undo-write order are fundamentally different. Blocks are stored in download order, whereas undo data is produced in validation order and placed in the file corresponding to its block. Consequently, validation can modify an older undo file after the block cursor has moved on.

The 2022 missing-undo discussion directly explains the historical limitation of cursor-only syncing. Ryan Ofsky distinguished download order from validation order; the reporter described BLOCK_HAVE_UNDO becoming persistent while the older undo file remained only in dirty OS pages. This is the historical rationale most directly relevant to syncing every written undo file before recording index flags.

Source 1 · Source 2 · Source 3

Ordinary buffered-write completion was not considered sufficient protection against power loss. The missing-undo report explicitly distinguished fclose/fflush handing bytes to the OS from making them durable, and described eventual background writeback as sufficient only on the happy path.

The affected behavior protects the meaning of persisted HAVE_DATA/HAVE_UNDO flags, not merely whether bytes can presently be read. The relevant failure model includes power loss or a disruptive event before dirty pages reach storage, rather than only a process exit.

Source 1 · Source 2