txdb: invalidate cursor if first coin key can't be decoded

bitcoin/bitcoin: PR #36845

1 minor finding

Findings

Link

[minor] · Malformed first coin keys now masquerade as an empty UTXO set

Source inspection shows that a malformed first coin key from local corruption or foreign writes can make cursor-based RPCs succeed with empty results; no current Core writer producing that key was found, and production occurrence is unconfirmed.

Location: src/txdb.cpp:263 (head).

Details and suggested fix

The one-byte 'C' key sorts before every complete CoinEntry. GetKey(entry) reads the prefix and fails on the missing hash, but this branch clears the same flag used for clean exhaustion. Consequently, ComputeUTXOStats and FindScriptPubKey skip their read-error paths: non-indexed gettxoutsetinfo returns zero outputs, and scantxoutset returns success: true without visiting valid coins after the bad record. Current-tip dumptxoutset also writes a zero-coin snapshot because its computed and written counts both equal zero. If the malformed record's value is undecodable too, these paths previously failed instead. Later-key truncation in Next() is pre-existing. Index-backed muhash/none statistics bypass this cursor, and assumeutxo still rejects a mismatching commitment.

Keep malformed entries from exposing a bogus outpoint, but distinguish decode failure from normal exhaustion with a terminal error status that scan, statistics, and snapshot callers check before reporting success. This preserves the new !Valid() contract. Add a caller-level regression using a valid chainstate fixture, an undecodable value under 'C', and a valid coin after it, asserting failure rather than successful empty output.


Coverage details