From 58d1a38ac0668bd61515917b8f95bbc2b5892e2e Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Mon, 16 Nov 2026 14:49:47 +0100 Subject: [PATCH] A8: public results, reproducible proof, guide and rules (WEB-12, WEB-13, RND-37) Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 4 +- README.md | 2 +- apps/lottery/proof.py | 63 +++++++++ apps/web/public_views.py | 48 +++++++ apps/web/static/web/site.css | 11 ++ apps/web/templates/web/base.html | 3 + apps/web/templates/web/guide.html | 13 ++ apps/web/templates/web/home.html | 1 + apps/web/templates/web/result_detail.html | 85 ++++++++++++ apps/web/templates/web/results.html | 34 +++++ apps/web/templates/web/rules.html | 13 ++ apps/web/urls.py | 7 +- docs/sito-e-app.md | 18 ++- roadmap.md | 2 +- tests/django/test_public.py | 160 ++++++++++++++++++++++ 15 files changed, 454 insertions(+), 10 deletions(-) create mode 100644 apps/lottery/proof.py create mode 100644 apps/web/public_views.py create mode 100644 apps/web/templates/web/guide.html create mode 100644 apps/web/templates/web/result_detail.html create mode 100644 apps/web/templates/web/results.html create mode 100644 apps/web/templates/web/rules.html create mode 100644 tests/django/test_public.py diff --git a/CLAUDE.md b/CLAUDE.md index e06245c..b55011f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -8,7 +8,7 @@ The user communicates in Italian in chat — reply to them in Italian. Everythin ## Project status -All 10 stages of the original build order are code-complete and unit-tested — 1109 tests, the old stack's under `tests/unit/` and the Django ones under `tests/django/` (`tests/integration/` is an empty package). 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 — 1120 tests, the old stack's under `tests/unit/` and the Django ones under `tests/django/` (`tests/integration/` is an empty package). 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. @@ -61,7 +61,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 1109 tests +python -m pytest # all 1120 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 ``` diff --git a/README.md b/README.md index a6d8230..3e8ad6e 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ python -m pytest # all tests python -m pytest tests/unit/test_hd.py # one file ``` -1109 unit tests cover HD derivation, PSBT building, the Electrum client, bets, +1120 unit tests cover HD derivation, PSBT building, the Electrum client, bets, deposits, withdrawals, the round/draw engine, RBF fee-bumping, admin config, the pending-inclusive balance calculation, and the SSE push channel. No automated integration tests against a live Electrum connection — mainnet diff --git a/apps/lottery/proof.py b/apps/lottery/proof.py new file mode 100644 index 0000000..d6c9414 --- /dev/null +++ b/apps/lottery/proof.py @@ -0,0 +1,63 @@ +"""The public proof of a round's draw (WEB-12, RND-37): everything anyone needs to recompute the winner, and the +recomputation itself. It carries no username and no account data — participants appear only under the pseudonymous +address of the public draw list (DAT-65) — and a field not yet available is `None`, never invented.""" + +from typing import Any + +from plm_engine.draw import draw_index, draw_seed, participants_hash, server_seed_commitment + +from .addresses import participant_address +from .format import plm_text +from .models import Participation, Round + + +def round_proof(round_: Round) -> dict[str, Any]: + drawn = round_.participants_hash is not None # the list is fixed once the round enters `drawing` (RND-14) + rows = [] + if drawn: + participants = (Participation.objects.filter(round=round_, draw_position__isnull=False) + .select_related("user").order_by("draw_position")) + rows = [{"position": p.draw_position, "txid": p.bet_txid, "vout": p.bet_vout, + "address": participant_address(p.user), "amount_sats": p.bet_amount_sats} for p in participants] + + data: dict[str, Any] = { + "round_id": round_.pk, "status": round_.status, + "server_seed_hash": round_.server_seed_hash, "pool_address": round_.pool_address or None, + "participants": rows, "participants_hash": round_.participants_hash, + "draw_height": round_.draw_height, "draw_confirmations": round_.draw_confirmations, + "server_seed": round_.server_seed, "draw_block_hash": round_.draw_block_hash, + "seed": None, "index": None, "winner_address": None, "winner_position": None, + "pool_amount_sats": round_.pool_amount_sats, "winner_amount_sats": round_.winner_amount_sats, + "fee_amount_sats": round_.fee_amount_sats, + "payout_txid": None, "payout_confirmed": False, + "checks": None, + } + if round_.pool_amount_sats is not None: + data["pool_text"] = plm_text(round_.pool_amount_sats) + if round_.winner_amount_sats is not None: + data["winner_amount_text"] = plm_text(round_.winner_amount_sats) + if round_.payout_transaction_id is not None: + payout = round_.payout_transaction + data["payout_txid"] = payout.current_txid + data["payout_confirmed"] = payout.status == "confirmed" # confirmed only when it is (WEB-12) + + if round_.server_seed and round_.draw_block_hash and drawn and rows: + seed = draw_seed(round_.server_seed, round_.draw_block_hash, round_.participants_hash) + index = draw_index(seed, len(rows)) + data.update(seed=f"{seed:064x}", index=index, winner_position=index, winner_address=rows[index]["address"]) + data["checks"] = _checks(round_, rows, index) + elif round_.server_seed: # closed with nobody in it: the secret is revealed, there is nothing to draw + data["checks"] = {"commitment": server_seed_commitment(round_.server_seed) == round_.server_seed_hash} + return data + + +def _checks(round_: Round, rows: list[dict[str, Any]], index: int) -> dict[str, bool]: + """The recomputation the page shows, done with the same primitives anyone can reimplement from the guide.""" + stored_winner = (Participation.objects.filter(round=round_, user_id=round_.winner_id).values_list("draw_position", flat=True) + .first() if round_.winner_id else None) + return { + "commitment": server_seed_commitment(round_.server_seed) == round_.server_seed_hash, + "order": [r["position"] for r in rows] == list(range(len(rows))) and [r["txid"] for r in rows] == sorted(r["txid"] for r in rows), + "fingerprint": participants_hash([(r["txid"], r["vout"], r["address"]) for r in rows]) == round_.participants_hash, + "winner": stored_winner == index, + } diff --git a/apps/web/public_views.py b/apps/web/public_views.py new file mode 100644 index 0000000..55631fd --- /dev/null +++ b/apps/web/public_views.py @@ -0,0 +1,48 @@ +from django.core.paginator import Paginator +from django.http import JsonResponse +from django.shortcuts import get_object_or_404, render +from django.views.decorators.cache import never_cache + +from apps.lottery.format import plm_text +from apps.lottery.models import LotteryConfig, Round +from apps.lottery.proof import round_proof +from apps.lottery.rules import WINNER_PERCENT + +PAGE_SIZE = 20 + + +def results(request): + """Concluded rounds, newest first (WEB-12): who won is only ever an address of the public draw list.""" + rounds = Round.objects.filter(status=Round.Status.CLOSED).order_by("-id") + page = Paginator(rounds, PAGE_SIZE).get_page(request.GET.get("pagina")) + rows = [{"id": r.pk, "closed_at": r.closed_at, "pool_text": plm_text(r.pool_amount_sats or 0), + "winner_text": plm_text(r.winner_amount_sats) if r.winner_amount_sats else "", + "has_winner": r.winner_id is not None} for r in page] + return render(request, "web/results.html", {"rows": rows, "page": page}) + + +@never_cache +def result_detail(request, round_id): + round_ = get_object_or_404(Round.objects.select_related("payout_transaction"), pk=round_id) + return render(request, "web/result_detail.html", {"p": round_proof(round_), "round": round_}) + + +@never_cache +def result_proof_json(request, round_id): + round_ = get_object_or_404(Round.objects.select_related("payout_transaction"), pk=round_id) + return JsonResponse(round_proof(round_)) + + +def _terms(): + config = LotteryConfig.get() + return {"bet_text": plm_text(config.bet_amount_sats), "winner_percent": WINNER_PERCENT, + "platform_percent": 100 - WINNER_PERCENT, "duration_minutes": max(1, config.round_duration_seconds // 60), + "confirmations": config.draw_confirmations} + + +def guide(request): + return render(request, "web/guide.html", _terms()) + + +def rules(request): + return render(request, "web/rules.html", _terms()) diff --git a/apps/web/static/web/site.css b/apps/web/static/web/site.css index 6f947fa..3098900 100644 --- a/apps/web/static/web/site.css +++ b/apps/web/static/web/site.css @@ -122,3 +122,14 @@ input[type=text].address { width: auto; } .movements td::before { content: attr(data-label); color: var(--muted); font-size: .9rem; text-align: left; } .movements td.num { text-align: right; } } + +/* A8 — public results, guide, rules */ +.break { overflow-wrap: anywhere; word-break: break-all; } +.facts dt { color: var(--muted); font-size: .9rem; margin-top: .8rem; } +.facts dd { margin: .15rem 0 0; } +.checks li { padding: .35rem 0 .35rem 1.6rem; position: relative; } +.checks li::before { position: absolute; left: 0; font-weight: 700; } +.checks li[data-ok="1"]::before { content: "✓"; color: var(--ok); } +.checks li[data-ok="0"]::before { content: "✗"; color: var(--error); } +.movements tr.winner td { font-weight: 600; } +.steps li, .rules li { margin-bottom: .7rem; max-width: 42rem; } diff --git a/apps/web/templates/web/base.html b/apps/web/templates/web/base.html index 8cbdea2..6f6a053 100644 --- a/apps/web/templates/web/base.html +++ b/apps/web/templates/web/base.html @@ -15,6 +15,9 @@ PLM Lottery