forked from Gridflare/lndpytools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadgraph.py
More file actions
59 lines (47 loc) · 1.89 KB
/
Copy pathloadgraph.py
File metadata and controls
59 lines (47 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import networkx as nx
# Multigraph is the most honest representation,
# but in practice we only care about the largest channels
# Some algorithms also prefer the simpler graph
def fromjson():
with open('describegraph.json') as f:
graphdata = json.load(f)
g = nx.Graph()
# Leave weight undefined for now, could be capacity or fee depending on algorithm
for node in graphdata['nodes']:
g.add_node(node['pub_key'],
pub_key=node['pub_key'],
last_update=node['last_update'],
alias=node['alias'],
color=node['color'],
addresses=node['addresses'],
capacity=0, # Increment while iterating channels
num_channels=0, # Increment while iterating channels
capacities=[], # For creating histograms
)
for edge in graphdata['edges']:
n1 = edge['node1_pub']
n2 = edge['node2_pub']
cap = int(edge['capacity'])
# track node capacity
# Note that this will not account for channels filtered later
for n in [n1, n2]:
g.nodes[n]['capacity'] += cap
g.nodes[n]['capacities'].append(cap)
g.nodes[n]['num_channels'] += 1
if g.has_edge(n1, n2):
# Need to decide to overwrite or skip
if g.edges[n1, n2]['capacity'] > cap:
# Keep the old edge if it is bigger
continue
g.add_edge(n1, n2,
channel_id=edge['channel_id'],
chan_point=edge['chan_point'],
last_update=edge['last_update'],
capacity=cap,
node1_pub=n1,
node2_pub=n2,
node1_policy=edge['node1_policy'],
node2_policy=edge['node2_policy'],
)
return g