-
Notifications
You must be signed in to change notification settings - Fork 6
/
populate_graph.py
114 lines (106 loc) · 4.13 KB
/
populate_graph.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import networkx as nx
import random as rn
import csv
import string
import re
import ast
PATH = "data/snapshot-01-09-2021/"
# read node information and initialize the graph with the nodes
def populate_nodes(G, tech=-1):
node_list = []
map = dict()
with open(PATH + "nodes.csv", 'r') as csv_file:
csvreader = csv.reader(csv_file)
line = 0
for row in csvreader:
if (line != 0):
G.add_node(line - 1)
G.nodes[line - 1]["name"] = row[2]
G.nodes[line - 1]["pubadd"] = row[1]
# G.nodes[line - 1]["Tech"] = rn.randint(0, 2)
if (tech == -1):
if row[4] == 'c-lightning':
G.nodes[line - 1]["Tech"] = 1
elif row[4] == 'lnd':
G.nodes[line - 1]["Tech"] = 0
elif row[4] == 'eclair':
G.nodes[line - 1]["Tech"] = 2
else:
G.nodes[line - 1]["Tech"] = -1
elif tech == 0:
G.nodes[line - 1]["Tech"] = 0
elif tech == 1:
G.nodes[line - 1]["Tech"] = 1
elif tech == 2:
G.nodes[line - 1]["Tech"] = 2
map[row[1]] = line - 1
line += 1
return G, map
# Add channel information with age and total capacities
def populate_channels(G, map, cbr):
map1 = dict()
with open(PATH + "channels.csv", 'r') as csv_file:
csvreader = csv.reader(csv_file)
line = 0
for row in csvreader:
if (line != 0):
# print(line)
id = row[1]
nodes = ast.literal_eval(row[3])
u = map[nodes[0]]
v = map[nodes[1]]
G.add_edge(u, v)
G.add_edge(v, u)
opens = row[6]
# print(opens.split()[7])
s = opens.split()[7]
s = re.findall("\d+", s)
blk = int(s[0])
G.edges[u, v]["Age"] = blk
G.edges[u, v]["id"] = id
G.edges[v, u]["Age"] = blk
G.edges[v, u]["id"] = id
# We randomly distribute the capacity in both directions initially.
x = rn.uniform(0,int(row[2]))
G.edges[u, v]["Balance"] = x
G.edges[v, u]["Balance"] = int(row[2]) - x
map1[id] = [u, v]
G.edges[u, v]["marked"] = 0
G.edges[v, u]["marked"] = 0
#Last failure is used in lnd for calculating edge probability. We ignore this aspect for now
# G.edges[u, v]["LastFailure"] = 25
# G.edges[v, u]["LastFailure"] = 25
line += 1
return G, map1
# add fee and delay policies
def populate_policies(G, map):
with open(PATH + "policies.csv", 'r') as csv_file:
csvreader = csv.reader(csv_file)
line = 0
channels = []
for row in csvreader:
if (line != 0):
id = row[1]
if (id in map):
# if(id not in channels):
# channels.append(id)
nodes = map[id]
u = nodes[0]
v = nodes[1]
# print(int(row[2]))
if (int(row[2]) == 0):
G.edges[u, v]["BaseFee"] = int(row[3]) / 1000
G.edges[u, v]["FeeRate"] = int(row[4]) / 1000000
G.edges[u, v]["Delay"] = int(row[5])
# print(G.edges[u,v]["id"],G.edges[u,v]["Delay"])
G.edges[u, v]["marked"] = 1
elif (int(row[2]) == 1):
G.edges[v, u]["BaseFee"] = int(row[3]) / 1000
G.edges[v, u]["FeeRate"] = int(row[4]) / 1000000
G.edges[v, u]["Delay"] = int(row[5])
# print(G.edges[v, u]["id"],G.edges[v,u]["Delay"])
G.edges[v, u]["marked"] = 1
line += 1
# print(len(channels))
return G