Skip to content

Commit 28e069c

Browse files
committed
import LN json
1 parent fac8d11 commit 28e069c

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-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: 105 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,107 @@ 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+
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+

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)