-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_consensuses.py
216 lines (198 loc) · 10.8 KB
/
process_consensuses.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pathsim
import stem.descriptor.reader
import stem.descriptor
import stem
import os
import os.path
import cPickle as pickle
def read_descriptors(descriptors, descriptor_dir, skip_listener):
"""Add to descriptors contents of descriptor archive in descriptor_dir."""
num_descriptors = 0
num_relays = 0
print('Reading descriptors from: {0}'.format(descriptor_dir))
reader = stem.descriptor.reader.DescriptorReader(descriptor_dir,
validate=True)
reader.register_skip_listener(skip_listener)
with reader:
for desc in reader:
if (num_descriptors % 10000 == 0):
print('{0} descriptors processed.'.format(num_descriptors))
num_descriptors += 1
if (desc.fingerprint not in descriptors):
descriptors[desc.fingerprint] = {}
num_relays += 1
descriptors[desc.fingerprint]\
[pathsim.timestamp(desc.published)] = desc
print('#descriptors: {0}; #relays:{1}'.\
format(num_descriptors,num_relays))
def process_consensuses(in_dirs, slim, initial_descriptor_dir):
"""For every input consensus, finds the descriptors published most recently before the descriptor times listed for the relays in that consensus, records state changes indicated by descriptors published during the consensus fresh period, and writes out pickled consensus and descriptor objects with the relevant information.
Inputs:
in_dirs: list of (consensus in dir, descriptor in dir,
processed descriptor out dir) triples *in order*
slim: Whether to use custom slim classes or Stem classes.
initial_descriptor_dir: Contains descriptors to initialize processing.
"""
water_filling = True
descriptors = {}
def skip_listener(path, exception):
print('ERROR [{0}]: {1}'.format(path.encode('ascii', 'ignore'), exception.__unicode__().encode('ascii','ignore')))
if slim:
print('Outputting slim classes.')
# initialize descriptors
if (initial_descriptor_dir is not None):
read_descriptors(descriptors, initial_descriptor_dir, skip_listener)
for in_consensuses_dir, in_descriptors, desc_out_dir in in_dirs:
# read all descriptors into memory
read_descriptors(descriptors, in_descriptors, skip_listener)
# output pickled consensuses, dict of most recent descriptors, and
# list of hibernation status changes
num_consensuses = 0
pathnames = []
for dirpath, dirnames, fnames in os.walk(in_consensuses_dir):
for fname in fnames:
pathnames.append(os.path.join(dirpath,fname))
pathnames.sort()
for pathname in pathnames:
filename = os.path.basename(pathname)
if (filename[0] == '.'):
continue
print('Processing consensus file {0}'.format(filename))
cons_f = open(pathname, 'rb')
descriptors_out = {}
hibernating_statuses = [] # (time, fprint, hibernating)
cons_valid_after = None
cons_fresh_until = None
if slim:
cons_bw_weights = None
cons_bwweightscale = None
relays = {}
else:
consensus = None
num_not_found = 0
num_found = 0
for r_stat in stem.descriptor.parse_file(cons_f, validate=True):
if (cons_valid_after == None):
cons_valid_after = r_stat.document.valid_after
# compute timestamp version once here
valid_after_ts = pathsim.timestamp(cons_valid_after)
if (cons_fresh_until == None):
cons_fresh_until = r_stat.document.fresh_until
# compute timestamp version once here
fresh_until_ts = pathsim.timestamp(cons_fresh_until)
if slim:
if (cons_bw_weights == None):
cons_bw_weights = r_stat.document.bandwidth_weights
if (cons_bwweightscale == None) and \
('bwweightscale' in r_stat.document.params):
cons_bwweightscale = r_stat.document.params[\
'bwweightscale']
if slim:
relays[r_stat.fingerprint] = pathsim.RouterStatusEntry(\
r_stat.fingerprint, r_stat.nickname, \
r_stat.flags, r_stat.bandwidth, water_filling=water_filling)
else:
if (consensus == None):
consensus = r_stat.document
consensus.routers = {} # should be empty - ensure
consensus.routers[r_stat.fingerprint] = r_stat
# find most recent unexpired descriptor published before
# the publication time in the consensus
# and status changes in fresh period (i.e. hibernation)
pub_time = pathsim.timestamp(r_stat.published)
desc_time = 0
descs_while_fresh = []
desc_time_fresh = None
# get all descriptors with this fingerprint
if (r_stat.fingerprint in descriptors):
for t,d in descriptors[r_stat.fingerprint].items():
# update most recent desc seen before cons pubtime
# allow pubtime after valid_after but not fresh_until
if (valid_after_ts-t <\
pathsim.TorOptions.router_max_age) and\
(t <= pub_time) and (t > desc_time) and\
(t <= fresh_until_ts):
desc_time = t
# store fresh-period descs for hibernation tracking
if (t >= valid_after_ts) and \
(t <= fresh_until_ts):
descs_while_fresh.append((t,d))
# find most recent hibernating stat before fresh period
# prefer most-recent descriptor before fresh period
# but use oldest after valid_after if necessary
if (desc_time_fresh == None):
desc_time_fresh = t
elif (desc_time_fresh < valid_after_ts):
if (t > desc_time_fresh) and\
(t <= valid_after_ts):
desc_time_fresh = t
else:
if (t < desc_time_fresh):
desc_time_fresh = t
# output best descriptor if found
if (desc_time != 0):
num_found += 1
# store discovered recent descriptor
desc = descriptors[r_stat.fingerprint][desc_time]
if slim:
descriptors_out[r_stat.fingerprint] = \
pathsim.ServerDescriptor(desc.fingerprint, \
desc.hibernating, desc.nickname, \
desc.family, desc.address, \
desc.exit_policy, desc.ntor_onion_key)
else:
descriptors_out[r_stat.fingerprint] = desc
# store hibernating statuses
if (desc_time_fresh == None):
raise ValueError('Descriptor error for {0}:{1}.\n Found descriptor before published date {2}: {3}\nDid not find descriptor for initial hibernation status for fresh period starting {4}.'.format(r_stat.nickname, r_stat.fingerprint, pub_time, desc_time, valid_after_ts))
desc = descriptors[r_stat.fingerprint][desc_time_fresh]
cur_hibernating = desc.hibernating
# setting initial status
hibernating_statuses.append((0, desc.fingerprint,\
cur_hibernating))
if (cur_hibernating):
print('{0}:{1} was hibernating at consenses period start'.format(desc.nickname, desc.fingerprint))
descs_while_fresh.sort(key = lambda x: x[0])
for (t,d) in descs_while_fresh:
if (d.hibernating != cur_hibernating):
cur_hibernating = d.hibernating
hibernating_statuses.append(\
(t, d.fingerprint, cur_hibernating))
if (cur_hibernating):
print('{0}:{1} started hibernating at {2}'\
.format(d.nickname, d.fingerprint, t))
else:
print('{0}:{1} stopped hibernating at {2}'\
.format(d.nickname, d.fingerprint, t))
else:
# print(\
# 'Descriptor not found for {0}:{1}:{2}'.format(\
# r_stat.nickname,r_stat.fingerprint, pub_time))
num_not_found += 1
# output pickled consensus, recent descriptors, and
# hibernating status changes
if (cons_valid_after != None) and\
(cons_fresh_until != None):
if slim:
consensus = pathsim.NetworkStatusDocument(\
cons_valid_after, cons_fresh_until, cons_bw_weights,\
cons_bwweightscale, relays)
hibernating_statuses.sort(key = lambda x: x[0],\
reverse=True)
outpath = os.path.join(desc_out_dir,\
cons_valid_after.strftime(\
'%Y-%m-%d-%H-%M-%S-network_state'))
f = open(outpath, 'wb')
pickle.dump(consensus, f, pickle.HIGHEST_PROTOCOL)
pickle.dump(descriptors_out,f,pickle.HIGHEST_PROTOCOL)
pickle.dump(hibernating_statuses,f,pickle.HIGHEST_PROTOCOL)
f.close()
print('Wrote descriptors for {0} relays.'.\
format(num_found))
print('Did not find descriptors for {0} relays\n'.\
format(num_not_found))
else:
print('Problem parsing {0}.'.format(filename))
num_consensuses += 1
cons_f.close()
print('# consensuses: {0}'.format(num_consensuses))