B3: attach to the local regtest node and Fulcrum; smoke tests, helper script and fixtures
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c832fd3d2
commit
0092596b07
9 files changed
+273
-6
No files matched your search
@@ -0,0 +1,52 @@
|
||||
"""Integration fixtures against the local regtest (ADR-58, roadmap B3): the node of `plm-regtest` with Fulcrum.
|
||||
|
||||
Nothing here starts them. When the node or Fulcrum is not reachable, every test that asks for them is skipped, so
|
||||
the suite stays green on a machine without Docker. Bring them up with `docker compose up -d` in `plm-regtest`;
|
||||
`plm_engine/regtest.py` says how to point at another place.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from embit.bip32 import HDKey
|
||||
|
||||
from plm_engine import hd
|
||||
from plm_engine.electrum.client import ElectrumClient
|
||||
from plm_engine.network import REGTEST
|
||||
from plm_engine.regtest import RegtestError, RegtestNode, electrum_address, electrum_reachable
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def regtest_node() -> RegtestNode:
|
||||
try:
|
||||
node = RegtestNode.from_environment()
|
||||
node.info()
|
||||
except RegtestError as error:
|
||||
pytest.skip(f"no regtest node: {error}")
|
||||
return node
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def electrum_endpoint(regtest_node) -> tuple[str, int]:
|
||||
if not electrum_reachable():
|
||||
pytest.skip("no Fulcrum on the regtest Electrum port")
|
||||
return electrum_address()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def electrum(electrum_endpoint):
|
||||
"""A connected client that checks the regtest genesis at the handshake (ELC-30)."""
|
||||
host, port = electrum_endpoint
|
||||
client = ElectrumClient(host, port, use_ssl=False, genesis_hash=REGTEST.genesis_hash)
|
||||
await client.connect()
|
||||
try:
|
||||
yield client
|
||||
finally:
|
||||
await client.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fresh_address():
|
||||
"""A regtest address nobody has used: the account of a random master key, user index 0."""
|
||||
import os
|
||||
|
||||
master = HDKey.from_seed(os.urandom(32), version=REGTEST.embit["xprv"])
|
||||
return hd.user_address(hd.account_key(master, REGTEST), 0, REGTEST)
|
||||
@@ -0,0 +1,45 @@
|
||||
"""B3 — smoke of the local regtest: the node, Fulcrum and our profile agree (ADR-58, ELC-30)."""
|
||||
|
||||
import asyncio
|
||||
|
||||
from plm_engine.electrum.scripthash import address_to_scripthash
|
||||
from plm_engine.network import REGTEST
|
||||
|
||||
|
||||
def test_the_node_is_on_regtest_and_mines_a_block(regtest_node):
|
||||
before = regtest_node.height()
|
||||
hashes = regtest_node.mine(1)
|
||||
assert len(hashes) == 1 and regtest_node.height() == before + 1
|
||||
|
||||
|
||||
def test_the_node_accepts_our_regtest_addresses(regtest_node, fresh_address):
|
||||
assert fresh_address.startswith("rplm1")
|
||||
assert regtest_node.rpc("validateaddress", fresh_address)["isvalid"] is True
|
||||
|
||||
|
||||
def test_the_node_has_the_genesis_of_our_profile(regtest_node):
|
||||
assert regtest_node.rpc("getblockhash", 0) == REGTEST.genesis_hash
|
||||
|
||||
|
||||
async def test_fulcrum_follows_the_node_tip(regtest_node, electrum):
|
||||
"""Fulcrum indexes asynchronously: give it a moment after a new block."""
|
||||
regtest_node.mine(1)
|
||||
expected = regtest_node.height()
|
||||
for _ in range(40):
|
||||
tip = (await electrum.subscribe_headers())["height"]
|
||||
if tip == expected:
|
||||
break
|
||||
await asyncio.sleep(0.25)
|
||||
assert tip == expected
|
||||
|
||||
|
||||
async def test_funds_sent_to_an_address_are_seen_through_electrum(regtest_node, electrum, fresh_address):
|
||||
regtest_node.fund(fresh_address, 1.5)
|
||||
scripthash = address_to_scripthash(fresh_address)
|
||||
utxos = []
|
||||
for _ in range(40):
|
||||
utxos = await electrum.listunspent(scripthash)
|
||||
if utxos:
|
||||
break
|
||||
await asyncio.sleep(0.25)
|
||||
assert [u["value"] for u in utxos] == [150_000_000]
|
||||
Reference in new issue
Block a user