forked from mit-ll/blockchain-simulation-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
160 lines (133 loc) · 5.64 KB
/
analysis.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
160
from collections import defaultdict
import json
import logging
import matplotlib.pyplot as plt
import numpy as np
import os
from json_endec import GraphDecoder
def plotCDF(data):
plt.plot(np.sort(data), np.linspace(0, 1, len(data), endpoint=False))
def showMultiCDF(run_results, exclude_genesis=True):
"""Displays overlaid CDFs of the max time it took for consensus to be reached for each transaction the results of one run.
Arguments:
run_results {dict} -- Dictionary mapping tx id to list of times it took for miners to reach consensus for that tx.
Keyword Arguments:
exclude_genesis {bool} -- Whether to exclude the genesis tx. (default: {True})
"""
for tx_id in run_results:
if exclude_genesis and tx_id == 0:
continue
plotCDF(run_results[tx_id]['times'])
plt.show()
def showTotalCDF(data, exclude_genesis=True):
"""Displays an aggregate CDF of the max time it took for consensus to be reached for every transaction in a set of results.
Arguments:
data {list((networkx.Graph, dict))} -- Output of loadData below.
Keyword Arguments:
exclude_genesis {bool} -- Whether to exclude the genesis tx. (default: {True})
"""
all_times = []
for run in data:
run_results = run[1]
for tx_id in run_results:
if exclude_genesis and tx_id == 0:
continue
all_times += run_results[tx_id]['times'] # Combines all histogram data into one.
plotCDF(all_times)
plt.show()
def reportDisconsensed(data):
"""Reports on which transactions were in consensus and left it during the simulation.
Arguments:
data {list((networkx.Graph, dict))} -- Output of loadData below.
"""
tx_count = 0
disc_count = 0
became_disc_times = set()
disc_lasted_durations = []
for run in data:
run_results = run[1]
for tx_id in run_results:
tx_count += 1
counted = False
for disc in run_results[tx_id]['disconsensed']:
if not counted:
disc_count += 1
counted = True
became_disc_times.add(disc[0])
if disc[1] != -1: # Don't count disc durations if tx hadn't become consensed again before sim finished.
disc_lasted_durations.append(disc[1])
if disc_count == 0:
logging.info("No nodes were disconsensed!")
return None
logging.info("%d out of %d tx disconsensed." % (disc_count, tx_count))
percent = disc_count / float(tx_count)
logging.info("Chance to be disconsensed: %f" % percent)
plotCDF(list(became_disc_times))
plt.show()
plotCDF(disc_lasted_durations)
plt.show()
return tx_count, disc_count, list(became_disc_times), disc_lasted_durations
def timeBetweenTx(data):
"""Returns a list of the number of elapsed ticks between each created transaction.
Arguments:
data {list((networkx.Graph, dict))} -- Output of loadData below.
"""
between = []
for run in data:
run_results = run[1]
last = 0
for created in sorted([run_results[tx_id]['created'][0] for tx_id in run_results]):
if created < 0:
continue
assert created > last, "created: %d; last: %d" % (created, last)
between.append(created-last)
last = created
return between
def loadData(data_dir):
"""Loads data from files in data_dir into a list of dictionaries mapping tx id to list of times it took for miners to reach consensus for that tx, one dict for each run.
Arguments:
data_dir {str} -- Directory where data files should be loaded from.
Returns:
{list((networkx.Graph, dict))} -- List of (graph, dictionary) tuples--one tuple for each run--in which the dictionary maps tx id to a (event, list) tuple of the tx's creation and times it took for miners to reach consensus for that tx.
"""
raw_data = []
for fname in os.listdir(data_dir):
if not fname.endswith('.json'):
continue
with open(data_dir+fname, 'r') as infile:
raw_data.append(json.load(infile, cls=GraphDecoder))
if not raw_data:
return
data = []
for run in raw_data:
run_results = defaultdict(dict)
for tx_id_str in run['tx_histories']:
history = run['tx_histories'][tx_id_str]
tx_id = int(tx_id_str)
max_times = defaultdict(int)
last_disc = -1
disconsensed = []
created = None
for event in history: # Event is [time_step, miner_id, transaction.State].
if event[2] == 'CREATED':
created = event
continue
elif event[2] == 'DISCONSENSED':
assert created is not None
disconsensed.append([event[0] - created[0], -1])
last_disc = event[0]
continue
elif event[2] != 'CONSENSUS':
continue
if last_disc > 0:
if event[0] > last_disc: # Sequence of chronologically ordered events is broken when another history is appended.
disconsensed[-1][1] = event[0] - last_disc
last_disc = -1
elapsed = event[0] - created[0]
if max_times[event[1]] < elapsed:
max_times[event[1]] = elapsed
run_results[tx_id]['created'] = created
run_results[tx_id]['times'] = max_times.values()
run_results[tx_id]['disconsensed'] = disconsensed
data.append((run['graph'], run_results))
return data