|
1 |
| -from hooks_api import post_status, pre_status |
| 1 | +import json |
| 2 | +import logging |
| 3 | +import random |
| 4 | +from pathlib import Path |
| 5 | +from subprocess import run |
| 6 | +from time import sleep |
2 | 7 |
|
| 8 | +from warnet.hooks import _get_plugin_directory as get_plugin_directory |
| 9 | +from warnet.k8s import get_pods_with_label |
| 10 | +from warnet.process import run_command |
| 11 | +from warnet.status import _get_tank_status as network_status |
3 | 12 |
|
4 |
| -@pre_status |
5 |
| -def print_something_first(): |
6 |
| - print("The simln plugin is enabled.") |
| 13 | +log = logging.getLogger("simln") |
| 14 | +log.setLevel(logging.DEBUG) |
| 15 | +console_handler = logging.StreamHandler() |
| 16 | +console_handler.setLevel(logging.DEBUG) |
| 17 | +formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 18 | +console_handler.setFormatter(formatter) |
| 19 | +log.addHandler(console_handler) |
7 | 20 |
|
| 21 | +lightning_selector = "mission=lightning" |
8 | 22 |
|
9 |
| -@post_status |
10 |
| -def print_something_afterwards(): |
11 |
| - print("The simln plugin executes after `status` has run.") |
12 | 23 |
|
| 24 | +# @post_deploy |
| 25 | +def deploy_helm(): |
| 26 | + print("SimLN Plugin ⚡") |
| 27 | + plugin_dir = get_plugin_directory() |
| 28 | + print(f"DIR: {plugin_dir}") |
| 29 | + command = f"helm upgrade --install simln {plugin_dir}/simln/charts/simln" |
| 30 | + helm_result = run_command(command) |
| 31 | + print(helm_result) |
13 | 32 |
|
14 |
| -def run(): |
15 |
| - print("Running the simln plugin") |
| 33 | + |
| 34 | +def run_simln(): |
| 35 | + init_network() |
| 36 | + fund_wallets() |
| 37 | + wait_for_predicate(everyone_has_a_host) |
| 38 | + log.info(warnet("bitcoin rpc tank-0000 -generate 7")) |
| 39 | + # warnet("ln open-all-channels") |
| 40 | + manual_open_channels() |
| 41 | + log.info(warnet("bitcoin rpc tank-0000 -generate 7")) |
| 42 | + wait_for_gossip_sync(2) |
| 43 | + log.info("done waiting") |
| 44 | + pods = get_pods_with_label(lightning_selector) |
| 45 | + pod_a = pods[0].metadata.name |
| 46 | + pod_b = pods[1].metadata.name |
| 47 | + sample_activity = [ |
| 48 | + {"source": pod_a, "destination": pod_b, "interval_secs": 1, "amount_msat": 2000} |
| 49 | + ] |
| 50 | + log.info(f"Activity: {sample_activity}") |
| 51 | + generate_activity(sample_activity) |
| 52 | + log.info("Sent command. Done.") |
| 53 | + |
| 54 | + |
| 55 | +def generate_activity(activity: list[dict]): |
| 56 | + random_digits = "".join(random.choices("0123456789", k=10)) |
| 57 | + plugin_dir = get_plugin_directory() |
| 58 | + generate_nodes_file(activity, plugin_dir / Path("simln/charts/simln/files/sim.json")) |
| 59 | + command = f"helm upgrade --install simln-{random_digits} {plugin_dir}/simln/charts/simln" |
| 60 | + log.info(f"generate activity: {command}") |
| 61 | + run_command(command) |
| 62 | + |
| 63 | + |
| 64 | +def init_network(): |
| 65 | + log.info("Initializing network") |
| 66 | + wait_for_all_tanks_status(target="running") |
| 67 | + |
| 68 | + warnet("bitcoin rpc tank-0000 createwallet miner") |
| 69 | + warnet("bitcoin rpc tank-0000 -generate 110") |
| 70 | + wait_for_predicate(lambda: int(warnet("bitcoin rpc tank-0000 getblockcount")) > 100) |
| 71 | + |
| 72 | + def wait_for_all_ln_rpc(): |
| 73 | + lns = get_pods_with_label(lightning_selector) |
| 74 | + for v1_pod in lns: |
| 75 | + ln = v1_pod.metadata.name |
| 76 | + try: |
| 77 | + warnet(f"ln rpc {ln} getinfo") |
| 78 | + except Exception: |
| 79 | + log.info(f"LN node {ln} not ready for rpc yet") |
| 80 | + return False |
| 81 | + return True |
| 82 | + |
| 83 | + wait_for_predicate(wait_for_all_ln_rpc) |
| 84 | + |
| 85 | + |
| 86 | +def fund_wallets(): |
| 87 | + log.info("Funding wallets") |
| 88 | + outputs = "" |
| 89 | + lns = get_pods_with_label(lightning_selector) |
| 90 | + for v1_pod in lns: |
| 91 | + lnd = v1_pod.metadata.name |
| 92 | + addr = json.loads(warnet(f"ln rpc {lnd} newaddress p2wkh"))["address"] |
| 93 | + outputs += f',"{addr}":10' |
| 94 | + # trim first comma |
| 95 | + outputs = outputs[1:] |
| 96 | + log.info(warnet("bitcoin rpc tank-0000 sendmany '' '{" + outputs + "}'")) |
| 97 | + log.info(warnet("bitcoin rpc tank-0000 -generate 1")) |
| 98 | + |
| 99 | + |
| 100 | +def everyone_has_a_host() -> bool: |
| 101 | + pods = get_pods_with_label(lightning_selector) |
| 102 | + host_havers = 0 |
| 103 | + for pod in pods: |
| 104 | + name = pod.metadata.name |
| 105 | + result = warnet(f"ln host {name}") |
| 106 | + if len(result) > 1: |
| 107 | + host_havers += 1 |
| 108 | + return host_havers == len(pods) |
| 109 | + |
| 110 | + |
| 111 | +def wait_for_predicate(predicate, timeout=5 * 60, interval=5): |
| 112 | + log.info( |
| 113 | + f"Waiting for predicate ({predicate.__name__}) with timeout {timeout}s and interval {interval}s" |
| 114 | + ) |
| 115 | + while timeout > 0: |
| 116 | + try: |
| 117 | + if predicate(): |
| 118 | + return |
| 119 | + except Exception: |
| 120 | + pass |
| 121 | + sleep(interval) |
| 122 | + timeout -= interval |
| 123 | + import inspect |
| 124 | + |
| 125 | + raise Exception( |
| 126 | + f"Timed out waiting for Truth from predicate: {inspect.getsource(predicate).strip()}" |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def wait_for_all_tanks_status(target="running", timeout=20 * 60, interval=5): |
| 131 | + """Poll the warnet server for container status |
| 132 | + Block until all tanks are running |
| 133 | + """ |
| 134 | + |
| 135 | + def check_status(): |
| 136 | + tanks = network_status() |
| 137 | + stats = {"total": 0} |
| 138 | + # "Probably" means all tanks are stopped and deleted |
| 139 | + if len(tanks) == 0: |
| 140 | + return True |
| 141 | + for tank in tanks: |
| 142 | + status = tank["status"] |
| 143 | + stats["total"] += 1 |
| 144 | + stats[status] = stats.get(status, 0) + 1 |
| 145 | + log.info(f"Waiting for all tanks to reach '{target}': {stats}") |
| 146 | + return target in stats and stats[target] == stats["total"] |
| 147 | + |
| 148 | + wait_for_predicate(check_status, timeout, interval) |
| 149 | + |
| 150 | + |
| 151 | +def wait_for_gossip_sync(expected): |
| 152 | + log.info(f"Waiting for sync (expecting {expected})...") |
| 153 | + current = 0 |
| 154 | + while current < expected: |
| 155 | + current = 0 |
| 156 | + pods = get_pods_with_label(lightning_selector) |
| 157 | + for v1_pod in pods: |
| 158 | + node = v1_pod.metadata.name |
| 159 | + chs = json.loads(run_command(f"warnet ln rpc {node} describegraph"))["edges"] |
| 160 | + log.info(f"{node}: {len(chs)} channels") |
| 161 | + current += len(chs) |
| 162 | + sleep(1) |
| 163 | + log.info("Synced") |
| 164 | + |
| 165 | + |
| 166 | +def warnet(cmd): |
| 167 | + log.info(f"Executing warnet command: {cmd}") |
| 168 | + command = ["warnet"] + cmd.split() |
| 169 | + proc = run(command, capture_output=True) |
| 170 | + if proc.stderr: |
| 171 | + raise Exception(proc.stderr.decode().strip()) |
| 172 | + return proc.stdout.decode() |
| 173 | + |
| 174 | + |
| 175 | +def generate_nodes_file(activity, output_file: Path = Path("nodes.json")): |
| 176 | + nodes = [] |
| 177 | + |
| 178 | + for i in get_pods_with_label(lightning_selector): |
| 179 | + name = i.metadata.name |
| 180 | + node = { |
| 181 | + "id": name, |
| 182 | + "address": f"https://{name}:10009", |
| 183 | + "macaroon": "/config/admin.macaroon", |
| 184 | + "cert": "/config/tls.cert", |
| 185 | + } |
| 186 | + nodes.append(node) |
| 187 | + |
| 188 | + data = {"nodes": nodes, "activity": activity} |
| 189 | + |
| 190 | + with open(output_file, "w") as f: |
| 191 | + json.dump(data, f, indent=2) |
| 192 | + |
| 193 | + |
| 194 | +def manual_open_channels(): |
| 195 | + def wait_for_two_txs(): |
| 196 | + wait_for_predicate( |
| 197 | + lambda: json.loads(warnet("bitcoin rpc tank-0000 getmempoolinfo"))["size"] == 2 |
| 198 | + ) |
| 199 | + |
| 200 | + # 0 -> 1 -> 2 |
| 201 | + pk1 = warnet("ln pubkey tank-0001-ln") |
| 202 | + pk2 = warnet("ln pubkey tank-0002-ln") |
| 203 | + log.info(f"pk1: {pk1}") |
| 204 | + log.info(f"pk2: {pk2}") |
| 205 | + |
| 206 | + host1 = "" |
| 207 | + host2 = "" |
| 208 | + |
| 209 | + while not host1 or not host2: |
| 210 | + if not host1: |
| 211 | + host1 = warnet("ln host tank-0001-ln") |
| 212 | + if not host2: |
| 213 | + host2 = warnet("ln host tank-0002-ln") |
| 214 | + sleep(1) |
| 215 | + |
| 216 | + print( |
| 217 | + warnet( |
| 218 | + f"ln rpc tank-0000-ln openchannel --node_key {pk1} --local_amt 100000 --connect {host1}" |
| 219 | + ) |
| 220 | + ) |
| 221 | + print( |
| 222 | + warnet( |
| 223 | + f"ln rpc tank-0001-ln openchannel --node_key {pk2} --local_amt 100000 --connect {host2}" |
| 224 | + ) |
| 225 | + ) |
| 226 | + |
| 227 | + wait_for_two_txs() |
| 228 | + |
| 229 | + warnet("bitcoin rpc tank-0000 -generate 10") |
0 commit comments