Make the draw a commit-reveal at a fixed, confirmed, quorum-agreed height with a public proof (B-73, RND-14 … RND-20, RND-33 … RND-37, DAT-61 … DAT-66, API-25, API-45, AUD-24 … AUD-27, AUD-37, ELC-15, SEC-56, ADR-24, ADR-25)
The seed was the hash of the first block seen after the close, so a miner holding tickets
could withhold blocks until one made them win, the active Electrum server could pick which
real block was used by not announcing another (a failed corroboration even moved the draw to
the next block), and the participant order was the server-only `broadcast_at` with no
tie-break, so nobody could recompute the result.
- Open: every round gets a 32-byte `server_seed`, stored only Fernet-encrypted with the
master-xprv key (`server_seed_enc`), with its published `server_seed_hash`;
`draw_confirmations` is copied from round_config. The plaintext column is written only by
the reveal.
- Entering `drawing` (one transaction, `draw_committed` audit row): participants ordered by
bet txid, each `draw_position`, `participants_hash` (sha256 of `txid:vout:address\n`
lines; new `bet_vout`), `draw_height = tip_at_close + 1`. A tip of 0 keeps the round
`closing` (`draw_baseline_tip_unknown`, once per round). All persisted, so a resumed draw
reads rather than recomputes (the `_tick` branch itself is B-81).
- `_complete_draw` / `_wait_for_draw_block` replace `_wait_for_next_block`: wait for
`draw_confirmations` blocks from `draw_height`, then `ElectrumListener.agreed_block_hash`
asks every server, the active one included, for the header at that exact height (valid
work, >= 2 other answers, strict majority of all); no agreement means the same height is
asked again and `draw_header_corroboration_failed` is written once per distinct reported
hash. `draw_stalled` now counts from the last tip advance, or from when the quorum started
to fail. Seed = sha256(server_seed || block_hash || participants_hash), index = seed mod n.
- The winner, the seed and the reveal (`server_seed`, `winner_drawn` payload) are written in
one transaction; a round closing with no participants reveals too. A round opened before
this change (no commitment) is given a secret as it enters `drawing`.
- `GET /rounds/{id}/proof` (public, 404 `round_not_found`, translated in all 7 languages) and
the new fields on `GET /rounds/current`; `draw_confirmations` in `/admin/config` and the
/admin form; `server_seed_hash`/`draw_height` in `/admin/rounds`, never the secret.
- Alembic d3e8b15a7c42 (plain add_column / create_index, expression indexes untouched).
- Docs: the B-73 `*Stato:*`/`*Oggi:*` lines are gone, guides and CLAUDE.md describe the new
draw. Ambiguities resolved in api.md: the proof's participants also carry `amount_sats`
(RND-37 lists it); `seed` is decimal in the proof and hex in the audit row, as specified.
Tests changed because the spec changed: ElectrumListener.corroborate_header (asked only the
other servers about the block the active one announced) is replaced by agreed_block_hash and
its quorum tests ported; draw_winner takes the secret and the list fingerprint; the four
_wait_for_next_block scheduler tests are replaced by _wait_for_draw_block ones (fixed height,
confirmations, same-height retry, stall clock). New: test_draw_proof.py recomputes the winner
independently from the public proof, checks the plaintext secret is nowhere in the database,
the API or the log before the reveal, and that a second pass over a `drawing` round yields
the same winner. Mutation-checked: ordering by broadcast_at, an early reveal and asking the
tip instead of the draw height each fail tests.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
82d078e2d6
commit
db24287ea5
42 files changed
+1715
-620
No files matched your search
@@ -45,6 +45,7 @@ _CONFIG_FIELDS = (
|
||||
"fee_rate_sat_vb",
|
||||
"rbf_timeout_seconds",
|
||||
"draw_animation_seconds",
|
||||
"draw_confirmations", # B-73 (DAT-62)
|
||||
)
|
||||
|
||||
|
||||
@@ -56,6 +57,7 @@ class RoundConfigResponse(BaseModel):
|
||||
fee_rate_sat_vb: int
|
||||
rbf_timeout_seconds: int
|
||||
draw_animation_seconds: int
|
||||
draw_confirmations: int # B-73 (DAT-62)
|
||||
paused: bool
|
||||
paused_reason: str | None = None # B-78 (DAT-69): why, or None when not paused
|
||||
|
||||
@@ -77,6 +79,7 @@ class RoundConfigUpdate(BaseModel):
|
||||
fee_rate_sat_vb: int | None = Field(default=None, ge=1, le=MAX_FEE_RATE_SAT_VB)
|
||||
rbf_timeout_seconds: int | None = Field(default=None, ge=60, le=7 * 24 * 3600)
|
||||
draw_animation_seconds: int | None = Field(default=None, ge=0, le=600)
|
||||
draw_confirmations: int | None = Field(default=None, ge=1, le=20) # B-73 (DAT-62)
|
||||
|
||||
@field_validator("fee_address")
|
||||
@classmethod
|
||||
@@ -264,6 +267,8 @@ class AdminRoundResponse(BaseModel):
|
||||
fee_amount_sats: int | None
|
||||
payout_txid: str | None
|
||||
pool_address: str | None # B-80
|
||||
server_seed_hash: str | None # B-73 (API-36): the commitment only — never the secret
|
||||
draw_height: int | None # B-73
|
||||
|
||||
|
||||
@router.get("/rounds", response_model=list[AdminRoundResponse], dependencies=[Depends(require_admin)])
|
||||
@@ -292,6 +297,8 @@ async def list_rounds(
|
||||
fee_amount_sats=r.fee_amount_sats,
|
||||
payout_txid=r.payout_txid,
|
||||
pool_address=r.pool_address or None,
|
||||
server_seed_hash=r.server_seed_hash,
|
||||
draw_height=r.draw_height,
|
||||
)
|
||||
for r in rounds
|
||||
]
|
||||
|
||||
+100
-1
@@ -9,9 +9,10 @@ from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.client_ip import client_ip
|
||||
from app.api.errors import http_error
|
||||
from app.api.timeutil import isoformat_utc
|
||||
from app.auth.dependencies import get_optional_user
|
||||
from app.db.models import RoundParticipant, User
|
||||
from app.db.models import Round, RoundParticipant, User
|
||||
from app.db.session import get_session
|
||||
from app.rounds.config import get_round_config
|
||||
from app.rounds.events import EVICTED, RoundEventCapacityError, broadcaster
|
||||
@@ -118,6 +119,13 @@ class CurrentRoundResponse(BaseModel):
|
||||
# waiting for a block" rather than a countdown implying a bounded wait — this
|
||||
# phase has no timeout, only draw_animation_seconds' cosmetic minimum.
|
||||
draw_waiting_since: str | None = None
|
||||
# B-73 (RND-34, RND-14): the commitment published from the opening, and what is fixed
|
||||
# on entering "drawing". The secret itself is never in this response (API-45 has it, once
|
||||
# revealed).
|
||||
server_seed_hash: str | None = None
|
||||
participants_hash: str | None = None
|
||||
draw_height: int | None = None
|
||||
draw_confirmations: int | None = None
|
||||
chain_tip_height: int | None = None
|
||||
lottery_paused: bool = False
|
||||
paused_reason: str | None = None # B-78 (DAT-69): operator / resurrected_tx / ..., None if not paused
|
||||
@@ -225,9 +233,100 @@ async def current_round(
|
||||
draw_block_height=round_.draw_block_height,
|
||||
draw_block_hash=round_.draw_block_hash,
|
||||
draw_waiting_since=isoformat_utc(round_.drawing_started_at) if round_.status == "drawing" else None,
|
||||
server_seed_hash=round_.server_seed_hash,
|
||||
participants_hash=round_.participants_hash,
|
||||
draw_height=round_.draw_height,
|
||||
draw_confirmations=round_.draw_confirmations,
|
||||
chain_tip_height=chain_tip_height,
|
||||
lottery_paused=config.paused,
|
||||
paused_reason=config.paused_reason,
|
||||
lottery_configured=rounds_can_open(config),
|
||||
user_played=user_played,
|
||||
)
|
||||
|
||||
|
||||
class ProofParticipant(BaseModel):
|
||||
draw_position: int
|
||||
txid: str
|
||||
vout: int
|
||||
address: str
|
||||
amount_sats: int
|
||||
status: str
|
||||
|
||||
|
||||
class RoundProofResponse(BaseModel):
|
||||
round_id: int
|
||||
status: str
|
||||
algorithm: str
|
||||
pool_address: str | None
|
||||
server_seed_hash: str | None
|
||||
server_seed: str | None
|
||||
draw_height: int | None
|
||||
draw_confirmations: int
|
||||
participants: list[ProofParticipant]
|
||||
participants_hash: str | None
|
||||
draw_block_hash: str | None
|
||||
seed: str | None
|
||||
winner_index: int | None
|
||||
winner_address: str | None
|
||||
payout_txid: str | None
|
||||
|
||||
|
||||
@router.get("/{round_id}/proof", response_model=RoundProofResponse)
|
||||
async def round_proof(round_id: int, session: AsyncSession = Depends(get_session)) -> RoundProofResponse:
|
||||
"""B-73 (RND-37, API-45): everything needed to recompute a round's winner without
|
||||
trusting the server — public, unauthenticated, for a round in any state. A field that
|
||||
does not exist yet is null (or an empty list).
|
||||
|
||||
`server_seed` is the plaintext column, which is written only by the reveal (the same
|
||||
transaction that stores the winner, or the close of a round without participants), so
|
||||
there is no state of the round in which this endpoint can return it early.
|
||||
"""
|
||||
round_ = await session.get(Round, round_id)
|
||||
if round_ is None:
|
||||
raise http_error(404, "round_not_found", "round not found")
|
||||
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(RoundParticipant, User.address)
|
||||
.join(User, User.id == RoundParticipant.user_id)
|
||||
.where(RoundParticipant.round_id == round_id, RoundParticipant.draw_position.is_not(None))
|
||||
.order_by(RoundParticipant.draw_position)
|
||||
)
|
||||
).all()
|
||||
participants = [
|
||||
ProofParticipant(
|
||||
draw_position=p.draw_position,
|
||||
txid=p.bet_txid,
|
||||
vout=p.bet_vout,
|
||||
address=address,
|
||||
amount_sats=p.bet_amount_sats,
|
||||
status=p.status,
|
||||
)
|
||||
for p, address in rows
|
||||
]
|
||||
|
||||
seed_decimal = round_.seed_int
|
||||
winner_index = None
|
||||
winner_address = None
|
||||
if seed_decimal is not None and participants:
|
||||
winner_index = int(seed_decimal) % len(participants)
|
||||
winner_address = participants[winner_index].address
|
||||
|
||||
return RoundProofResponse(
|
||||
round_id=round_.id,
|
||||
status=round_.status,
|
||||
algorithm="commit-reveal-v1" if round_.server_seed_hash else "block-hash-v0",
|
||||
pool_address=round_.pool_address or None,
|
||||
server_seed_hash=round_.server_seed_hash,
|
||||
server_seed=round_.server_seed,
|
||||
draw_height=round_.draw_height,
|
||||
draw_confirmations=round_.draw_confirmations,
|
||||
participants=participants,
|
||||
participants_hash=round_.participants_hash,
|
||||
draw_block_hash=round_.draw_block_hash,
|
||||
seed=seed_decimal,
|
||||
winner_index=winner_index,
|
||||
winner_address=winner_address,
|
||||
payout_txid=round_.payout_txid,
|
||||
)
|
||||
Reference in new issue
Block a user