Skip to content

Commit 6a4b46a

Browse files
committed
import LN json
1 parent fac8d11 commit 6a4b46a

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

resources/charts/bitcoincore/charts/lnd/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ baseConfig: |
112112
trickledelay=1
113113
rpclisten=0.0.0.0:10009
114114
bitcoind.rpcuser=user
115+
protocol.wumbo-channels=1
115116
# zmq* and bitcoind.rpcpass are set in configmap.yaml
116117
117118
config: ""

src/warnet/graph.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import random
34
import sys
@@ -226,3 +227,91 @@ def create():
226227
fg="yellow",
227228
)
228229
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()}"

src/warnet/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .control import down, logs, run, snapshot, stop
66
from .dashboard import dashboard
77
from .deploy import deploy
8-
from .graph import create, graph
8+
from .graph import create, graph, import_network
99
from .image import image
1010
from .ln import ln
1111
from .project import init, new, setup
@@ -25,6 +25,7 @@ def cli():
2525
cli.add_command(down)
2626
cli.add_command(dashboard)
2727
cli.add_command(graph)
28+
cli.add_command(import_network)
2829
cli.add_command(image)
2930
cli.add_command(init)
3031
cli.add_command(logs)

0 commit comments

Comments
 (0)