|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import random
|
3 | 4 | import sys
|
@@ -226,3 +227,91 @@ def create():
|
226 | 227 | fg="yellow",
|
227 | 228 | )
|
228 | 229 | return False
|
| 230 | + |
| 231 | + |
| 232 | +@click.command() |
| 233 | +@click.argument("graph_file_path", type=click.Path(exists=True, file_okay=True, dir_okay=False)) |
| 234 | +@click.argument("output_path", type=click.Path(exists=False, file_okay=False, dir_okay=True)) |
| 235 | +def import_network(graph_file_path: str, output_path: str): |
| 236 | + """Create a network from an imported lightning network graph JSON""" |
| 237 | + print(_import_network(graph_file_path, output_path)) |
| 238 | + |
| 239 | + |
| 240 | +def _import_network(graph_file_path, output_path): |
| 241 | + output_path = Path(output_path) |
| 242 | + graph_file_path = Path(graph_file_path).resolve() |
| 243 | + with open(graph_file_path) as graph_file: |
| 244 | + graph = json.loads(graph_file.read()) |
| 245 | + |
| 246 | + tanks = {} |
| 247 | + pk_to_tank = {} |
| 248 | + tank_to_pk = {} |
| 249 | + index = 0 |
| 250 | + for node in graph["nodes"]: |
| 251 | + tank = f"tank-{index:04d}" |
| 252 | + pk_to_tank[node["pub_key"]] = tank |
| 253 | + tank_to_pk[tank] = node["pub_key"] |
| 254 | + tanks[tank] = {"name": tank, "ln": {"lnd": True}, "lnd": {"channels": []}} |
| 255 | + index += 1 |
| 256 | + print(f"Imported {index} nodes") |
| 257 | + |
| 258 | + sorted_edges = sorted(graph["edges"], key=lambda x: int(x["channel_id"])) |
| 259 | + |
| 260 | + supported_policies = [ |
| 261 | + "base_fee_msat", |
| 262 | + "fee_rate_ppm", |
| 263 | + "time_lock_delta", |
| 264 | + "min_htlc_msat", |
| 265 | + "max_htlc_msat", |
| 266 | + ] |
| 267 | + |
| 268 | + for_fuck_sake_lnd_what_is_your_fucking_problem = {"min_htlc": "min_htlc_msat"} |
| 269 | + |
| 270 | + def import_policy(json_policy): |
| 271 | + for ugh in for_fuck_sake_lnd_what_is_your_fucking_problem: |
| 272 | + if ugh in json_policy: |
| 273 | + new_key = for_fuck_sake_lnd_what_is_your_fucking_problem[ugh] |
| 274 | + json_policy[new_key] = json_policy[ugh] |
| 275 | + return {key: int(json_policy[key]) for key in supported_policies if key in json_policy} |
| 276 | + |
| 277 | + # By default we start including channel open txs in block 300 |
| 278 | + block = 300 |
| 279 | + # Coinbase occupies the 0 position! |
| 280 | + index = 1 |
| 281 | + count = 0 |
| 282 | + for edge in sorted_edges: |
| 283 | + source = pk_to_tank[edge["node1_pub"]] |
| 284 | + amt = int(edge["capacity"]) // 2 |
| 285 | + channel = { |
| 286 | + "id": {"block": block, "index": index}, |
| 287 | + "target": pk_to_tank[edge["node2_pub"]] + "-ln", |
| 288 | + "local_amt": amt, |
| 289 | + "push_amt": amt - 1, |
| 290 | + "source_policy": import_policy(edge["node1_policy"]), |
| 291 | + "target_policy": import_policy(edge["node2_policy"]), |
| 292 | + } |
| 293 | + tanks[source]["lnd"]["channels"].append(channel) |
| 294 | + index += 1 |
| 295 | + if index > 1000: |
| 296 | + index = 1 |
| 297 | + block += 1 |
| 298 | + count += 1 |
| 299 | + |
| 300 | + print(f"Imported {count} channels") |
| 301 | + |
| 302 | + network = {"nodes": []} |
| 303 | + prev_node_name = list(tanks.keys())[-1] |
| 304 | + for name, obj in tanks.items(): |
| 305 | + obj["name"] = name |
| 306 | + obj["addnode"] = [prev_node_name] |
| 307 | + prev_node_name = name |
| 308 | + network["nodes"].append(obj) |
| 309 | + |
| 310 | + output_path.mkdir(parents=True, exist_ok=True) |
| 311 | + # This file must exist and must contain at least one line of valid yaml |
| 312 | + with open(output_path / "node-defaults.yaml", "w") as f: |
| 313 | + f.write(f"imported_from: {graph_file_path}\n") |
| 314 | + # Here's the good stuff |
| 315 | + with open(output_path / "network.yaml", "w") as f: |
| 316 | + f.write(yaml.dump(network, sort_keys=False)) |
| 317 | + return f"Network created in {output_path.resolve()}" |
0 commit comments