test: add feature_dbcrash_replay.py

bitcoin/bitcoin: PR #36837

1 suggestion

Optional improvements

Link

[suggestion] · A missing simulated crash delays and obscures the test failure

Source inspection shows that if the flush RPC finishes but node0 stays running, this functional test waits 60 seconds × timeout_factor and reports a process-wait timeout instead of the missing crash-log message.

Location: test/functional/feature_dbcrash_replay.py:49 (head).

Details and suggested fix

except Exception: pass allows either a normal RPC return or an unrelated RPC error to reach wait_until_stopped(). If node0 stays running, that wait raises inside assert_debug_log, skipping the log check after its yield. The test still fails, but more slowly and with a less specific diagnostic. Move node.wait_until_stopped() immediately after the with block. On the expected crash path, BatchWrite logs Simulating a crash. Goodbye. before _Exit(0), and debug.log is unbuffered, so the message is available before the RPC observes the dropped connection. The subsequent wait still verifies that the process exits successfully.


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

Non-atomic chainstate flushing was introduced to reduce the flush-time memory peak by using the blockchain as an existing replay journal instead of requiring one atomic LevelDB batch for the entire flush.

The PR changes tests, not this runtime design. In both pinned base and head, CCoinsViewDB::BatchWrite splits dirty coin changes into bounded batches. ReplayBlocks supplies the recovery mechanism that makes an interrupted multi-batch flush recoverable.

Source 1 · Source 2

The head-block record deliberately identifies an unfinished transition so startup can restore a consistent chainstate before normal operation. Recovery must handle reorganizations, not merely replay descendants of the previously flushed tip.

BatchWrite records the intended new tip and the old persisted tip before partial writes, then replaces that marker with the best-block record upon completion. ReplayBlocks finds their common ancestor, undoes the old branch, and applies the new branch through its tip. The new test constructs precisely the case where the last persisted tip belongs to a reorged-out branch.

Source 1 · Source 2 · Source 3 · Source 4

Crash injection, very small write batches, and reorganizations were intentional testing techniques from the original design's development, because successful ordinary flushing does not exercise recovery from partially applied coin updates.

The historical proposal added simulated crashes after partial flushes and contemplated randomized blocks and frequent reorganizations. The existing feature_dbcrash.py follows that broad stress-testing approach. The new test instead selects a fixed reorganization and forces crashes both during the initial flush and while recovery flushes its result.

Source 1 · Source 2 · Source 3 · Source 4 · Source 5

Batch size embodies a resource tradeoff: smaller batches limit temporary memory growth, while larger batches can reduce write overhead and improve disk throughput, especially on slower storage.

The later batch-size proposal explicitly discusses this balance. In the new test, -dbbatchsize=1 is a fault-injection control that makes partial writes frequent, rather than a representative production tuning choice; -dbcrashratio=1 then forces termination after a partial batch.

Source 1 · Source 2

Ralph's inferences

Replay repairs an unknown subset of applied UTXO changes, not necessarily a database corresponding to any intermediate block height. Its permissive handling of already-applied effects is therefore a recovery requirement, not a relaxation of transaction validity rules.

In the pinned implementation, rollback tolerates DISCONNECT_UNCLEAN but not DISCONNECT_FAILED, while rollforward spends inputs and permits output overwrites. These operations reconstruct the intended state even when some coin writes or deletions already occurred. BatchWrite also preserves the original transition endpoints when recovery itself is interrupted, explaining the test's second forced crash. The relevant failure model is local interrupted persistence, not authority for peers to supply unchecked replacement state.

Source 1 · Source 2 · Source 3 · Source 4

Correct recovery requires agreement on spendable state as well as on the chain-tip identifier; reporting the intended tip alone does not establish that the old branch was fully undone and the new branch fully applied.

The new test checks both the best-block hash and the serialized UTXO-set hash against an uninterrupted reference node. Its old branch spends coins that the reference branch leaves untouched, making restoration of those inputs part of the expected result. This separates recovery of branch-specific coin effects from recovery of the tip metadata.

Source 1 · Source 2 · Source 3

Unresolved disagreements

The historical record leaves recovery memory guarantees as an open qualification: a follow-up requested bounded UTXO-cache usage and suggested incremental recovery writes, while Pieter Wuille cautioned that startup memory spikes cannot generally be eliminated because LevelDB may convert a large pre-shutdown log into a table.

ReplayBlocks reconstructs changes in a local coin cache and flushes at the end. The new test exercises repeated interruption of that final persistence step, but its small workload does not address the separate resource-bound question. This historical concern is not a finding about the PR.

Source 1 · Source 2 · Source 3