375 lines
16 KiB
Python
375 lines
16 KiB
Python
"""B6 — a real bet: reservation, signing in the worker, broadcast verdicts: BET-30..BET-33, TX-20, TX-26, ARC-72, ARC-78."""
|
|||
|
|
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
from datetime import timedelta
|
||
|
|
from pathlib import Path
|
||
|
|
from uuid import uuid4
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from django.utils import timezone
|
||
|
|
from embit.transaction import Transaction
|
||
|
|
from plm_engine.electrum.client import ElectrumEndpoint, ElectrumRpcError
|
||
|
|
from plm_engine.psbt_builder import DUST_LIMIT_SATS
|
||
|
|
|
||
|
|
from apps.lottery import bets, chain, keys, reservation
|
||
|
|
from apps.lottery.balances import balance_of
|
||
|
|
from apps.lottery.electrum_chain import ElectrumChain, Outcome, Verdict
|
||
|
|
from apps.lottery.errors import LedgerError
|
||
|
|
from apps.lottery.ledger import ChainLedger, get_ledger
|
||
|
|
from apps.lottery.models import (
|
||
|
|
AuditEntry, ChainTransaction, Deposit, Movement, Participation, Round, Utxo,
|
||
|
|
)
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parents[2]
|
||
|
|
User = get_user_model()
|
||
|
|
PLM = 100_000_000
|
||
|
|
BET = 10 * PLM
|
||
|
|
pytestmark = pytest.mark.django_db
|
||
|
|
ledger = ChainLedger()
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def world(settings, tmp_path):
|
||
|
|
"""A real master key: the public part configured as the web has it, the private part as the worker has it."""
|
||
|
|
settings.LEDGER_BACKEND = "chain"
|
||
|
|
settings.PLM_ACCOUNT_XPUB = keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)
|
||
|
|
settings.PLM_MASTER_KEY_FILE = str(tmp_path / "keys" / "master.key")
|
||
|
|
settings.PLM_KEY_ENCRYPTION_KEY_FILE = str(tmp_path / "keys" / "master.encryption.key")
|
||
|
|
settings.PROCESS_ROLE = "worker"
|
||
|
|
return settings
|
||
|
|
|
||
|
|
|
||
|
|
def make_round():
|
||
|
|
round_ = Round.objects.create(opened_at=timezone.now() - timedelta(seconds=30), bet_amount_sats=BET)
|
||
|
|
round_.pool_address = chain.derive_pool_address(round_.pk)
|
||
|
|
round_.save(update_fields=["pool_address"])
|
||
|
|
return round_
|
||
|
|
|
||
|
|
|
||
|
|
_counter = iter(range(1, 10_000))
|
||
|
|
|
||
|
|
|
||
|
|
def fund(user, amount, *, credited=True):
|
||
|
|
"""A deposit the scan has already registered: a coin of the player's own address."""
|
||
|
|
address = chain.assign_deposit_address(user).address
|
||
|
|
n = next(_counter)
|
||
|
|
utxo = Utxo.objects.create(txid=f"{n:064x}", vout=0, amount_sats=amount, address=address, user=user, confirmed_height=100)
|
||
|
|
movement = Movement.objects.create(
|
||
|
|
user=user, kind="deposit", status="confirmed" if credited else "pending", amount_sats=amount,
|
||
|
|
confirmed_at=timezone.now() if credited else None)
|
||
|
|
Deposit.objects.create(user=user, utxo=utxo, movement=movement, status="credited" if credited else "detected",
|
||
|
|
credited_at=timezone.now() if credited else None)
|
||
|
|
return utxo
|
||
|
|
|
||
|
|
|
||
|
|
def player(name="mario", funds=3 * BET):
|
||
|
|
user = User.objects.create_user(name, password="x" * 12)
|
||
|
|
if funds:
|
||
|
|
fund(user, funds)
|
||
|
|
return user
|
||
|
|
|
||
|
|
|
||
|
|
def bet(user, round_):
|
||
|
|
return ledger.place_bet(user, round_.pk, request_id=uuid4())
|
||
|
|
|
||
|
|
|
||
|
|
# --- BET-30: admission and reservation ----------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_bet_reserves_its_coins_and_takes_the_quota_from_the_balance(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
result = bet(user, round_)
|
||
|
|
tx = ChainTransaction.objects.get()
|
||
|
|
assert (tx.kind, tx.status, tx.current_txid) == ("bet", "building", None)
|
||
|
|
assert list(tx.reserved_utxos.values_list("amount_sats", flat=True)) == [3 * BET]
|
||
|
|
assert result.obj.status == "pending" and balance_of(user).spendable_sats == 2 * BET
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_reserved_coin_is_not_offered_again(world):
|
||
|
|
user, round_ = player(funds=BET + 2 * PLM), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
assert not reservation.spendable(user).exists()
|
||
|
|
|
||
|
|
|
||
|
|
def test_waiting_deposits_are_not_spendable(world):
|
||
|
|
user, round_ = player(funds=0), make_round()
|
||
|
|
fund(user, 2 * BET, credited=False)
|
||
|
|
with pytest.raises(LedgerError) as refused:
|
||
|
|
bet(user, round_)
|
||
|
|
assert refused.value.code == "balance_pending_confirmation" and not ChainTransaction.objects.exists()
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_balance_that_leaves_no_change_is_refused_with_the_documented_code(world):
|
||
|
|
user, round_ = player(funds=BET), make_round()
|
||
|
|
with pytest.raises(LedgerError) as refused:
|
||
|
|
bet(user, round_)
|
||
|
|
assert refused.value.code == "balance_leaves_no_change"
|
||
|
|
assert not ChainTransaction.objects.exists() and not Utxo.objects.filter(reserved_by__isnull=False).exists()
|
||
|
|
|
||
|
|
|
||
|
|
def test_not_enough_money_is_refused(world):
|
||
|
|
user, round_ = player(funds=PLM), make_round()
|
||
|
|
with pytest.raises(LedgerError) as refused:
|
||
|
|
bet(user, round_)
|
||
|
|
assert refused.value.code == "insufficient_balance"
|
||
|
|
|
||
|
|
|
||
|
|
def test_the_same_request_is_one_bet(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
key = uuid4()
|
||
|
|
first = ledger.place_bet(user, round_.pk, request_id=key)
|
||
|
|
again = ledger.place_bet(user, round_.pk, request_id=key)
|
||
|
|
assert again.replayed and again.obj == first.obj and ChainTransaction.objects.count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_second_bet_in_the_round_is_refused_and_reserves_nothing(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
with pytest.raises(LedgerError) as refused:
|
||
|
|
bet(user, round_)
|
||
|
|
assert refused.value.code == "already_betting" and ChainTransaction.objects.count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
def test_the_chain_ledger_does_not_pretend_what_is_not_there_yet(world):
|
||
|
|
with pytest.raises(LedgerError):
|
||
|
|
ledger.deposit(player(), PLM)
|
||
|
|
with pytest.raises(NotImplementedError):
|
||
|
|
ledger.pay_winner(1)
|
||
|
|
assert isinstance(get_ledger(), ChainLedger)
|
||
|
|
|
||
|
|
|
||
|
|
_SETUP = """
|
||
|
|
import django, json
|
||
|
|
django.setup()
|
||
|
|
from uuid import uuid4
|
||
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from django.utils import timezone
|
||
|
|
from apps.lottery import chain
|
||
|
|
from apps.lottery.models import Utxo, Deposit, Movement
|
||
|
|
user = get_user_model().objects.create_user('mario', password='x'*12)
|
||
|
|
address = chain.assign_deposit_address(user).address
|
||
|
|
utxo = Utxo.objects.create(txid='ab'*32, vout=0, amount_sats=3_000_000_000, address=address, user=user, confirmed_height=1)
|
||
|
|
m = Movement.objects.create(user=user, kind='deposit', status='confirmed', amount_sats=3_000_000_000, confirmed_at=timezone.now())
|
||
|
|
Deposit.objects.create(user=user, utxo=utxo, movement=m, status='credited', credited_at=timezone.now())
|
||
|
|
"""
|
||
|
|
|
||
|
|
_RACE = """
|
||
|
|
import django, json
|
||
|
|
django.setup()
|
||
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from apps.lottery import reservation
|
||
|
|
from apps.lottery.models import ChainTransaction
|
||
|
|
user = get_user_model().objects.get(username='mario')
|
||
|
|
tx = ChainTransaction.objects.create(kind='bet', user=user)
|
||
|
|
try:
|
||
|
|
reservation.reserve_for(user, 1_000_000_000, tx)
|
||
|
|
print(json.dumps('won'))
|
||
|
|
except Exception as exc:
|
||
|
|
print(json.dumps(type(exc).__name__))
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
def test_two_requests_never_reserve_the_same_coin(tmp_path, settings):
|
||
|
|
"""ARC-72: six processes want the one coin of a player at once; exactly one gets it."""
|
||
|
|
env = {"PATH": os.environ["PATH"], "PYTHONPATH": str(ROOT), "DJANGO_SECRET_KEY": "k" * 60,
|
||
|
|
"DJANGO_DB_PATH": str(tmp_path / "site.sqlite3"), "DJANGO_SETTINGS_MODULE": "config.settings",
|
||
|
|
"PLM_ACCOUNT_XPUB": keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE)}
|
||
|
|
subprocess.run([sys.executable, "manage.py", "migrate", "--noinput", "-v", "0"], cwd=ROOT, env=env, check=True)
|
||
|
|
subprocess.run([sys.executable, "-c", _SETUP], cwd=ROOT, env=env, check=True)
|
||
|
|
procs = [subprocess.Popen([sys.executable, "-c", _RACE], cwd=ROOT, env=env, stdout=subprocess.PIPE,
|
||
|
|
stderr=subprocess.PIPE, text=True) for _ in range(6)]
|
||
|
|
outputs = [p.communicate(timeout=120) for p in procs]
|
||
|
|
assert all(p.returncode == 0 for p in procs), [o[1] for o in outputs]
|
||
|
|
results = [json.loads(o[0].strip().splitlines()[-1]) for o in outputs]
|
||
|
|
assert results.count("won") == 1 and results.count("InsufficientFundsError") == 5, results
|
||
|
|
|
||
|
|
|
||
|
|
# --- BET-31: signing in the worker, saved before it is sent ---------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
def test_without_the_signing_key_nothing_is_signed(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
world.PROCESS_ROLE = "web"
|
||
|
|
assert bets.sign_pending() == 0
|
||
|
|
tx = ChainTransaction.objects.get()
|
||
|
|
assert tx.current_txid is None and not tx.versions.exists()
|
||
|
|
|
||
|
|
|
||
|
|
def test_the_stake_in_the_pool_is_the_quota_minus_the_fee(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
assert bets.sign_pending() == 1
|
||
|
|
tx = ChainTransaction.objects.get()
|
||
|
|
participation = Participation.objects.get()
|
||
|
|
parsed = Transaction.parse(bytes.fromhex(tx.raw_hex))
|
||
|
|
assert tx.current_txid == parsed.txid().hex() and tx.versions.get().txid == tx.current_txid
|
||
|
|
assert participation.bet_txid == tx.current_txid and participation.bet_vout == 0
|
||
|
|
pool_out, change_out = parsed.vout
|
||
|
|
assert pool_out.value == participation.bet_amount_sats < BET # the fee comes out of the stake (BET-04)
|
||
|
|
assert pool_out.value + (BET - pool_out.value) == BET
|
||
|
|
assert change_out.value == 3 * BET - BET and change_out.value >= DUST_LIMIT_SATS
|
||
|
|
assert pool_out.script_pubkey.address(world.PLM_PROFILE.embit) == round_.pool_address
|
||
|
|
assert [(i.txid.hex(), i.vout) for i in parsed.vin] == [(u.txid, u.vout) for u in tx.reserved_utxos.all()]
|
||
|
|
assert AuditEntry.objects.filter(action="bet_signed").count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_signed_transaction_is_not_signed_twice(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
assert bets.sign_pending() == 1 and bets.sign_pending() == 0
|
||
|
|
assert ChainTransaction.objects.get().versions.count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
# --- BET-32: verdicts ------------------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
class ScriptedChain:
|
||
|
|
"""What `broadcast_pending` asks of the network: a verdict per call."""
|
||
|
|
|
||
|
|
def __init__(self, *verdicts):
|
||
|
|
self.verdicts, self.sent = list(verdicts), []
|
||
|
|
|
||
|
|
async def broadcast(self, raw, txid):
|
||
|
|
self.sent.append(txid)
|
||
|
|
return self.verdicts.pop(0) if self.verdicts else Verdict(Outcome.ACCEPTED, "")
|
||
|
|
|
||
|
|
|
||
|
|
def placed_and_signed(world):
|
||
|
|
user, round_ = player(), make_round()
|
||
|
|
bet(user, round_)
|
||
|
|
bets.sign_pending()
|
||
|
|
return user, round_, ChainTransaction.objects.get()
|
||
|
|
|
||
|
|
|
||
|
|
def test_the_transaction_is_saved_before_it_is_broadcast(world):
|
||
|
|
user, round_, tx = placed_and_signed(world)
|
||
|
|
assert tx.status == "building" and tx.current_txid and tx.versions.exists() # nothing was sent yet
|
||
|
|
class Crashing:
|
||
|
|
async def broadcast(self, raw, txid):
|
||
|
|
raise RuntimeError("the process died here")
|
||
|
|
with pytest.raises(RuntimeError):
|
||
|
|
bets.broadcast_pending(Crashing())
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "building" and tx.versions.count() == 1 # saved, still unsent: the next pass sends it
|
||
|
|
|
||
|
|
|
||
|
|
def test_an_accepted_broadcast_makes_the_bet_pending(world):
|
||
|
|
user, round_, tx = placed_and_signed(world)
|
||
|
|
assert bets.broadcast_pending(ScriptedChain())["accepted"] == 1
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "pending" and tx.broadcast_at is not None
|
||
|
|
assert AuditEntry.objects.filter(action="bet_broadcast").exists()
|
||
|
|
assert bets.broadcast_pending(ScriptedChain()) == {"accepted": 0, "refused": 0, "uncertain": 0} # not sent again
|
||
|
|
|
||
|
|
|
||
|
|
def test_a_proven_refusal_frees_the_coins(world):
|
||
|
|
user, round_, tx = placed_and_signed(world)
|
||
|
|
assert bets.broadcast_pending(ScriptedChain(Verdict(Outcome.REFUSED, "bad-txns-inputs-missingorspent")))["refused"] == 1
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "failed" and "missingorspent" in tx.failure_reason and tx.failed_at
|
||
|
|
assert Participation.objects.get().status == "rejected"
|
||
|
|
assert Movement.objects.get(kind="bet").status == "failed"
|
||
|
|
assert reservation.spendable(user).count() == 1 and balance_of(user).spendable_sats == 3 * BET
|
||
|
|
assert AuditEntry.objects.filter(action="bet_refused").exists()
|
||
|
|
|
||
|
|
|
||
|
|
def test_an_uncertain_broadcast_frees_nothing_and_is_retried(world):
|
||
|
|
user, round_, tx = placed_and_signed(world)
|
||
|
|
assert bets.broadcast_pending(ScriptedChain(Verdict(Outcome.UNCERTAIN, "timeout")))["uncertain"] == 1
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "building" and tx.reserved_utxos.count() == 1 and not reservation.spendable(user).exists()
|
||
|
|
retry = ScriptedChain()
|
||
|
|
assert bets.broadcast_pending(retry)["accepted"] == 1 and retry.sent == [tx.current_txid] # the same transaction
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "pending" and tx.versions.count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
def test_an_outcome_is_not_written_over_a_changed_row(world):
|
||
|
|
user, round_, tx = placed_and_signed(world)
|
||
|
|
other = "ee" * 32
|
||
|
|
assert bets._settle(tx.pk, other, Verdict(Outcome.ACCEPTED, ""), timezone.now()) is False # another txid (an RBF meanwhile)
|
||
|
|
ChainTransaction.objects.filter(pk=tx.pk).update(status="pending")
|
||
|
|
assert bets._settle(tx.pk, tx.current_txid, Verdict(Outcome.REFUSED, "late"), timezone.now()) is False
|
||
|
|
tx.refresh_from_db()
|
||
|
|
assert tx.status == "pending" and tx.reserved_utxos.count() == 1
|
||
|
|
|
||
|
|
|
||
|
|
# --- the verdict as the network phase reaches it ---------------------------------------------------------------
|
||
|
|
|
||
|
|
|
||
|
|
class Server:
|
||
|
|
def __init__(self, name, *, broadcast=None, knows=None):
|
||
|
|
self.name, self.broadcast_error, self.knows = name, broadcast, knows # knows: True / False / None (silent)
|
||
|
|
self.sent = []
|
||
|
|
|
||
|
|
|
||
|
|
class FakeClient:
|
||
|
|
def __init__(self, server):
|
||
|
|
self.s = server
|
||
|
|
|
||
|
|
async def connect(self):
|
||
|
|
pass
|
||
|
|
|
||
|
|
async def close(self):
|
||
|
|
pass
|
||
|
|
|
||
|
|
async def broadcast(self, raw):
|
||
|
|
self.s.sent.append(raw)
|
||
|
|
if self.s.broadcast_error is not None:
|
||
|
|
raise self.s.broadcast_error
|
||
|
|
return "txid"
|
||
|
|
|
||
|
|
async def get_transaction(self, txid, verbose=False):
|
||
|
|
if self.s.knows is True:
|
||
|
|
return "00"
|
||
|
|
if self.s.knows is False:
|
||
|
|
raise ElectrumRpcError({"code": 2, "message": "No such mempool or blockchain transaction"})
|
||
|
|
raise OSError("timeout")
|
||
|
|
|
||
|
|
|
||
|
|
def network(*servers, allow_no_quorum=False):
|
||
|
|
by_endpoint = {ElectrumEndpoint(s.name, 1, False): s for s in servers}
|
||
|
|
from plm_engine.network import REGTEST
|
||
|
|
|
||
|
|
return ElectrumChain(list(by_endpoint), REGTEST, allow_no_quorum, lambda e: FakeClient(by_endpoint[e]))
|
||
|
|
|
||
|
|
|
||
|
|
async def test_a_plain_acceptance_is_fanned_out_to_the_others():
|
||
|
|
servers = [Server("a"), Server("b"), Server("c")]
|
||
|
|
verdict = await network(*servers).broadcast("00ff", "tx")
|
||
|
|
assert verdict.outcome is Outcome.ACCEPTED and [len(s.sent) for s in servers] == [1, 1, 1]
|
||
|
|
|
||
|
|
|
||
|
|
async def test_already_known_counts_as_accepted():
|
||
|
|
refusal = ElectrumRpcError({"code": -26, "message": "txn-already-in-mempool"})
|
||
|
|
verdict = await network(Server("a", broadcast=refusal), Server("b"), Server("c")).broadcast("00ff", "tx")
|
||
|
|
assert verdict.outcome is Outcome.ACCEPTED
|
||
|
|
|
||
|
|
|
||
|
|
async def test_a_refusal_that_another_server_contradicts_is_an_acceptance():
|
||
|
|
refusal = ElectrumRpcError({"code": 1, "message": "weird wording"})
|
||
|
|
servers = [Server("a", broadcast=refusal, knows=False), Server("b", knows=True), Server("c", knows=False)]
|
||
|
|
assert (await network(*servers).broadcast("00ff", "tx")).outcome is Outcome.ACCEPTED
|
||
|
|
|
||
|
|
|
||
|
|
async def test_a_refusal_every_server_confirms_is_a_refusal():
|
||
|
|
refusal = ElectrumRpcError({"code": 1, "message": "bad-txns-inputs-missingorspent"})
|
||
|
|
servers = [Server("a", broadcast=refusal, knows=False), Server("b", knows=False), Server("c", knows=False)]
|
||
|
|
verdict = await network(*servers).broadcast("00ff", "tx")
|
||
|
|
assert verdict.outcome is Outcome.REFUSED and "missingorspent" in verdict.reason
|
||
|
|
|
||
|
|
|
||
|
|
async def test_a_refusal_the_others_cannot_confirm_is_only_uncertain():
|
||
|
|
refusal = ElectrumRpcError({"code": 1, "message": "bad-txns-inputs-missingorspent"})
|
||
|
|
servers = [Server("a", broadcast=refusal, knows=False), Server("b", knows=None), Server("c", knows=None)]
|
||
|
|
assert (await network(*servers).broadcast("00ff", "tx")).outcome is Outcome.UNCERTAIN
|
||
|
|
|
||
|
|
|
||
|
|
async def test_a_timeout_is_uncertain_and_never_a_refusal():
|
||
|
|
servers = [Server("a", broadcast=OSError("timeout")), Server("b"), Server("c")]
|
||
|
|
assert (await network(*servers).broadcast("00ff", "tx")).outcome is Outcome.UNCERTAIN
|