"""B7 — confirmations: TX-30, TX-31, TX-26, TX-08.""" import struct from datetime import timedelta import pytest from django.utils import timezone from embit.transaction import Transaction from plm_engine.electrum.client import ElectrumEndpoint from plm_engine.headers import header_meets_its_own_target from plm_engine.network import REGTEST from apps.lottery import bets, chain, confirmation from apps.lottery.electrum_chain import ElectrumChain from apps.lottery.models import AuditEntry, ChainTransaction, Movement, Participation, Utxo from tests.django.test_chain_bets import ScriptedChain, bet, make_round, placed_and_signed, player, world # noqa: F401 pytestmark = pytest.mark.django_db def pending(world): user, round_, tx = placed_and_signed(world) bets.broadcast_pending(ScriptedChain()) tx.refresh_from_db() return user, round_, tx # --- the write phase ------------------------------------------------------------------------------------------ def test_a_bet_is_confirmed_and_enters_the_draw(world): user, round_, tx = pending(world) assert confirmation.apply_confirmations({tx.pk: (tx.current_txid, 120)}) == 1 tx.refresh_from_db() bet_row = Participation.objects.get() assert tx.status == "confirmed" and tx.confirmed_at assert (bet_row.status, bet_row.bet_txid) == ("confirmed", tx.current_txid) and bet_row.confirmed_at assert Movement.objects.get(kind="bet").status == "confirmed" assert set(Utxo.objects.filter(reserved_by=tx).values_list("spent_txid", flat=True)) == {tx.current_txid} entry = AuditEntry.objects.get(action="tx_confirmed") assert entry.payload["height"] == 120 and entry.payload["adopted"] is False def test_an_earlier_version_that_confirms_is_adopted(world): """TX-31: the original won while a replacement was in flight.""" user, round_, tx = pending(world) original = tx.current_txid replacement = "ee" * 32 chain.register_version(tx, replacement, "00aa") # the row follows the replacement... Participation.objects.update(bet_txid=replacement) assert confirmation.apply_confirmations({tx.pk: (original, 130)}) == 1 # ...but the original is what the chain mined tx.refresh_from_db() assert tx.current_txid == original and tx.raw_hex and tx.raw_hex != "00aa" assert Participation.objects.get().bet_txid == original assert Utxo.objects.get(reserved_by=tx).spent_txid == original assert AuditEntry.objects.get(action="tx_confirmed").payload["adopted"] is True def test_a_row_that_is_no_longer_pending_is_left_alone(world): user, round_, tx = pending(world) ChainTransaction.objects.filter(pk=tx.pk).update(status="failed", failed_at=timezone.now(), failure_reason="x") assert confirmation.apply_confirmations({tx.pk: (tx.current_txid, 120)}) == 0 assert Participation.objects.get().status == "pending" def test_confirming_twice_changes_nothing_the_second_time(world): user, round_, tx = pending(world) confirmation.apply_confirmations({tx.pk: (tx.current_txid, 120)}) assert confirmation.apply_confirmations({tx.pk: (tx.current_txid, 120)}) == 0 assert AuditEntry.objects.filter(action="tx_confirmed").count() == 1 def test_the_plan_lists_pending_rows_with_every_version(world): user, round_, tx = pending(world) chain.register_version(tx, "ee" * 32, "00aa") (row,) = confirmation.plan() assert row[0] == tx.pk and row[1] == chain.assign_deposit_address(user).address and len(row[2]) == 2 def test_a_building_row_is_not_polled(world): user, round_, tx = placed_and_signed(world) # signed, never accepted: still building assert confirmation.plan() == [] # --- the network phase -------------------------------------------------------------------------------------------- def mined(root: bytes) -> str: for nonce in range(10_000): header = (struct.pack("