#!/usr/bin/env python3 """Differential: our transaction parser against both rig nodes. For each chain: take the tip block, pick a spending transaction, ask the node for its raw bytes and for its own decoding, ask the verifier's parser for the same facts, and compare txid, locktime, every input sequence and every output value and script. usage: differential.py [path-to-tinywallet-verify] """ import base64 import json import subprocess import sys import urllib.error import urllib.request VERIFY = sys.argv[1] if len(sys.argv) > 1 else "/tmp/tinywallet-verify" CHAINS = [ ("btc", 18443, "/home/mleku/.blake-swap-test/btc/regtest/.cookie"), ("blake", 19443, "/home/mleku/.blake-swap-test/blake/regtest/.cookie"), ] def rpc(port, cookie_path, method, *params): with open(cookie_path) as handle: cookie = handle.read().strip() body = json.dumps({"jsonrpc": "1.0", "id": 1, "method": method, "params": list(params)}).encode() request = urllib.request.Request( f"http://127.0.0.1:{port}/", data=body, headers={"content-type": "application/json", "authorization": "Basic " + base64.b64encode(cookie.encode()).decode()}) try: answer = json.loads(urllib.request.urlopen(request, timeout=20).read()) except urllib.error.HTTPError as err: raise RuntimeError(f"{method}: {err.read().decode()[:200]}") if answer.get("error"): raise RuntimeError(f"{method}: {answer['error']}") return answer["result"] def main(): failures = 0 for name, port, cookie in CHAINS: tip = rpc(port, cookie, "getbestblockhash") block = rpc(port, cookie, "getblock", tip, 2) txs = block["tx"] if isinstance(block["tx"], list) else [] if not txs: print(name, "tip block has no transactions to compare") failures += 1 continue chosen = next((t for t in txs if not t["vin"][0].get("coinbase")), txs[0]) txid = chosen["txid"] raw = rpc(port, cookie, "getrawtransaction", txid, 0, tip) ours = json.loads(subprocess.run([VERIFY, "-tx", raw], capture_output=True, text=True, check=True).stdout) theirs = rpc(port, cookie, "decoderawtransaction", raw) checks = { "txid": ours["txid"] == theirs["txid"], "locktime": ours["locktime"] == theirs["locktime"], "sequences": ours["sequences"] == [v["sequence"] for v in theirs["vin"]], "values": [o["value"] for o in ours["outputs"]] == [int(round(v["value"] * 1e8)) for v in theirs["vout"]], "scripts": [o["script"] for o in ours["outputs"]] == [v["scriptPubKey"]["hex"] for v in theirs["vout"]], } bad = [label for label, ok in checks.items() if not ok] print(f"{name}: block {block['height']} tx {txid[:16]}… outputs={len(ours['outputs'])} " f"inputs={len(ours['sequences'])} " + ("match" if not bad else "MISMATCH: " + ", ".join(bad))) failures += len(bad) print("failures:", failures) return 1 if failures else 0 sys.exit(main())