Corroborate an external spend before marking a UTXO gone (B-29)

detect_external_spends marked a UTXO spent_txid='external-spend'
irreversibly the moment it was missing from one listunspent reply, on
one server, with no way to undo it. A rotated-to server that's broken
or behind, or an empty reply, could zero a user's balance permanently.

Split into three functions in deposits/service.py: find_utxos_missing_
from (read-only candidate detection, and refuses to flag anything at
all when listunspent comes back entirely empty for a funded address -
that reads as a broken response, not a full sweep), mark_utxos_spent_
externally (persistence only, once a candidate is already confirmed),
and reinstate_reappeared_utxos (undoes the mark if the outpoint
reappears as unspent later).

ElectrumListener gains corroborate_utxo_spent, sharing the same
majority-quorum logic corroborate_header already uses for B-28: before
a candidate is marked, the other configured servers are asked whether
they also see it as spent. _refresh_user now reads candidates, then
corroborates each one with no DB session held open across those
network calls (same shape as B-18/B-25), then persists.

Suite grows from 165 to 176 tests. BUGS.md moves B-29 to Previously
fixed.
This commit is contained in:
davide committed 2026-07-27 10:25:07 +02:00
1 parent 0ce0562fd7
commit e8fdea0389
5 files changed
+453 -84

No files matched your search

+9 -25
View File
@@ -1,17 +1,17 @@
# Known bugs
A second full-codebase audit on 2026-07-27 found **25 further issues** (4 critical, 6 high,
7 medium, 8 low), listed below as B-29 … B-49. B-25 through B-28 are fixed (see "Previously
fixed" below) — no Critical-severity finding remains open; the other 21 are High/Medium/Low.
7 medium, 8 low), listed below as B-30 … B-49. B-25 through B-29 are fixed (see "Previously
fixed" below) — no Critical-severity finding remains open; the other 20 are High/Medium/Low.
The 139-test suite was green at the time of the audit, so none of these were caught by existing
coverage — every fix lands with a regression test (the four fixes so far brought the suite from
139 to 165).
coverage — every fix lands with a regression test (the five fixes so far brought the suite from
139 to 176).
The recurring pattern across the open findings is worth stating once: the code is rigorous
about the failure modes that have actually been hit, and silent about the ones that have not.
Outgoing transactions reconcile; deposits do not (B-29). The payout phase is now fully
recoverable; the "drawing" phase (waiting on a block) still has no equivalent
resume-after-restart or stall visibility (B-36).
The payout phase is now fully recoverable; the "drawing" phase (waiting on a block) still has
no equivalent resume-after-restart or stall visibility (B-36), and there's still no periodic
deposit-side reconciler independent of scripthash notifications (B-30).
For limitations that are accepted by design rather than bugs (single-shared-token admin auth,
single-process assumptions, no user-facing history, etc.), see "Known gaps / TODO" in
@@ -21,23 +21,6 @@ single-process assumptions, no user-facing history, etc.), see "Known gaps / TOD
## High
### B-29 — `detect_external_spends` is irreversible and trusts a single response
`deposits/service.py:87` marks `spent_txid = "external-spend"` for any UTXO missing from the
current `listunspent`. There is **no path to undo it**: `credit_confirmed_utxos` skips
`(txid, vout)` keys that already exist, regardless of their spent status (`:19-31`).
One incomplete `listunspent` — a rotated-to server that is broken or behind, an empty reply on
error, or a reorg — permanently and silently zeroes a user's balance, recoverable only by
editing the database. Crediting is idempotent and conservative; debiting is neither, and it
acts on a single reply from a single unauthenticated server.
**Proposed fix.** Treat a missing outpoint as *evidence*, not proof. Require the same UTXO to
be absent across N consecutive refreshes (or confirm the spend by looking up the outpoint's
spending tx) before marking it, and skip the whole pass when `listunspent` returns empty for
an address the DB believes is funded. Make the mark reversible: re-crediting should clear
`spent_txid` when the sentinel value is present and the outpoint reappears as unspent.
### B-30 — No deposit-side reconciler: one missed subscription means deposits are never credited
`electrum/listener.py:61-67` (`address_for_new_user`) fires `asyncio.create_task(...)` without
@@ -301,9 +284,10 @@ already does.
- **B-26** — a payout failure or a process restart could wedge a round in `paying_out` forever
- **B-27** — an RBF bump reset the reconciler's own abandon clock, so a repeatedly-bumped tx was never abandoned
- **B-28** — a hostile Electrum server (or a MITM) could single-handedly pick the round's winner
- **B-29** — a UTXO absent from one server's `listunspent` was marked spent immediately, irreversibly, on a single unauthenticated reply
See git history for the fix-by-fix breakdown (commits `f13f685`, `50a43ae`, `933760e`, and the
B-28 fix). Suite grew from 139 to 165 tests over the four.
B-28/B-29 fixes). Suite grew from 139 to 176 tests over the five.
A full-codebase audit on 2026-07-26 (commit `d4e0974`) found 24 bugs across every Python
module under `app/`, both static frontends, and the Docker/Caddy deployment — 5 critical,