-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlightning_proc.py
160 lines (125 loc) · 4.35 KB
/
lightning_proc.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import networkx as nx
import numpy as np
import csv
import random
from scipy import stats
# returns network topology and transactions for Lightning
def setup():
# load nodes (very hacky way to non-parse the JSON ...)
nodes = []
with open('traces/lightning/allnodes.txt', 'r') as f:
for line in f:
if 'nodeid' in line:
nodeid = line.split()[1]
nodeid = nodeid.replace('"','').replace(',','')
nodes.append(nodeid)
# load channels (very hacky way to non-parse the JSON ...)
G = nx.DiGraph()
listC = []
with open('traces/lightning/channels.txt', 'r') as f:
for line in f:
if 'source' in line:
source = line.split()[1]
source = source.replace('"','').replace(',','')
elif 'destination' in line:
destination = line.split()[1]
destination = destination.replace('"','').replace(',','')
elif 'satoshis' in line:
capacity = line.split()[1]
capacity = capacity.replace(',','')
listC.append(float(capacity))
G.add_edge(
# from
int(nodes.index(source)),
# to
int(nodes.index(destination)),
# capacity according to dataset
capacity = float(capacity),
# transaction fees: randomly sampled
cost = random.random()*10
)
# while there are nodes with less than 2 neighbors (ie, who do not
# take routing decisions anyways), remove them
while True:
nodes_to_remove = []
for node_index in list(G.nodes()):
if len(list(G.neighbors(node_index))) < 2:
nodes_to_remove.append(node_index)
if len(nodes_to_remove) == 0:
break
for node_index in nodes_to_remove:
G.remove_node(node_index)
# "close gaps" in the enumeration of the nodes that was created
# by the above pruning procedure
mapping = dict(list(zip(G.nodes(), list(range(0, len(G))))))
G = nx.relabel_nodes(G, mapping, copy=False)
# increase transaction fees of 10% of the edges by a factor of 10
random.seed(1)
random_edges = random.sample(range(G.number_of_edges()), int(G.number_of_edges()*0.1))
for (i, e) in enumerate(G.edges()):
if i in random_edges:
G[e[0]][e[1]]['cost'] = G[e[0]][e[1]]['cost']*10
# print some stats
# (average channel cap and number of edges are not current anymore,
# after pruning of nodes has taken place ...)
print("number of nodes", len(G))
print('average channel cap', float(sum(listC))/len(listC))
print('num of edges', len(listC))
listC_sorted = np.sort(listC)
print('medium channel capacity', stats.scoreatpercentile(listC_sorted, 50))
# # collect some data for stats printout later
# listC = []
# for e in G.edges():
# listC.append(G[e[0]][e[1]]['capacity'])
# listC.append(G[e[1]][e[0]]['capacity'])
# listC_sorted = np.sort(listC)
# # print stats
# print("number of nodes", len(G))
# print('average channel cap', float(sum(listC))/len(listC))
# print('num of edges', len(listC))
# print('medium capacity', stats.scoreatpercentile(listC_sorted, 50))
# load the transaction values that have been obtained from
# an analysis of the Bitcoin blockchain
trans = []
with open('traces/lightning/BitcoinVal.txt', 'r') as f:
for line in f:
trans.append(float(line))
# print some stats
print('num of transactions', len(trans))
# return: graph of network, list of transaction values
return G, trans
# generates src/dst pairs using the Ripple dataset
# (the Lightning/Bitcoin dataset does not provide src/dst information)
# (node numbers from Ripple are mapped to Lightning using simple modulo ...)
def get_stpair(num_nodes):
st = []
with open('traces/ripple/ripple_val.csv', 'r') as f:
csv_reader = csv.reader(f, delimiter=',')
for row in csv_reader:
# only for positive payments
if float(row[2]) > 0:
# map Ripple nodes to Lightning nodes
src = int(row[0]) % num_nodes
dst = int(row[1]) % num_nodes
if src == dst:
continue
st.append((int(src), int(dst)))
return st
# generates payments based on src/dst pairs from Ripple dataset
# and transaction amounts from Bitcoin/Lightning dataset
def generate_payments(seed, nflows, trans, G):
random.seed(seed)
payments = []
src_dst = get_stpair(len(G))
while True:
# are we done yet?
if len(payments) >= nflows:
break
# sample random src/dst pair for which there exists a path
src, dst = random.choice(src_dst)
if not nx.has_path(G, src, dst):
continue
# sample transaction value
val = random.choice(trans)
payments.append((src, dst, val, 1, 0))
return payments