"""Reconciling what the database believes with what the network knows (roadmap B7, TX-33, TX-15..TX-17, TX-23, TX-26). A row is only ever given up on proof (ADR-51): a refusal of the newest version for missing or already spent inputs while no server knows any version of it. A timeout, silence or a refusal for the fee is never a proof. The second proof of TX-17 (an input spent in a block by another transaction) arrives with the reorg work of B10. """ import asyncio import logging from datetime import datetime, timedelta from django.db import transaction from django.utils import timezone from . import audit from .bets import WORKER, _fail from .confirmation import apply_confirmations, spending_address from .electrum_chain import Outcome from .models import ChainTransaction, LotteryConfig logger = logging.getLogger(__name__) GRACE_BUILDING = timedelta(seconds=120) GRACE_PENDING = timedelta(hours=6) FAILED_WATCH = timedelta(days=14) # Wordings by which a node says the inputs are not there to spend. Anything else is not a proof (TX-17). _MISSING_INPUTS = ("missing-inputs", "missing inputs", "missingorspent", "already spent") def is_missing_inputs(reason: str) -> bool: lowered = reason.lower() return any(marker in lowered for marker in _MISSING_INPUTS) def _is_due(tx: ChainTransaction, now: datetime) -> bool: if tx.status == ChainTransaction.Status.BUILDING: return tx.current_txid is not None and now - tx.created_at >= GRACE_BUILDING return now - (tx.broadcast_at or tx.created_at) >= GRACE_PENDING def _promote(tx_id: int, now: datetime, txid: str) -> bool: """TX-16: the network has it. A `building` row becomes `pending` (so confirmation and RBF can take over).""" with transaction.atomic(): fresh = ChainTransaction.objects.select_for_update().get(pk=tx_id) if fresh.status != ChainTransaction.Status.BUILDING or fresh.current_txid != txid: # TX-26 return False fresh.status, fresh.broadcast_at, fresh.last_broadcast_at = ChainTransaction.Status.PENDING, now, now fresh.save(update_fields=["status", "broadcast_at", "last_broadcast_at"]) audit.record("pending_tx_recovered", actor_label=WORKER, subject_type="chain_transaction", subject_id=fresh.pk, payload={"txid": txid}) return True def reconcile_once(chain, now: datetime | None = None) -> dict[str, int]: now = now or timezone.now() done = {"recovered": 0, "abandoned": 0, "resurrected": 0} rows = ChainTransaction.objects.filter( status__in=[ChainTransaction.Status.BUILDING, ChainTransaction.Status.PENDING] ).select_related("round").order_by("id") for tx in rows: if not _is_due(tx, now): continue try: _reconcile_row(tx, chain, now, done) except Exception: logger.exception("reconciling transaction %s failed", tx.pk) _watch_failed(chain, now, done) return done def _reconcile_row(tx: ChainTransaction, chain, now: datetime, done: dict[str, int]) -> None: address = spending_address(tx) txids = frozenset(tx.versions.values_list("txid", flat=True)) if not address or not txids: return known, complete = asyncio.run(chain.versions_known(address, txids)) if known: # TX-15: at least one server has at least one version: it is alive if _promote(tx.pk, now, tx.current_txid): done["recovered"] += 1 return if not complete or not tx.raw_hex: # an unreadable network proves nothing return verdict = asyncio.run(chain.broadcast(tx.raw_hex, tx.current_txid)) if verdict.outcome is Outcome.ACCEPTED: if _promote(tx.pk, now, tx.current_txid): done["recovered"] += 1 elif verdict.outcome is Outcome.REFUSED: if is_missing_inputs(verdict.reason): # TX-17, proof 2 _fail(tx, f"abandoned: {verdict.reason}", release=True, action="tx_abandoned") done["abandoned"] += 1 elif _promote(tx.pk, now, tx.current_txid): # refused for the fee: not a proof; let RBF accelerate it done["recovered"] += 1 def _watch_failed(chain, now: datetime, done: dict[str, int]) -> None: """TX-23: a transaction given up for dead can still confirm. If one does, say it once and stop new bets.""" rows = ChainTransaction.objects.filter( status=ChainTransaction.Status.FAILED, failed_at__gte=now - FAILED_WATCH ).select_related("round") wanted = [] for tx in rows: if audit_exists("tx_resurrected", tx.pk): continue address, txids = spending_address(tx), frozenset(tx.versions.values_list("txid", flat=True)) if address and txids: wanted.append((tx.pk, address, txids)) if not wanted: return for tx_id, (txid, height) in asyncio.run(chain.observe_confirmations(wanted)).items(): with transaction.atomic(): if audit_exists("tx_resurrected", tx_id): continue audit.record("tx_resurrected", actor_label=WORKER, subject_type="chain_transaction", subject_id=tx_id, outcome="error", payload={"txid": txid, "height": height}) LotteryConfig.objects.filter(pk=1).update(new_bets_suspended=True) # the operator puts things in order done["resurrected"] += 1 def audit_exists(action: str, subject_id: int) -> bool: from .models import AuditEntry return AuditEntry.objects.filter(action=action, subject_id=str(subject_id)).exists() __all__ = ["apply_confirmations", "is_missing_inputs", "reconcile_once"]