| … | … | @@ -1,11 +1,18 @@ |
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | """Gate D1/D4: a frame crosses between two swarmd daemons over a real channel. |
| 3 | 3 | |
| 4 | | python3 -B test/test_channel_e2e.py [--bootstrap host:port] |
| 4 | python3 -B test/test_channel_e2e.py [--bootstrap host:port] [--seconds N] |
| 5 | 5 | |
| 6 | | Two daemons join one topic, each local client opens the SAME protocol channel, |
| 7 | | and a frame sent by one arrives at the other. That is the whole product in one |
| 8 | | assertion: everything before it is plumbing. |
| 6 | Two daemons join one topic, a swarmd-probe client on each opens the SAME |
| 7 | protocol channel, and a frame sent by one arrives at the other. That is the |
| 8 | whole product in one assertion: everything before it is plumbing. |
| 9 | |
| 10 | ⛔ NO WIRE IS WRITTEN HERE. The first version hand-rolled compact-encoding in |
| 11 | Python and the daemon answered "undecodable message" to every frame — the test |
| 12 | was wrong, not the daemon. Hand-writing the wire in a client is exactly what |
| 13 | generating it was supposed to stop, and a test client is still a client. So this |
| 14 | drives tools/swarmd-probe, which uses the generated encoders, and reads its |
| 15 | words. The only thing Python parses here is stdout. |
| 9 | 16 | |
| 10 | 17 | ⛔ IT ASSERTS ITS OWN BASELINE. If the two daemons never see each other the |
| 11 | 18 | message result says nothing, so a swarm that never meshes exits INCONCLUSIVE |
| … | … | @@ -13,146 +20,147 @@ rather than FAIL — the mistake the first long-lived gate made, reporting "the |
| 13 | 20 | responder has gone silent" from a run in which nothing had ever connected. |
| 14 | 21 | """ |
| 15 | 22 | |
| 16 | | import os, socket, struct, subprocess, sys, tempfile, time |
| 23 | import os, re, subprocess, sys, tempfile, time |
| 17 | 24 | |
| 18 | 25 | HERE = os.path.dirname(os.path.realpath(__file__)) |
| 19 | | SWARMD = os.path.join(os.path.dirname(HERE), "swarmd") |
| 26 | ROOT = os.path.dirname(HERE) |
| 27 | SWARMD = os.path.join(ROOT, "swarmd") |
| 28 | PROBE = os.path.join(ROOT, "tools", "swarmd-probe") |
| 20 | 29 | EXIT_PASS, EXIT_FAIL, EXIT_INCONCLUSIVE = 0, 1, 2 |
| 21 | 30 | |
| 22 | | # Command ids are the append-only ids from schema/hrpc.json. |
| 23 | | import json |
| 24 | | HRPC = json.load(open(os.path.join(os.path.dirname(HERE), "schema", "hrpc.json"))) |
| 25 | | CMD = {h["name"].split("/", 1)[1]: h["id"] for h in HRPC["schema"]} |
| 26 | | |
| 27 | | |
| 28 | | def frame(payload): |
| 29 | | return struct.pack("<I", len(payload)) + payload |
| 31 | TOPIC = "5a" * 32 |
| 32 | PROTOCOL = "swarmd-gate/v1" |
| 33 | PAYLOAD = "hello-over-the-swarm" |
| 30 | 34 | |
| 35 | # The sender goes first ON PURPOSE. It re-offers the frame until the daemon |
| 36 | # accepts it, so starting it before the receiver exists exercises the refusal |
| 37 | # path as well as the crossing — the case the fire-once-on-a-timer probe passed |
| 38 | # by luck on a fast testnet. |
| 39 | RECEIVER_DELAY = 5 |
| 31 | 40 | |
| 32 | | def uint(n): |
| 33 | | # compact-encoding uint |
| 34 | | if n < 0xfc: return bytes([n]) |
| 35 | | if n <= 0xffff: return b"\xfd" + struct.pack("<H", n) |
| 36 | | if n <= 0xffffffff: return b"\xfe" + struct.pack("<I", n) |
| 37 | | return b"\xff" + struct.pack("<Q", n) |
| 38 | 41 | |
| 42 | def arg(name, default=None): |
| 43 | return sys.argv[sys.argv.index(name) + 1] if name in sys.argv else default |
| 39 | 44 | |
| 40 | | def rpc_request(cmd_id, req_id, body): |
| 41 | | # rpc_message: type=1 (request), id, command, stream=0, then data |
| 42 | | return uint(1) + uint(req_id) + uint(cmd_id) + uint(0) + uint(len(body)) + body |
| 43 | 45 | |
| 44 | | |
| 45 | | def spawn(sock, bootstrap, log): |
| 46 | | argv = [SWARMD, "--socket", sock] |
| 47 | | if bootstrap: |
| 48 | | argv += ["--bootstrap", bootstrap] |
| 49 | | return subprocess.Popen(argv, stdout=log, stderr=subprocess.STDOUT) |
| 46 | def read(path): |
| 47 | try: |
| 48 | with open(path, "rb") as fh: |
| 49 | return fh.read().decode("utf-8", "replace") |
| 50 | except OSError: |
| 51 | return "" |
| 50 | 52 | |
| 51 | 53 | |
| 52 | | def wait_log(path, needle, timeout, proc): |
| 54 | def wait_for(path, needle, timeout, proc): |
| 53 | 55 | end = time.time() + timeout |
| 54 | 56 | while time.time() < end: |
| 55 | | if proc.poll() is not None: |
| 57 | if proc is not None and proc.poll() is not None: |
| 56 | 58 | return False |
| 57 | | try: |
| 58 | | with open(path, "rb") as fh: |
| 59 | | if needle in fh.read(): |
| 60 | | return True |
| 61 | | except OSError: |
| 62 | | pass |
| 59 | if needle in read(path): |
| 60 | return True |
| 63 | 61 | time.sleep(0.3) |
| 64 | 62 | return False |
| 65 | 63 | |
| 66 | 64 | |
| 65 | def peers_seen(text): |
| 66 | """The highest peer count this daemon ever reported.""" |
| 67 | return max([int(m) for m in re.findall(r"peers=(\d+)", text)] or [0]) |
| 68 | |
| 69 | |
| 67 | 70 | def main(): |
| 68 | | bootstrap = None |
| 69 | | if "--bootstrap" in sys.argv: |
| 70 | | bootstrap = sys.argv[sys.argv.index("--bootstrap") + 1] |
| 71 | bootstrap = arg("--bootstrap") |
| 72 | window = int(arg("--seconds", "60" if bootstrap else "180")) |
| 73 | |
| 74 | for path in (SWARMD, PROBE): |
| 75 | if not os.path.isfile(path): |
| 76 | print("missing %s — run ./build.sh" % path) |
| 77 | return EXIT_INCONCLUSIVE |
| 71 | 78 | |
| 72 | | if not os.path.isfile(SWARMD): |
| 73 | | print("missing %s — run ./build.sh" % SWARMD) |
| 74 | | return EXIT_INCONCLUSIVE |
| 79 | where = "bootstrap %s" % bootstrap if bootstrap else "the PUBLIC DHT" |
| 80 | print("two daemons on %s, %ds window" % (where, window)) |
| 75 | 81 | |
| 76 | 82 | tmp = tempfile.mkdtemp(prefix="swarmd-chan-") |
| 77 | | topic = bytes([0x5a] * 32) |
| 78 | | protocol = b"swarmd-gate/v1" |
| 83 | procs, socks, logs = [], [], {} |
| 84 | |
| 85 | def spawn(argv, logpath): |
| 86 | fh = open(logpath, "wb") |
| 87 | p = subprocess.Popen(argv, stdout=fh, stderr=subprocess.STDOUT) |
| 88 | procs.append(p) |
| 89 | return p |
| 79 | 90 | |
| 80 | | procs, socks, logs = [], [], [] |
| 81 | 91 | try: |
| 92 | daemons = {} |
| 82 | 93 | for tag in ("a", "b"): |
| 83 | 94 | sp = os.path.join(tmp, tag + ".sock") |
| 84 | | lp = os.path.join(tmp, tag + ".log") |
| 85 | | log = open(lp, "wb") |
| 86 | | p = spawn(sp, bootstrap, log) |
| 87 | | procs.append(p); socks.append(sp); logs.append(lp) |
| 88 | | |
| 89 | | for i, lp in enumerate(logs): |
| 90 | | if not wait_log(lp, b"dht=up", 60, procs[i]): |
| 91 | | print("\nINCONCLUSIVE: daemon %d never reached dht=up" % i) |
| 95 | logs[tag] = os.path.join(tmp, tag + ".log") |
| 96 | argv = [SWARMD, "--socket", sp] |
| 97 | if bootstrap: |
| 98 | argv += ["--bootstrap", bootstrap] |
| 99 | daemons[tag] = spawn(argv, logs[tag]) |
| 100 | socks.append(sp) |
| 101 | |
| 102 | # ---- baseline 1: both daemons are actually on a DHT ---- |
| 103 | for tag in ("a", "b"): |
| 104 | if not wait_for(logs[tag], "dht=up", 90, daemons[tag]): |
| 105 | print("\nINCONCLUSIVE: daemon %s never reached dht=up." % tag) |
| 92 | 106 | return EXIT_INCONCLUSIVE |
| 93 | 107 | print(" both daemons are on the DHT") |
| 94 | 108 | |
| 95 | | conns = [] |
| 96 | | for sp in socks: |
| 97 | | c = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 98 | | c.connect(sp) |
| 99 | | conns.append(c) |
| 100 | | |
| 101 | | # join the topic, open the channel |
| 102 | | for c in conns: |
| 103 | | body = topic + b"\x01\x01" # fixed32 + bool + bool |
| 104 | | c.sendall(frame(rpc_request(CMD["swarm-join"], 1, body))) |
| 105 | | ob = uint(len(protocol)) + protocol + uint(0) + uint(2) # protocol, id(empty), slots |
| 106 | | c.sendall(frame(rpc_request(CMD["channel-open"], 2, ob))) |
| 107 | | print(" joined and opened the channel on both") |
| 108 | | |
| 109 | | # Give the swarm time to mesh and pair the channel. |
| 110 | | deadline = time.time() + 90 |
| 111 | | meshed = False |
| 112 | | while time.time() < deadline and not meshed: |
| 113 | | for lp in logs: |
| 114 | | with open(lp, "rb") as fh: |
| 115 | | if b"peers=1" in fh.read(): |
| 116 | | meshed = True |
| 117 | | time.sleep(1) |
| 109 | def probe(tag, sock_tag, extra, seconds): |
| 110 | logs[tag] = os.path.join(tmp, tag + ".log") |
| 111 | argv = [PROBE, "--socket", os.path.join(tmp, sock_tag + ".sock"), |
| 112 | "--topic-hex", TOPIC, "--protocol", PROTOCOL, |
| 113 | "--seconds", str(seconds)] + extra |
| 114 | return spawn(argv, logs[tag]) |
| 115 | |
| 116 | sender = probe("send", "a", ["--send", PAYLOAD], window) |
| 117 | time.sleep(RECEIVER_DELAY) |
| 118 | receiver = probe("recv", "b", [], window - RECEIVER_DELAY) |
| 119 | print(" probes attached; waiting up to %ds" % window) |
| 118 | 120 | |
| 119 | | if not meshed: |
| 120 | | print("\nINCONCLUSIVE: the two daemons never met, so a message result " |
| 121 | | "would say nothing about the channel.") |
| 121 | end = time.time() + window + 15 |
| 122 | while time.time() < end and receiver.poll() is None: |
| 123 | time.sleep(1) |
| 124 | for p in (sender, receiver): |
| 125 | if p.poll() is None: |
| 126 | p.terminate() |
| 127 | try: p.wait(timeout=10) |
| 128 | except subprocess.TimeoutExpired: p.kill() |
| 129 | |
| 130 | sender_out, receiver_out = read(logs["send"]), read(logs["recv"]) |
| 131 | met = min(peers_seen(read(logs["a"])), peers_seen(read(logs["b"]))) |
| 132 | accepted = "accepted after" in sender_out |
| 133 | |
| 134 | print(" daemons met: %s | frame accepted by the daemon: %s" |
| 135 | % ("yes" if met else "no", "yes" if accepted else "no")) |
| 136 | |
| 137 | # ---- baseline 2: if they never met, nothing downstream means anything ---- |
| 138 | if not met: |
| 139 | print("\nINCONCLUSIVE: the two daemons never saw each other, so the " |
| 140 | "message result says nothing about the channel.") |
| 141 | print("(logs: %s)" % tmp) |
| 122 | 142 | return EXIT_INCONCLUSIVE |
| 123 | | print(" the daemons meshed") |
| 124 | | |
| 125 | | # a -> b |
| 126 | | payload = b"hello-over-the-swarm" |
| 127 | | sb = uint(1) + uint(0) + uint(0) + uint(len(payload)) + payload # channel, slot, target, bytes |
| 128 | | conns[0].sendall(frame(rpc_request(CMD["send"], 3, sb))) |
| 129 | | |
| 130 | | conns[1].settimeout(30) |
| 131 | | got = b"" |
| 132 | | end = time.time() + 30 |
| 133 | | while time.time() < end: |
| 134 | | try: |
| 135 | | chunk = conns[1].recv(65536) |
| 136 | | except socket.timeout: |
| 137 | | break |
| 138 | | if not chunk: |
| 139 | | break |
| 140 | | got += chunk |
| 141 | | if payload in got: |
| 142 | | break |
| 143 | | |
| 144 | | if payload in got: |
| 143 | |
| 144 | if ("RECEIVED: " + PAYLOAD) in receiver_out: |
| 145 | 145 | print("\nPASS: the frame crossed the swarm and arrived on the other daemon.") |
| 146 | 146 | return EXIT_PASS |
| 147 | | print("\nFAIL: the daemons meshed but the frame did not arrive.") |
| 147 | |
| 148 | if not accepted: |
| 149 | print("\nFAIL: the daemons met but neither channel ever opened — the " |
| 150 | "daemon refused the frame for the whole window.") |
| 151 | else: |
| 152 | print("\nFAIL: the daemon accepted the frame and it never arrived.") |
| 153 | print("--- sender ---\n%s--- receiver ---\n%s" % (sender_out, receiver_out)) |
| 148 | 154 | return EXIT_FAIL |
| 149 | 155 | finally: |
| 150 | | for c in socks: |
| 151 | | pass |
| 152 | 156 | for p in procs: |
| 153 | | p.terminate() |
| 154 | | try: p.wait(timeout=5) |
| 155 | | except subprocess.TimeoutExpired: p.kill() |
| 157 | if p.poll() is None: |
| 158 | p.terminate() |
| 159 | try: p.wait(timeout=5) |
| 160 | except subprocess.TimeoutExpired: p.kill() |
| 161 | for sp in socks: |
| 162 | try: os.unlink(sp) |
| 163 | except OSError: pass |
| 156 | 164 | print("(logs: %s)" % tmp) |
| 157 | 165 | |
| 158 | 166 | |