Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
"""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."""
|
|
|
|
|
|
2026-10-14 15:29:30 +02:00
|
|
|
def __init__(self, known: bool | None = False, proven: bool = True):
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
self.known = known
|
2026-10-14 15:29:30 +02:00
|
|
|
self.proven = proven # what `confirmation_proven` answers (B-83)
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
self.asked: list[str] = []
|
|
|
|
|
self.fanned_out: list[str] = []
|
2026-10-14 15:29:30 +02:00
|
|
|
self.proofs_asked: list[tuple[str, int]] = []
|
Credit deposits at deposit_confirmations and verify the round's coins with the quorum before paying (B-82, DEP-03, DEP-04, DEP-09, DEP-12, DEP-14, ELC-17, ELC-27, RND-21, RND-22, RND-25, RND-39, DAT-10, DAT-11, DAT-24, DAT-62, DAT-67, DAT-68, AUD-12, AUD-44, AUD-45, API-30, API-31, API-36, ADR-06, ADR-52)
2026-10-16 14:25:35 +02:00
|
|
|
# B-82: what `verify_pool_outpoints` answers. Every bet output is present unless listed
|
|
|
|
|
# in `pool_absent`; `pool_unavailable` models a quorum that could not be reached.
|
|
|
|
|
self.pool_absent: set[tuple[str, int]] = set()
|
|
|
|
|
self.pool_unavailable = False
|
|
|
|
|
self.verified_scripthashes: list[str] = []
|
|
|
|
|
self.verified_outpoints: list[list[tuple[str, int, int]]] = []
|
Never roll back an ambiguous broadcast as if it had been refused (B-76, TX-04, TX-05, TX-20, TX-21, BET-16, BET-17, BET-23, WDR-11, WDR-12, WDR-16, API-13)
2026-10-14 14:22:20 +02:00
|
|
|
|
|
|
|
|
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)
|
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)
2026-10-14 15:07:09 +02:00
|
|
|
|
2026-10-14 15:29:30 +02:00
|
|
|
async def confirmation_proven(self, txid: str, height: int) -> bool:
|
|
|
|
|
self.proofs_asked.append((txid, height))
|
|
|
|
|
return self.proven
|
|
|
|
|
|
Credit deposits at deposit_confirmations and verify the round's coins with the quorum before paying (B-82, DEP-03, DEP-04, DEP-09, DEP-12, DEP-14, ELC-17, ELC-27, RND-21, RND-22, RND-25, RND-39, DAT-10, DAT-11, DAT-24, DAT-62, DAT-67, DAT-68, AUD-12, AUD-44, AUD-45, API-30, API-31, API-36, ADR-06, ADR-52)
2026-10-16 14:25:35 +02:00
|
|
|
async def verify_pool_outpoints(self, scripthash, outpoints):
|
|
|
|
|
self.verified_scripthashes.append(scripthash)
|
|
|
|
|
self.verified_outpoints.append(list(outpoints))
|
|
|
|
|
hook = getattr(self, "on_verify", None)
|
|
|
|
|
if hook is not None:
|
|
|
|
|
await hook()
|
|
|
|
|
if self.pool_unavailable:
|
|
|
|
|
return None
|
|
|
|
|
return {(txid, vout): (txid, vout) not in self.pool_absent for txid, vout, _amount in outpoints}
|
|
|
|
|
|
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)
2026-10-14 15:07:09 +02:00
|
|
|
|
|
|
|
|
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."""
|
|
|
|
|
|
2026-10-14 15:29:30 +02:00
|
|
|
def __init__(self, *, active=(), others=((), ()), spender=None, rebroadcast=None, others_needed=2, proven=True):
|
|
|
|
|
super().__init__(known=False, proven=proven)
|
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)
2026-10-14 15:07:09 +02:00
|
|
|
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)
|