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