"""B5 — a real deposit on the local regtest, end to end: fund an address derived from our public key, watch it wait, and see it credited at the configured threshold (DEP-20..DEP-23, ELC-31).""" import pytest from django.contrib.auth import get_user_model from apps.lottery import chain, deposits, keys from apps.lottery.balances import balance_of from apps.lottery.electrum_chain import ElectrumChain from apps.lottery.models import Deposit, LotteryConfig from plm_engine.electrum.client import ElectrumEndpoint from plm_engine.network import REGTEST pytestmark = pytest.mark.django_db PLM = 100_000_000 @pytest.fixture def deposit_address(settings, tmp_path): settings.PLM_ACCOUNT_XPUB = keys.generate_keys(tmp_path / "keys", settings.PLM_PROFILE) user = get_user_model().objects.create_user("giocatore", password="x" * 12) return user, chain.assign_deposit_address(user).address def scan(electrum_endpoint): host, port = electrum_endpoint # one Fulcrum: the development exception of ELC-14 applies, as ELECTRUM_ALLOW_NO_QUORUM would say return deposits.scan_deposits(ElectrumChain([ElectrumEndpoint(host, port, False)], REGTEST, allow_no_quorum=True)) def wait_for_fulcrum(regtest_node, electrum_endpoint): """Fulcrum indexes a moment after the node: scan until it has seen the tip.""" import time for _ in range(60): scan(electrum_endpoint) if Deposit.objects.exists(): return time.sleep(0.25) def test_a_deposit_waits_and_is_credited_at_the_configured_threshold(regtest_node, electrum_endpoint, deposit_address): user, address = deposit_address regtest_node.fund(address, 2.5) # mined once: one confirmation wait_for_fulcrum(regtest_node, electrum_endpoint) waiting = balance_of(user) assert (waiting.spendable_sats, waiting.incoming_sats) == (0, 250_000_000) for _ in range(4): # 2 .. 5 confirmations: still waiting regtest_node.mine(1) import time time.sleep(0.5) scan(electrum_endpoint) assert balance_of(user).spendable_sats == 0 regtest_node.mine(1) # the sixth for _ in range(60): scan(electrum_endpoint) if balance_of(user).spendable_sats: break import time time.sleep(0.25) assert balance_of(user).spendable_sats == 250_000_000 assert Deposit.objects.get().status == "credited" assert scan(electrum_endpoint) == {"detected": 0, "credited": 0, "orphaned": 0, "reinstated": 0} # once def test_the_operator_can_lower_the_threshold(regtest_node, electrum_endpoint, deposit_address): user, address = deposit_address config = LotteryConfig.get() config.deposit_confirmations = 2 config.save() regtest_node.fund(address, 1.0) regtest_node.mine(1) # two confirmations import time for _ in range(60): scan(electrum_endpoint) if balance_of(user).spendable_sats: break time.sleep(0.25) assert balance_of(user).spendable_sats == PLM