|
| 1 | +"""Shared fixtures and helpers for the integration tests. |
| 2 | +
|
| 3 | +The integration tests drive a live ``StellarRpcServer`` over HTTP. This module centralises |
| 4 | +everything they build on: the server fixture, the JSON-RPC / HTTP plumbing, wat compilation, |
| 5 | +the JSON-shape predicates used to assert the stellar-rpc wire format, and the transaction |
| 6 | +submit / deploy helpers. Keeping these in one place stops each feature's test file from |
| 7 | +carrying its own drifting copy. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import json |
| 13 | +import re |
| 14 | +import socket |
| 15 | +import subprocess |
| 16 | +import threading |
| 17 | +import time |
| 18 | +import urllib.request |
| 19 | +from typing import TYPE_CHECKING, Any, NamedTuple |
| 20 | + |
| 21 | +import pytest |
| 22 | +from stellar_sdk import Account, Keypair, Network, TransactionBuilder |
| 23 | +from stellar_sdk.utils import sha256 |
| 24 | + |
| 25 | +from komet_node.server import StellarRpcServer |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from collections.abc import Callable, Iterator |
| 29 | + from pathlib import Path |
| 30 | + |
| 31 | + from stellar_sdk import xdr |
| 32 | + |
| 33 | +PASSPHRASE = Network.TESTNET_NETWORK_PASSPHRASE |
| 34 | + |
| 35 | + |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | +# HTTP / JSON-RPC plumbing |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | + |
| 40 | + |
| 41 | +def wat_to_wasm(wat_path: Path) -> bytes: |
| 42 | + proc_res = subprocess.run(['wat2wasm', str(wat_path), '--output=/dev/stdout'], check=True, capture_output=True) |
| 43 | + return proc_res.stdout |
| 44 | + |
| 45 | + |
| 46 | +def _find_free_port() -> int: |
| 47 | + with socket.socket() as s: |
| 48 | + s.bind(('', 0)) |
| 49 | + return s.getsockname()[1] |
| 50 | + |
| 51 | + |
| 52 | +def _wait_for_server(host: str, port: int, timeout: float = 10.0) -> None: |
| 53 | + deadline = time.time() + timeout |
| 54 | + while time.time() < deadline: |
| 55 | + try: |
| 56 | + with socket.create_connection((host, port), timeout=0.1): |
| 57 | + return |
| 58 | + except OSError: |
| 59 | + time.sleep(0.05) |
| 60 | + raise TimeoutError(f'Server did not start on {host}:{port}') |
| 61 | + |
| 62 | + |
| 63 | +def _rpc(port: int, method: str, params: dict[str, Any]) -> dict[str, Any]: |
| 64 | + body = json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': method, 'params': params}).encode() |
| 65 | + return _post(port, body) |
| 66 | + |
| 67 | + |
| 68 | +def _post(port: int, body: bytes) -> Any: |
| 69 | + return json.loads(_post_raw(port, body)) |
| 70 | + |
| 71 | + |
| 72 | +def _post_raw(port: int, body: bytes) -> bytes: |
| 73 | + req = urllib.request.Request( |
| 74 | + f'http://localhost:{port}', |
| 75 | + data=body, |
| 76 | + headers={'Content-Type': 'application/json'}, |
| 77 | + ) |
| 78 | + with urllib.request.urlopen(req) as resp: |
| 79 | + return resp.read() |
| 80 | + |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# Spec-shape predicates |
| 84 | +# |
| 85 | +# The official serialization rules come from the Go structs in stellar/go-stellar-sdk |
| 86 | +# protocols/rpc (what real stellar-rpc emits): ledger sequence numbers and protocolVersion |
| 87 | +# are JSON numbers; the close-time fields on the singular methods are int64 with Go's |
| 88 | +# `,string` encoding, i.e. JSON strings holding a decimal integer; hashes are 64 lowercase |
| 89 | +# hex characters. |
| 90 | +# --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +_HEX64_RE = re.compile(r'[0-9a-f]{64}') |
| 93 | +_INT_STRING_RE = re.compile(r'-?[0-9]+') |
| 94 | + |
| 95 | + |
| 96 | +def _is_number(value: Any) -> bool: |
| 97 | + """True for a JSON number decoded to int (bool is a distinct JSON type).""" |
| 98 | + return isinstance(value, int) and not isinstance(value, bool) |
| 99 | + |
| 100 | + |
| 101 | +def _is_int_string(value: Any) -> bool: |
| 102 | + """True for a JSON string holding a decimal integer (Go int64 `,string` encoding).""" |
| 103 | + return isinstance(value, str) and _INT_STRING_RE.fullmatch(value) is not None |
| 104 | + |
| 105 | + |
| 106 | +def _is_hex64(value: Any) -> bool: |
| 107 | + return isinstance(value, str) and _HEX64_RE.fullmatch(value) is not None |
| 108 | + |
| 109 | + |
| 110 | +# --------------------------------------------------------------------------- |
| 111 | +# Server fixture |
| 112 | +# --------------------------------------------------------------------------- |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture |
| 116 | +def server(tmp_path: Path) -> Iterator[StellarRpcServer]: |
| 117 | + port = _find_free_port() |
| 118 | + srv = StellarRpcServer( |
| 119 | + host='localhost', |
| 120 | + port=port, |
| 121 | + io_dir=tmp_path, |
| 122 | + network_passphrase=PASSPHRASE, |
| 123 | + ) |
| 124 | + thread = threading.Thread(target=srv.serve, daemon=True) |
| 125 | + thread.start() |
| 126 | + _wait_for_server('localhost', port) |
| 127 | + yield srv |
| 128 | + srv.shutdown() |
| 129 | + |
| 130 | + |
| 131 | +# --------------------------------------------------------------------------- |
| 132 | +# Transaction submit / deploy helpers |
| 133 | +# |
| 134 | +# Every lifecycle test builds on the same primitives: submit a signed transaction and |
| 135 | +# confirm it reaches SUCCESS, fund a fresh account, deploy a contract from a .wat file, and |
| 136 | +# invoke functions on it. They compose — ``deploy_and_get_invoker`` is just the three-step |
| 137 | +# account -> upload -> deploy setup wrapped around ``make_invoker``. |
| 138 | +# --------------------------------------------------------------------------- |
| 139 | + |
| 140 | + |
| 141 | +class Deployed(NamedTuple): |
| 142 | + """A deployed contract instance: its C-strkey address, wasm hash, and wasm bytecode.""" |
| 143 | + |
| 144 | + address: str |
| 145 | + wasm_hash: bytes |
| 146 | + wasm_bytecode: bytes |
| 147 | + |
| 148 | + |
| 149 | +def send_tx(server: StellarRpcServer, keypair: Keypair, tb: TransactionBuilder) -> tuple[str, dict[str, Any]]: |
| 150 | + """Sign, submit, and confirm a transaction. |
| 151 | +
|
| 152 | + Asserts the submission is accepted (PENDING) and the transaction executes to SUCCESS. |
| 153 | + Returns ``(tx_hash, getTransaction_result)``. |
| 154 | + """ |
| 155 | + env = tb.set_timeout(30).build() |
| 156 | + env.sign(keypair) |
| 157 | + res = _rpc(server.port(), 'sendTransaction', {'transaction': env.to_xdr()}) |
| 158 | + assert res['result']['status'] == 'PENDING' |
| 159 | + tx_hash = res['result']['hash'] |
| 160 | + get_res = _rpc(server.port(), 'getTransaction', {'hash': tx_hash})['result'] |
| 161 | + assert get_res['status'] == 'SUCCESS', f'Transaction failed: {get_res}' |
| 162 | + return tx_hash, get_res |
| 163 | + |
| 164 | + |
| 165 | +def fund_account(server: StellarRpcServer) -> tuple[Keypair, Account]: |
| 166 | + """Create a fresh, funded account; return its keypair and sequence-tracking ``Account``.""" |
| 167 | + keypair = Keypair.random() |
| 168 | + account = Account(keypair.public_key, sequence=0) |
| 169 | + tb = TransactionBuilder(account, PASSPHRASE).append_create_account_op(keypair.public_key, '1000') |
| 170 | + send_tx(server, keypair, tb) |
| 171 | + return keypair, account |
| 172 | + |
| 173 | + |
| 174 | +def deploy_contract(server: StellarRpcServer, keypair: Keypair, account: Account, wat_path: Path) -> Deployed: |
| 175 | + """Upload ``wat_path`` and deploy an instance from it, signed by ``keypair``/``account``. |
| 176 | +
|
| 177 | + The account must already exist. Submits two transactions (upload + create) and returns |
| 178 | + the deployed contract's address, wasm hash, and wasm bytecode. |
| 179 | + """ |
| 180 | + |
| 181 | + def builder() -> TransactionBuilder: |
| 182 | + return TransactionBuilder(account, PASSPHRASE) |
| 183 | + |
| 184 | + wasm_bytecode = wat_to_wasm(wat_path) |
| 185 | + send_tx(server, keypair, builder().append_upload_contract_wasm_op(wasm_bytecode)) |
| 186 | + wasm_hash = sha256(wasm_bytecode) |
| 187 | + salt = b'\x00' * 32 |
| 188 | + send_tx(server, keypair, builder().append_create_contract_op(wasm_hash, keypair.public_key, None, salt)) |
| 189 | + address = server.encoder.contract_address_from_deployer_address(keypair.public_key, salt) |
| 190 | + return Deployed(address=address, wasm_hash=wasm_hash, wasm_bytecode=wasm_bytecode) |
| 191 | + |
| 192 | + |
| 193 | +def make_invoker( |
| 194 | + server: StellarRpcServer, keypair: Keypair, account: Account, contract_address: str |
| 195 | +) -> Callable[..., str]: |
| 196 | + """Return an ``invoke(func, args=None) -> tx_hash`` callable that asserts each call SUCCEEDs.""" |
| 197 | + |
| 198 | + def invoke(func: str, args: list[xdr.SCVal] | None = None) -> str: |
| 199 | + tb = TransactionBuilder(account, PASSPHRASE).append_invoke_contract_function_op( |
| 200 | + contract_address, func, args or [] |
| 201 | + ) |
| 202 | + tx_hash, _ = send_tx(server, keypair, tb) |
| 203 | + return tx_hash |
| 204 | + |
| 205 | + return invoke |
| 206 | + |
| 207 | + |
| 208 | +def deploy_and_get_invoker(server: StellarRpcServer, wat_path: Path) -> Callable[..., str]: |
| 209 | + """Fund an account, upload ``wat_path``, and deploy a contract instance from it. |
| 210 | +
|
| 211 | + Returns an ``invoke(func, args=None)`` callable that runs a contract function and returns |
| 212 | + the executed transaction's hash, asserting the whole setup and each call reaches SUCCESS. |
| 213 | + """ |
| 214 | + keypair, account = fund_account(server) |
| 215 | + deployed = deploy_contract(server, keypair, account, wat_path) |
| 216 | + return make_invoker(server, keypair, account, deployed.address) |
0 commit comments