Files
plm-lottery/docs/adr/ADR-51-dubbio-liberano-mai-fondi.md
T
davideandClaude Sonnet 5.5 2b6ddb019b Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
place_bet and request_withdrawal released everything on any exception from the
broadcast, including the 15s timeout and a connection dropped after the write. The
tx may have propagated: a bet's sats landed in the pool with no participant, a
withdrawal left while its row said failed and its coins were spendable again.

- client: ElectrumRpcError (subclass of ElectrumError) is raised only for a reply
  carrying a JSON-RPC `error`; timeouts, drops and write errors stay plain
  ElectrumError.
- tx/outcome.py:broadcast_with_verdict sorts a broadcast into accepted / refused /
  uncertain. A refusal is verified first: ElectrumListener.transaction_known asks
  blockchain.transaction.get of the active server and every other one (10s each) —
  any server returning the tx means accepted; the active server plus
  MIN_QUORUM_RESPONSES others all denying it means refused (ALLOW_NO_QUORUM keeps
  the reduced rule); too few answers is uncertain. "already known" wording is only a
  shortcut that skips the check. With no network to ask, a refusal is uncertain.
- place_bet / request_withdrawal / _trigger_payout use it: only a verified refusal
  releases; uncertain leaves the rows building for the reconciler, publishes SSE and
  POST /bets and POST /withdrawals answer 202 with status building; "already known"
  promotes a payout (payout_sent) like any accepted broadcast.
- An accepted transaction is fanned out to every other server
  (ElectrumListener.fan_out_transaction, best effort, off the caller's path). The RBF
  replacement in bump_fee is left for B-77, which rewrites that function.

Tests changed because the spec changed: the existing "refused broadcast" tests raised
a bare RuntimeError (now an uncertain outcome) and are made explicit refusals
(rpc_error) with a network that denies the tx; direct route calls pass a Response;
FakePayoutListener gains the network surface.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 14:22:20 +02:00

2.8 KiB

ADR-51 — Nel dubbio non si liberano mai i fondi

  • Stato: Accettata — implementazione da completare (B-77, B-78)
  • Area: Denaro in movimento
  • Fonte di verità per: la scelta di progetto descritta qui sotto; i requisiti (PREFISSO-NN) che la attuano stanno nei documenti di dominio, indicati nelle Conseguenze.

Decisione

Nel dubbio non si liberano mai i fondi (decisione del 2026-09-28). Una riga che sposta denaro (scommessa, prelievo, pagamento) si annulla, liberandone le monete, solo con una prova: un rifiuto esplicito della rete all'invio, oppure i suoi ingressi spesi da un'altra transazione confermata (corroborata dal quorum), oppure nessun server del quorum che conosca alcun txid della riga e una ritrasmissione dell'ultima versione a tutti i server rifiutata per ingressi mancanti o già spesi. Mai per un timeout, per l'età della riga o per il silenzio di un solo server. Si seguono tutti i txid che una riga ha avuto (ogni versione RBF): la conferma di uno qualsiasi conferma la riga. Le righe failed restano sorvegliate per 14 giorni; una conferma tardiva scrive pending_tx_resurrected e mette in pausa la lotteria (ADR-28). Regole in transazioni-e-riconciliazione.md (B-76, B-77, B-78).

  • Motivazione: liberare le monete di una transazione che poi conferma è l'errore peggiore possibile: la scommessa finisce nel montepremi senza partecipante, il prelievo parte mentre la riga dice failed, il pagamento viene ricostruito e il vincitore pagato due volte. Tutti e tre accadevano con un invio andato in timeout (B-76), con la conferma di una versione RBF precedente (B-77) o con un riavvio su un server che non aveva ancora visto la transazione (B-78).
  • Alternative scartate: abbandono a tempo (la regola precedente: 120s per building, 6 ore per pending), che trasforma un ritardo di rete in una perdita; abbandono sul parere del solo server attivo.
  • Conseguenze: una transazione ferma ma viva non si abbandona mai: la si accelera (ADR-18); un round può restare in closing o paying_out finché la rete non decide ([LIM-02], [LIM-03]). Il denaro in dubbio resta congelato e visibile, mai duplicato.

Codice: app/tx/reconcile.py:reconcile_once · app/tx/reconcile.py:_is_due · app/bets/service.py:place_bet · app/withdrawals/service.py:request_withdrawal · app/tx/confirmation.py:poll_once · app/tx/broadcast.py:bump_fee · Test: tests/unit/test_broadcast_outcome.py Stato: da implementare (B-77, B-78) · Oggi: l'abbandono avviene quando la cronologia del solo server attivo non contiene il txid corrente, dopo 120s per building e 6 ore per pending; si segue solo l'ultimo txid; le righe failed non sono più controllate.