"""B7 — confirmation, replace-by-fee and the adoption of an earlier version, on the local regtest (TX-30..TX-32, TX-12, TX-19).""" import time from datetime import timedelta import pytest from django.utils import timezone from apps.lottery import bets, confirmation, rbf from apps.lottery.ledger import ChainLedger from apps.lottery.models import ChainTransaction, Movement, Participation, Utxo from plm_engine.regtest import RegtestError from tests.django.test_chain_bets import ScriptedChain from tests.integration.test_bet_regtest import BET, PLM, funded_player, network, open_round # noqa: F401 from uuid import uuid4 pytestmark = pytest.mark.django_db def placed(user, network): ChainLedger().place_bet(user, open_round().pk, request_id=uuid4()) assert bets.process_bets(network)["accepted"] == 1 return ChainTransaction.objects.get() def poll_until(network, tx, status="confirmed"): for _ in range(80): confirmation.poll_confirmations(network) tx.refresh_from_db() if tx.status == status: return time.sleep(0.25) def age(tx): ChainTransaction.objects.filter(pk=tx.pk).update(last_broadcast_at=timezone.now() - timedelta(hours=1)) def test_a_bet_is_confirmed_by_a_block_and_enters_the_draw(regtest_node, network, funded_player): user, _ = funded_player tx = placed(user, network) regtest_node.mine(1) poll_until(network, tx) assert tx.status == "confirmed" bet = Participation.objects.get() assert bet.status == "confirmed" and bet.confirmed_at and bet.bet_txid == tx.current_txid assert Movement.objects.get(kind="bet").status == "confirmed" assert Utxo.objects.get(reserved_by=tx).spent_txid == tx.current_txid def test_a_slow_bet_is_replaced_by_fee_and_the_replacement_confirms(regtest_node, network, funded_player): user, _ = funded_player tx = placed(user, network) first = tx.current_txid age(tx) assert rbf.bump_due(network) == 1 tx.refresh_from_db() assert tx.current_txid != first and tx.fee_rate_sat_vb == 2 assert regtest_node.rpc("getmempoolentry", tx.current_txid)["vsize"] > 0 # the node took the replacement... with pytest.raises(RegtestError): regtest_node.rpc("getmempoolentry", first) # ...and dropped the original regtest_node.mine(1) poll_until(network, tx) assert tx.status == "confirmed" and Participation.objects.get().bet_txid == tx.current_txid assert tx.versions.count() == 2 def test_an_earlier_version_that_confirms_is_adopted(regtest_node, network, funded_player): """The replacement never reached the node (the send was uncertain); the original is what the chain mines.""" user, _ = funded_player tx = placed(user, network) original = tx.current_txid age(tx) from apps.lottery.electrum_chain import Outcome, Verdict assert rbf.bump(tx.pk, ScriptedChain(Verdict(Outcome.UNCERTAIN, "timeout"))) is not None # the row follows version 2 tx.refresh_from_db() assert tx.current_txid != original regtest_node.mine(1) # version 1 is in the node's mempool and gets mined poll_until(network, tx) assert tx.status == "confirmed" and tx.current_txid == original # adopted (TX-31) assert Participation.objects.get().bet_txid == original assert Utxo.objects.get(reserved_by=tx).spent_txid == original