B4: chain tables, HD index assignment and signing key reserved to the worker (DAT-81..84, ARC-78)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-11-16 15:53:50 +01:00
1 parent 0092596b07
commit f1d09075a1
18 files changed
+741 -8

No files matched your search

+9
View File
@@ -981,6 +981,15 @@ non è rimosso le sezioni precedenti descrivono lo stato storico. Scelte: [ADR-5
- **[ARC-72]** **Nessuna coordinazione in memoria fra web e worker.** Esclusione reciproca e unicità si
ottengono con vincoli e transazioni del database, mai con blocchi o code in memoria.
*Stato:* da implementare (A2 per i vincoli, B6 per la prenotazione degli UTXO).
- **[ARC-78]** **La chiave di firma è del solo worker.** Il processo web conosce soltanto la chiave **pubblica** dell'account
(`PLM_ACCOUNT_XPUB`): basta a derivare gli indirizzi di deposito e dei round ([DAT-81]). La chiave privata (la master xprv
cifrata e la chiave che la cifra, due file diversi) si carica solo se `PROCESS_ROLE=worker` e i file sono montati; in
qualunque altro processo `signing_account_key()` fallisce con `SigningKeyUnavailable`, anche se i file ci fossero.
`manage.py init_keys` crea le chiavi una tantum, scrive i file con modo `0600` e stampa la sola chiave pubblica.
*Stato:* implementato (B4); il montaggio dei file nel solo servizio `worker` arriva con B6, quando il worker firma ·
*Codice:* `apps/lottery/keys.py`, `apps/lottery/management/commands/init_keys.py` ·
*Test:* `tests/django/test_chain_models.py::test_the_web_process_cannot_load_the_signing_key`, `test_the_worker_can_and_the_files_are_private`
- **[ARC-73]** **Liveness.** `GET /health/live` risponde `200 {"status": "ok"}` se il processo risponde e il
database si apre, `503` altrimenti; il compose lo usa come healthcheck del servizio `web`.
*Stato:* implementato (A1) ·
+2 -2
View File
@@ -4,7 +4,7 @@
## Project status (old stack)
All 10 stages of the original build order are code-complete and unit-tested — 1187 tests, the old stack's under `tests/unit/` and the Django ones under `tests/django/` (`tests/integration/` needs the local regtest node and skips itself without it). Beyond them: Docker + Caddy deployment, admin dashboard (`/admin`), static test UI (`/`), pending-inclusive balance display, an SSE push channel layered over the original polling, self-service password change + admin password reset, and the reconciliation/corroboration machinery below.
All 10 stages of the original build order are code-complete and unit-tested — 1211 tests, the old stack's under `tests/unit/` and the Django ones under `tests/django/` (`tests/integration/` needs the local regtest node and skips itself without it). Beyond them: Docker + Caddy deployment, admin dashboard (`/admin`), static test UI (`/`), pending-inclusive balance display, an SSE push channel layered over the original polling, self-service password change + admin password reset, and the reconciliation/corroboration machinery below.
Verified on mainnet with real money: registration + address derivation, deposit crediting (at the time, 1-conf; deposits now need `deposit_confirmations`), a real 10 PLM bet (broadcast → confirmed → change credited back), and one full round cycle (close → draw on a real block hash → 70/30 payout with sat math checked against the broadcast tx → confirmation → close → next round auto-opened). **Withdrawal and the RBF bump path have never been exercised against a live broadcast** — unit-tested only.
@@ -30,7 +30,7 @@ PYTHONPATH=. python scripts/decrypt_master_key.py # ops recovery: decrypt+pr
PYTHONPATH=. python scripts/encrypt_master_key.py # ops bootstrap: import an externally-generated xprv (--overwrite to replace)
PYTHONPATH=. python scripts/electrum_smoke_test.py # manual check: connect, handshake, subscribe to headers, print the tip
python -m pytest # all 1187 tests
python -m pytest # all 1211 tests
python -m pytest tests/unit/test_hd.py # one file
python -m pytest tests/unit/test_hd.py::test_derivation_is_deterministic # one test
```
+28 -1
View File
@@ -1493,7 +1493,7 @@ scrive la stringa vuota).
Lo schema del sito Django (`apps/lottery/models.py`, `apps/accounts/models.py`) sostituisce quello
SQLAlchemy descritto sopra (roadmap A2–D1); fino ad allora il resto del documento è lo stato storico.
Le regole che devono valere comunque sono **vincoli del database**, non convenzioni ([ARC-72]).
Le tabelle di catena (UTXO, versioni delle transazioni, depositi, indici HD) arrivano con la Fase B (B4).
Le tabelle di catena (UTXO, versioni delle transazioni, depositi, indici HD) sono la Fase B (B4: [DAT-81]…[DAT-84]).
- **[DAT-76]** **Il denaro non si cancella.** Ogni chiave esterna delle tabelle `lottery_*` verso utenti, round,
partecipazioni e transazioni è `PROTECT`: un utente, un round o una transazione con righe collegate non
@@ -1531,3 +1531,30 @@ Le tabelle di catena (UTXO, versioni delle transazioni, depositi, indici HD) arr
accanto alle righe, quindi non può divergere da esse.
*Stato:* implementato (A3) · *Codice:* `apps/lottery/balances.py:balance_of` ·
*Test:* `tests/django/test_ledger.py::test_a_pending_deposit_is_incoming_not_spendable`, `test_a_whole_round_conserves_the_money`
- **[DAT-81]** **Indirizzo di deposito e indice HD.** Ogni giocatore ha **un solo** indirizzo di deposito
(`DepositAddress`): l'indice di derivazione (`0/<indice>` sotto l'account del profilo di rete, [ADR-58](adr/ADR-58-alfa-su-regtest-profilo-di-rete-configurabile.md))
è **unico** e l'indirizzo è unico; l'indice è il successivo al massimo assegnato e due assegnazioni
concorrenti non possono avere lo stesso, perché è il vincolo del database a deciderlo (chi perde riprova).
L'assegnazione è idempotente: lo stesso utente riceve sempre lo stesso indirizzo, e a parità di chiave e indice
l'indirizzo è sempre lo stesso. Si deriva dalla sola chiave **pubblica** dell'account ([ARC-78]).
*Stato:* implementato (B4) · *Codice:* `apps/lottery/models.py:DepositAddress`, `apps/lottery/chain.py:assign_deposit_address` ·
*Test:* `tests/django/test_chain_models.py::test_a_player_gets_one_address_and_always_the_same`, `test_derivation_is_identical_for_the_same_input`, `test_two_players_never_share_an_index`
- **[DAT-82]** **UTXO.** `Utxo` è un output che la piattaforma controlla: `(txid, vout)` unico, importo positivo, indirizzo,
a chi appartiene (un giocatore, per un deposito, oppure un round, per il suo montepremi; mai entrambi),
altezza di conferma (vuota finché non è confermato), `spent_txid` (vuoto finché non è speso) e la prenotazione
`reserved_by` verso la transazione della piattaforma che lo sta spendendo ([ARC-72], B6). Un `txid` ha sempre 64 caratteri.
*Stato:* implementato (B4); la prenotazione atomica è B6 · *Codice:* `apps/lottery/models.py:Utxo` ·
*Test:* `tests/django/test_chain_models.py::test_an_outpoint_is_stored_once`, `test_a_utxo_belongs_to_a_player_or_a_round_never_both`, `test_amounts_and_txids_are_checked_by_the_database`
- **[DAT-83]** **Tutte le versioni di una transazione** ([TX-19]). `TransactionVersion` registra ogni txid che una
`ChainTransaction` ha avuto (la prima, ogni sostituzione RBF, ogni candidata scritta prima del bump) con la
transazione grezza; il `txid` è **unico in tutta la tabella** (appartiene a una sola transazione) e le righe sono
**in sola aggiunta** (il modello e il database rifiutano modifiche e cancellazioni). `register_version` è
idempotente e aggiorna `current_txid` nella stessa transazione del database.
*Stato:* implementato (B4) · *Codice:* `apps/lottery/models.py:TransactionVersion`, `apps/lottery/chain.py:register_version` ·
*Test:* `tests/django/test_chain_models.py::test_every_version_of_a_transaction_is_kept`, `test_a_txid_belongs_to_one_transaction`, `test_versions_cannot_be_changed_or_deleted`
- **[DAT-84]** **Depositi.** `Deposit` lega un `Utxo` a un giocatore: stati `detected` (visto sulla catena),
`credited` (accreditato con un `Movement` di tipo `deposit`) e `orphaned` (sparito per una riorganizzazione).
Un output è un solo deposito e un `credited` ha sempre il movimento e la data di accredito.
*Stato:* tabella implementata (B4); il listener e l'accredito sono B5 · *Codice:* `apps/lottery/models.py:Deposit` ·
*Test:* `tests/django/test_chain_models.py::test_a_credited_deposit_has_its_movement_and_date`, `test_an_output_is_one_deposit`