-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-eval-success-malicious.py
157 lines (136 loc) · 4.72 KB
/
plot-eval-success-malicious.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
import json
import matplotlib
import math
import numpy as np
import os
from scipy import interpolate
from scipy.signal import savgol_filter
import sys
import time
import re
matplotlib.use('PDF')
import matplotlib.pyplot as plt
lineTypes = {
'basic': '^-k',
'etora': 'x-k',
'mdart': '|-k',
'terp': 'o-k'
}
algNames = {
'basic': 'BASIC',
'etora': 'E-TORA',
'mdart': 'M-DART',
'terp': 'TERP'
}
plotAll = (len(sys.argv) > 1 and sys.argv[1] == 'all')
plotPath = os.path.abspath('eval-success-malicious.pdf')
if os.path.isfile(plotPath) and plotAll == False:
print(plotPath + ' exists')
sys.exit()
else:
print(plotPath + ' plotting')
scaleF = 1.2
fig = plt.figure(figsize=(4.13 * scaleF, 3.3 * scaleF), tight_layout={'rect': [0, 0.13, 1, 1], 'w_pad': 4})
axs = fig.subplots(nrows=1, ncols=1, squeeze=False)
scenarios = ['basic', 'malicious']
for scenarioName in scenarios:
templateName = lambda f: os.path.basename(f).split('.')[0]
templateSize = lambda f: templateName(f).split('-')[1]
algorithmName = lambda f: os.path.basename(f).split('.')[2]
# Plot STDOUT
stdoutFiles = [
os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk(scenarioName)
for filename in filenames if filename.endswith('.stdout') and '1.simulate.' in filename
]
for stdoutFile in stdoutFiles:
if '-md-' not in stdoutFile:
continue
if '.basic.' in stdoutFile:
continue
currentCycle = 0
paymentCount = 0
paymentSuccessCount = 0
hopCount = 0
lastHopCount = 0
fee = 0.0
lastFee = 0.0
channelCount = 0
nodeCount = 0
with open(os.path.join(scenarioName, templateName(stdoutFile) + '.json'), 'r') as templateFd:
template = json.load(templateFd)
channelCount = len(template['network']['channels'])
nodeCount = len(template['network']['nodes'])
successRates = []
avgHopCount = []
avgFee = []
avgChannelCounts = []
nodeNetworkUsage = []
routerNetworkUsage = []
with open(stdoutFile, 'r') as stdoutFp:
for line in stdoutFp:
# Parse data lines
dataLine = re.search(r'^Data\(topic=(.+), data=(.+)\)$', line)
if not dataLine:
continue
topic = dataLine.group(1)
data = dataLine.group(2)
if topic == 'cycle':
currentCycle = int(data)
avgChannelCounts.append((currentCycle, channelCount * 2 / nodeCount))
if topic.endswith('-payment'):
paymentCount += 1
if topic.endswith('-payment-successful'):
paymentSuccessCount += 1
successRates.append((currentCycle, paymentSuccessCount / paymentCount))
hopCount += lastHopCount
avgHopCount.append((currentCycle, hopCount / paymentSuccessCount))
fee += lastFee
avgFee.append((currentCycle, fee / paymentSuccessCount))
if topic.endswith('-payment-failed') and (len(successRates) == 0 or successRates[-1][0] != currentCycle):
successRates.append((currentCycle, paymentSuccessCount / paymentCount))
defaultAvgHopCount = 0 if len(avgHopCount) == 0 else avgHopCount[-1][1]
avgHopCount.append((currentCycle, defaultAvgHopCount))
defaultAvgFee = 0 if len(avgFee) == 0 else avgFee[-1][1]
avgFee.append((currentCycle, defaultAvgFee))
if topic == 'single-payment':
lastFee = 0.0
if topic == 'channel':
lastHopCount = 1
if topic == 'multi-channel':
lastHopCount = data.count('0x') - 1
if topic == 'payment-fees':
lastFee = float(data.split(',')[1].strip(' )'))
if topic == 'node-network-usage':
nodeNetworkUsage.append((currentCycle, int(data.split(',')[0].strip(' (')), int(data.split(',')[1].strip(' )')) / 1024 / 1024))
if topic == 'router-network-usage':
routerNetworkUsage.append((currentCycle, int(data.split(',')[0].strip(' (')), int(data.split(',')[1].strip(' )')) / 1024 / 1024))
if topic == 'open-channel':
channelCount += 1
if topic == 'close-channel':
channelCount -= 1
algName = algorithmName(stdoutFile)
x_val = [e[0] for e in successRates]
y_val = [e[1] for e in successRates]
t, c, k = interpolate.splrep(x_val, y_val)
spline = interpolate.BSpline(t, c, k, extrapolate=False)
xx = np.arange(0, 520, 20)
xx[0] = 1
axs[0, 0].plot(xx, spline(xx), lineTypes[algName], label=algNames[algName] + ' ' + scenarioName)
ax = axs[0, 0]
ax.set_xlabel('cycles')
ax.set_ylabel('success ratio')
for ax in axs.flat:
ax.set_ylim(bottom=-0.05, top=1.05)
ax.set_xlim(left=-10, right=510)
ax.grid()
for ln in ax.get_lines():
ln.set_markersize(5.0)
ln.set_linewidth(0.9)
if ' malicious' in ln.get_label():
ln.set_linestyle(':')
handles, labels = axs[0, 0].get_legend_handles_labels()
index, handles, labels = zip(*sorted(zip(range(6), handles, labels), key=lambda t: (t[0] % 3) * 2 + (t[0] // 3)))
fig.legend(handles, labels, loc='lower center', ncol=3, fontsize=9.2)
fig.savefig(plotPath)
#os.startfile(plotPath)