Files
plm-lottery/tests/unit/fake_network.py
T
davideandClaude Sonnet 5.5 a4b160e240 Abandon a transaction only on proof, and keep watching failed rows (B-78, TX-15, TX-17, TX-23, TX-26, ELC-28, DAT-33, DAT-36, DAT-69, DAT-72, AUD-11, AUD-12, AUD-31, AUD-39, API-32)
reconcile_once abandoned a row when the *active* server's history lacked its txid:
after 120s for `building` (which a restart onto a different server makes likely),
after 6h for `pending`, and all at once after a forward clock jump. It re-checked the
status but not the txid, so it raced the bumper, and a `failed` row was never looked at
again.

- The reconciler asks every server (ElectrumListener.get_histories, active + others) for
  the history of the row's address and looks for every txid of the row. Present on any
  server -> promoted (adopting the version found). Absent is only "no server of the
  quorum knows it" and needs the active server plus the quorum minimum of the others to
  have answered; even then nothing is released without proof:
  (a) an input spent in a block by a transaction that is none of the row's versions,
      agreed by a strict majority of a quorum of the other servers (confirmed_spender), or
  (b) a rebroadcast of the latest raw tx to every server (rebroadcast_everywhere)
      refused by all of them, explicitly, for missing/spent inputs.
  Accepted or "already known" by any server means alive; a fee refusal is not proof
  (`building` -> `pending` for the bumper, `pending` untouched); too few answers or
  anything else leaves the row alone. failure_reason names the proof.
- The write re-reads status *and* current_txid under begin_immediate (TX-26).
- pending_transactions.failed_at (set by _abandon and _release_failed_withdrawal) starts
  a 14-day watch: a failed row that a majority of the other servers (at least the quorum
  minimum) list in a block becomes `confirmed`, writes pending_tx_resurrected once and
  pauses the lottery with paused_reason `resurrected_tx`; linked rows are not corrected.
  The confirmation is by quorum of histories for now; merkle proofs come with B-83.
- round_config.paused_reason (migration b7d2e41c9a05, with the failed_at one): `operator`
  from POST /admin/pause (which accepts an optional `note` and never overwrites an
  automatic reason), cleared by resume, which logs the reason it lifted; exposed on
  /admin/config and GET /rounds/current. rounds/pause.py:pause_automatically is the
  system's way in.
- PendingTransactionReconciler takes the listener as `network`; with none it can promote
  but never abandon.

Tests changed because the spec changed: the three reconciler tests that expect an
abandonment (and the one in test_tx_versions) now supply a network that proves the
transaction is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:07:09 +02:00

73 lines
3.0 KiB
Python

"""Test doubles for what the services ask of the network (B-76 and after)."""
from app.electrum.client import ElectrumRpcError
def rpc_error(message: str, code: int = -26) -> ElectrumRpcError:
"""A server that answered with a JSON-RPC `error` object — an explicit refusal."""
return ElectrumRpcError({"code": code, "message": message})
class FakeNetwork:
"""The ElectrumListener surface a broadcast needs: `known` is what
`transaction_known` answers (True: some server has it, False: every server denies
it, None: too few answers to tell); fanned-out transactions are recorded."""
def __init__(self, known: bool | None = False):
self.known = known
self.asked: list[str] = []
self.fanned_out: list[str] = []
async def transaction_known(self, txid: str) -> bool | None:
self.asked.append(txid)
return self.known
def fan_out_transaction(self, raw_tx_hex: str) -> None:
self.fanned_out.append(raw_tx_hex)
MISSING_INPUTS = "bad-txns-inputs-missingorspent"
class ScriptedNetwork(FakeNetwork):
"""What the reconciler asks of the listener (B-78), scripted per test.
`active` and `others` are the history answers of the active server and of each other
server — a list of {"tx_hash", "height"} entries, or None for a server that did not
answer. `spender` is what `confirmed_spender` returns (the txid of a mined
transaction spending the row's inputs, or None). `rebroadcast` is the
(active, others) pair of verdicts `rebroadcast_everywhere` returns: ("accepted", ""),
("refused", message) or None."""
def __init__(self, *, active=(), others=((), ()), spender=None, rebroadcast=None, others_needed=2):
super().__init__(known=False)
self.active = None if active is None else list(active)
self.others = tuple(None if o is None else list(o) for o in others)
self.spender = spender
self.rebroadcast = rebroadcast
self.others_needed = others_needed
self.history_calls = 0
self.rebroadcast_calls: list[str] = []
async def get_histories(self, scripthash):
from app.electrum.quorum import ServerAnswers
self.history_calls += 1
return ServerAnswers(self.active, self.others, self.others_needed)
async def confirmed_spender(self, scripthash, own_txids, outpoints):
return self.spender
async def rebroadcast_everywhere(self, raw_tx_hex):
from app.electrum.quorum import ServerAnswers
self.rebroadcast_calls.append(raw_tx_hex)
active, others = self.rebroadcast if self.rebroadcast is not None else (None, (None, None))
return ServerAnswers(active, tuple(others), self.others_needed)
@classmethod
def proof_of_absence(cls, message: str = MISSING_INPUTS, **kwargs):
"""Nobody of the quorum knows the tx and every server refuses it for missing inputs."""
refusal = ("refused", message)
return cls(rebroadcast=(refusal, (refusal, refusal)), **kwargs)