Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 47d07ca

Browse files
committed
Wait for NEWCONSENSUS event when necessary
1 parent d2db0c4 commit 47d07ca

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

bwscanner/attacher.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,30 @@ def launch_and_get_state(ignore):
134134
return get_random_tor_ports().addCallback(launch_and_get_state)
135135

136136

137-
def update_tor_config(tor, config):
137+
def options_need_new_consensus(tor_config, new_options):
138138
"""
139-
Update the Tor config from a dict of config key: value pairs.
139+
Check if we need to wait for a new consensus after updating
140+
the Tor config with the new options.
140141
"""
141-
config_pairs = [(key, value) for key, value in config.items()]
142-
d = tor.protocol.set_conf(*itertools.chain.from_iterable(config_pairs))
143-
#XXX Only follow this path if we are changing config options that
144-
# require a wait for NEWCONSENSUS
145-
def wait_for_newconsensus(_):
146-
got_consensus = defer.Deferred()
147-
def got_newconsensus(evt):
148-
got_consensus.callback(tor)
149-
tor.protocol.remove_event_listener('NEWCONSENSUS', got_newconsensus)
150-
tor.protocol.add_event_listener('NEWCONSENSUS', got_newconsensus)
151-
return got_consensus
152-
return d.addCallback(wait_for_newconsensus)
142+
if "UseMicroDescriptors" in new_options:
143+
if tor_config.UseMicroDescriptors != new_options["UseMicroDescriptors"]:
144+
log.debug("Changing UseMicroDescriptors from {current} to {new}.",
145+
current=tor_config.UseMicroDescriptors,
146+
new=new_options["UseMicroDescriptors"])
147+
return True
148+
return False
149+
150+
151+
def wait_for_newconsensus(tor_state):
152+
got_consensus = defer.Deferred()
153+
154+
def got_newconsensus(event):
155+
log.debug("Got NEWCONSENSUS event: {event}", event=event)
156+
got_consensus.callback(event)
157+
tor_state.protocol.remove_event_listener('NEWCONSENSUS', got_newconsensus)
158+
159+
tor_state.protocol.add_event_listener('NEWCONSENSUS', got_newconsensus)
160+
return got_consensus
153161

154162

155163
def setconf_singleport_exit(tor):
@@ -205,10 +213,16 @@ def connect_to_tor(launch_tor, circuit_build_timeout, control_port=None,
205213
# messages are received while Txtorcon is reading the consensus.
206214
tor_state = yield tor.create_state()
207215

208-
# Update Tor config options from dictionary
216+
# Get current TorConfig object
209217
tor_config = yield tor.get_config()
218+
wait_for_consensus = options_need_new_consensus(tor_config, tor_options)
219+
220+
# Update Tor config options from dictionary
210221
for key, value in tor_options.items():
211222
setattr(tor_config, key, value)
212223
yield tor_config.save() # Send updated options to Tor
213224

225+
if wait_for_consensus:
226+
yield wait_for_newconsensus(tor_state)
227+
214228
defer.returnValue(tor_state)

0 commit comments

Comments
 (0)