run.py raw

   1  #!/usr/bin/env python3
   2  """Sync a chain in the browser wallet through the live rendezvous relay.
   3  
   4  The page is pointed at a bare `wss://` relay, so the channel discovers the gateway from the
   5  relay's listings instead of being handed a gateway key. The sync is bounded on purpose: a
   6  mainnet chain without a checkpoint in its profile starts at genesis, and this run records how
   7  far it gets through the relay in a fixed window, then pauses. Every verified height stays in
   8  the store, so a later run resumes.
   9  
  10  usage: RELAY=wss://swaprelay.smesh.lol CHAIN=btc SECONDS=180 run.py
  11  """
  12  
  13  import asyncio
  14  import os
  15  import sys
  16  import time
  17  
  18  from playwright.async_api import async_playwright
  19  
  20  RELAY = os.environ.get("RELAY", "wss://swaprelay.smesh.lol")
  21  CHAIN = os.environ.get("CHAIN", "btc")
  22  SECONDS = int(os.environ.get("SECONDS", "180"))
  23  NETWORK = os.environ.get("NETWORK", "btc")
  24  PAGE = os.environ.get("PAGE", "http://127.0.0.1:8080/")
  25  PASSPHRASE = "relay-run-passphrase"
  26  
  27  HERE = os.path.dirname(os.path.abspath(__file__))
  28  PROFILE = os.path.join(HERE, "chromium-profile-" + CHAIN)
  29  LOG_PATH = os.path.join(HERE, "run-" + CHAIN + ".log")
  30  
  31  
  32  def say(line):
  33      print(line, flush=True)
  34      with open(LOG_PATH, "a") as handle:
  35          handle.write(str(line) + "\n")
  36  
  37  
  38  def card(page):
  39      return page.locator("#cards .card").first
  40  
  41  
  42  async def wait_text(page, needle, seconds):
  43      deadline = time.time() + seconds
  44      text = ""
  45      while time.time() < deadline:
  46          text = (await card(page).locator(".status").last.inner_text()).strip()
  47          if needle in text:
  48              return text
  49          await asyncio.sleep(1)
  50      return text
  51  
  52  
  53  async def main():
  54      url = PAGE + "?chains=" + CHAIN + "&network=" + NETWORK
  55      say("page " + url)
  56      say("relay " + RELAY + " (bare: the channel discovers the gateway)")
  57  
  58      async with async_playwright() as p:
  59          context = await p.chromium.launch_persistent_context(
  60              PROFILE, viewport={"width": 1280, "height": 1400})
  61          page = context.pages[0] if context.pages else await context.new_page()
  62          # The worker scripts are fetched by URL, and Chromium caches them across runs; a stale
  63          # channel wasm would make every fix look untested.
  64          cdp = await context.new_cdp_session(page)
  65          await cdp.send("Network.clearBrowserCache")
  66          page.on("console", lambda message: say("console " + message.type + ": " + message.text))
  67          page.on("pageerror", lambda error: say("pageerror: " + str(error)))
  68  
  69          await page.goto(url, wait_until="load", timeout=60000)
  70          await page.wait_for_timeout(500)
  71  
  72          inputs = card(page).locator("input")
  73          await inputs.nth(0).fill(RELAY)
  74          await inputs.nth(1).fill("")
  75          await page.screenshot(path=os.path.join(HERE, "01-relay-set.png"), full_page=True)
  76          say("gateway field: " + await inputs.nth(0).input_value())
  77          say("profile field: " + repr(await inputs.nth(1).input_value()) + " (built-in chain profile)")
  78          say("network profile: " + await card(page).locator(".status").first.inner_text())
  79  
  80          started = time.time()
  81          await page.get_by_role("button", name="Connect").click()
  82          await page.wait_for_function("document.getElementById('connect-status').textContent.includes('connected')", timeout=120000)
  83          say("connect (%.1fs): %s" % (time.time() - started, (await page.inner_text('#connect-status')).strip()))
  84          await page.screenshot(path=os.path.join(HERE, "02-connected.png"), full_page=True)
  85  
  86          state_text = (await card(page).locator(".status").first.inner_text()).strip()
  87  
  88          if "no wallet yet" in state_text:
  89              await card(page).locator("input[type=password]").fill(PASSPHRASE)
  90              await card(page).get_by_role("button", name="Create").click()
  91              words = card(page).locator(".words")
  92              await words.wait_for(timeout=60000)
  93              mnemonic = (await words.inner_text()).strip()
  94              await card(page).locator("textarea[placeholder^='type the words']").fill(mnemonic)
  95              await card(page).get_by_role("button", name="Confirm").click()
  96              state_text = (await card(page).locator(".status").first.inner_text()).strip()
  97  
  98          if "locked" in state_text and "no wallet yet" not in state_text:
  99              await card(page).locator("input[type=password]").fill(PASSPHRASE)
 100              await card(page).get_by_role("button", name="Unlock").click()
 101              await page.wait_for_timeout(1500)
 102              state_text = (await card(page).locator(".status").first.inner_text()).strip()
 103  
 104          say("wallet: " + " ".join(state_text.split()))
 105          await page.screenshot(path=os.path.join(HERE, "03-wallet.png"), full_page=True)
 106  
 107          started = time.time()
 108          await card(page).get_by_role("button", name="Sync").click()
 109          say("sync started; waiting up to %ds" % SECONDS)
 110  
 111          pause = card(page).get_by_role("button", name="Pause")
 112          finished = False
 113  
 114          deadline = time.time() + SECONDS
 115  
 116          while time.time() < deadline:
 117              if await pause.is_disabled():
 118                  finished = True
 119                  break
 120              await asyncio.sleep(3)
 121  
 122          if not finished:
 123              await pause.click()
 124              text = await wait_text(page, "paused", 120)
 125              say("pause (%.1fs): %s" % (time.time() - started, " ".join(text.split())))
 126          else:
 127              say("sync completed in %.1fs" % (time.time() - started))
 128  
 129          say("result: " + " ".join((await card(page).locator(".status").last.inner_text()).split()))
 130          await page.screenshot(path=os.path.join(HERE, "04-synced.png"), full_page=True)
 131  
 132          state = await page.evaluate("(async () => await window.wallet.status(%r))()" % CHAIN)
 133          say("status " + str(state))
 134          with open(os.path.join(HERE, "status-" + CHAIN + ".json"), "w") as handle:
 135              handle.write(str(state) + "\n")
 136  
 137          await context.close()
 138  
 139  
 140  asyncio.run(main())
 141