Follow and record chain reorganisations, pausing on deep ones (B-85, ELC-09, ELC-22, ELC-23, ELC-24, AUD-38, DAT-69)

A reorganisation was neither recorded nor acted upon, and a competing header at the tip's
own height was ignored for as long as the tip stood, leaving the listener on an orphan.

- A header at the height we hold with a different hash is followed through the same
  fork-point search as any other reorg (ELC-09) instead of being ignored; it still has to
  bring valid work and a connected chain.
- ElectrumListener._record_reorg writes `chain_reorg_detected` (depth, fork height, old and
  new tip height and hash) before the window moves, so a failed write ends the session and
  the next server's announcement of the same fork is recorded then. Depth 1 is a
  transaction of its own; depth >= 2 also pauses the lottery with paused_reason 'reorg'
  and writes `lottery_paused` in the same transaction (begin_immediate first).
- Docs: ELC-09/22/23/24, AUD-03/11/38, DAT-30/69, OPS-30, RND-04, ADR-26/52 lose their
  B-85 "da implementare" lines.

Tests changed because the spec changed: test_apply_header_ignores_a_competing_header_at_
the_current_tip_height encoded the B-64 behaviour that ELC-09 now reverses; it is replaced
by test_a_competing_header_at_the_tip_height_is_a_reorg_to_follow and the depth tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-10-14 15:56:21 +02:00
1 parent ca50ef62f3
commit 50b68d2957
16 files changed
+254 -111

No files matched your search

+12 -13
View File
@@ -43,9 +43,10 @@ ogni tipo di evento. Le colonne della tabella `audit_log` sono descritte in
`app/rounds/scheduler.py:RoundScheduler._log_payout_failure`,
`app/tx/broadcast.py:RbfBumper._tick`, `app/main.py:lifespan`,
`app/rounds/scheduler.py:RoundScheduler._tick`,
`app/electrum/listener.py:ElectrumListener._apply_header`
*Stato:* da implementare (B-85, B-87, B-90, B-96), per i quattro eventi
nuovi · *Oggi:* gli eventi osservativi sono solo i primi quattro.
`app/electrum/listener.py:ElectrumListener._record_reorg`
*Stato:* da implementare (B-87, B-90, B-96), per tre degli eventi
nuovi · *Oggi:* gli eventi osservativi sono solo i primi quattro e
`chain_reorg_detected`.
- **[AUD-04]** Il `payload` è un dizionario serializzato in JSON nella colonna
`payload_json`. `created_at` è impostato dall'applicazione, non dal
@@ -210,14 +211,14 @@ flowchart LR
ogni pausa, anche se la lotteria era già in pausa. Stessa transazione di
`paused = true`.
*Codice:* `app/api/routes/admin.py:pause_lottery`, `app/main.py:lifespan`,
`app/electrum/listener.py:ElectrumListener._apply_header`,
`app/electrum/listener.py:ElectrumListener._record_reorg`,
`app/rounds/scheduler.py:RoundScheduler._trigger_payout`,
`app/tx/reconcile.py:reconcile_once` ·
*Test:* `tests/unit/test_pause_reason.py`
*Stato:* da implementare (B-82, B-85, B-90) · *Oggi:* scritto da
`POST /admin/pause` (`operator`) e dal riconciliatore (`resurrected_tx`);
`reorg`, `payout_blocked` e l'avvio con `START_PAUSED` non lo scrivono
ancora.
*Test:* `tests/unit/test_pause_reason.py`, `tests/unit/test_electrum_listener.py`
*Stato:* da implementare (B-82, B-90) · *Oggi:* scritto da
`POST /admin/pause` (`operator`), dal riconciliatore (`resurrected_tx`) e dal
listener (`reorg`); `payout_blocked` e l'avvio con `START_PAUSED` non lo
scrivono ancora.
- **[AUD-12]** `lottery_resumed` — `POST /admin/resume`. Payload
`{"reason"}`: il `paused_reason` che la ripresa ha tolto (`null` se la
@@ -632,10 +633,8 @@ flowchart LR
`lottery_paused` con `reason = "reorg"` ([AUD-11]). Una biforcazione più
profonda di 10 header non viene seguita e non scrive questo evento (la
sessione si chiude). Nessuna logica rilegge la riga.
*Codice:* `app/electrum/listener.py:ElectrumListener._apply_header` ·
*Test:* —
*Stato:* da implementare (B-85) · *Oggi:* nessuna riorganizzazione è
rilevata; l'evento non esiste.
*Codice:* `app/electrum/listener.py:ElectrumListener._record_reorg` ·
*Test:* `tests/unit/test_electrum_listener.py`
## Cosa non viene registrato