place_bet and request_withdrawal released everything on any exception from the broadcast, including the 15s timeout and a connection dropped after the write. The tx may have propagated: a bet's sats landed in the pool with no participant, a withdrawal left while its row said failed and its coins were spendable again. - client: ElectrumRpcError (subclass of ElectrumError) is raised only for a reply carrying a JSON-RPC `error`; timeouts, drops and write errors stay plain ElectrumError. - tx/outcome.py:broadcast_with_verdict sorts a broadcast into accepted / refused / uncertain. A refusal is verified first: ElectrumListener.transaction_known asks blockchain.transaction.get of the active server and every other one (10s each) — any server returning the tx means accepted; the active server plus MIN_QUORUM_RESPONSES others all denying it means refused (ALLOW_NO_QUORUM keeps the reduced rule); too few answers is uncertain. "already known" wording is only a shortcut that skips the check. With no network to ask, a refusal is uncertain. - place_bet / request_withdrawal / _trigger_payout use it: only a verified refusal releases; uncertain leaves the rows building for the reconciler, publishes SSE and POST /bets and POST /withdrawals answer 202 with status building; "already known" promotes a payout (payout_sent) like any accepted broadcast. - An accepted transaction is fanned out to every other server (ElectrumListener.fan_out_transaction, best effort, off the caller's path). The RBF replacement in bump_fee is left for B-77, which rewrites that function. Tests changed because the spec changed: the existing "refused broadcast" tests raised a bare RuntimeError (now an uncertain outcome) and are made explicit refusals (rpc_error) with a network that denies the tx; direct route calls pass a Response; FakePayoutListener gains the network surface. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
from fastapi import APIRouter, Depends, Request, Response, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.api.errors import from_api_error, http_error
|
|
from app.auth.dependencies import get_current_user
|
|
from app.bets.service import BetError, place_bet
|
|
from app.db.models import User
|
|
from app.db.session import get_session
|
|
|
|
router = APIRouter(prefix="/bets", tags=["bets"])
|
|
|
|
|
|
class BetResponse(BaseModel):
|
|
round_id: int
|
|
bet_txid: str
|
|
bet_amount_sats: int
|
|
status: str
|
|
|
|
|
|
@router.post("", response_model=BetResponse, status_code=status.HTTP_201_CREATED)
|
|
async def create_bet(
|
|
request: Request,
|
|
response: Response,
|
|
user: User = Depends(get_current_user),
|
|
session: AsyncSession = Depends(get_session),
|
|
) -> BetResponse:
|
|
listener = request.app.state.electrum_listener
|
|
if listener.client is None:
|
|
raise http_error(
|
|
status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
"network_unavailable",
|
|
"not connected to the network, try again shortly",
|
|
)
|
|
|
|
async with request.app.state.user_locks.acquire(user.id):
|
|
try:
|
|
participant = await place_bet(session, listener.client, user, network=listener)
|
|
except BetError as exc:
|
|
# A rejected broadcast isn't the client's fault — it's the network
|
|
# refusing our transaction, so it answers 502 rather than 400 (B-07).
|
|
code = (
|
|
status.HTTP_502_BAD_GATEWAY
|
|
if exc.code == "broadcast_failed"
|
|
else status.HTTP_503_SERVICE_UNAVAILABLE # B-88: write lock not granted, nothing sent
|
|
if exc.code == "service_busy"
|
|
else status.HTTP_400_BAD_REQUEST
|
|
)
|
|
raise from_api_error(code, exc) from exc
|
|
|
|
if participant.status == "building":
|
|
# B-76 (TX-20, BET-23): the outcome of the broadcast is uncertain — the tx may
|
|
# already be on the network — so the bet is accepted for processing, not
|
|
# confirmed as sent, and the reconciler settles it.
|
|
response.status_code = status.HTTP_202_ACCEPTED
|
|
return BetResponse(
|
|
round_id=participant.round_id,
|
|
bet_txid=participant.bet_txid,
|
|
bet_amount_sats=participant.bet_amount_sats,
|
|
status=participant.status,
|
|
)
|