64 lines
3.7 KiB
Python
64 lines
3.7 KiB
Python
"""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,
|
|
}
|