From 80ae9236df63bf872d4f4a12d73aad1a84e6afad Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 22 Dec 2016 18:28:26 -0500 Subject: [PATCH] make RelayV1Hint objects hashable/comparable --- src/wormhole/test/test_transit.py | 11 +++++++++++ src/wormhole/transit.py | 11 +++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/wormhole/test/test_transit.py b/src/wormhole/test/test_transit.py index 65bdf18..8d49994 100644 --- a/src/wormhole/test/test_transit.py +++ b/src/wormhole/test/test_transit.py @@ -139,6 +139,17 @@ class Hints(unittest.TestCase): ep = c._endpoint_from_hint_obj("unknown:stuff:yowza:pivlor") self.assertEqual(ep, None) + def test_comparable(self): + h1 = transit.DirectTCPV1Hint("hostname", "port1") + h1b = transit.DirectTCPV1Hint("hostname", "port1") + h2 = transit.DirectTCPV1Hint("hostname", "port2") + r1 = transit.RelayV1Hint(tuple(sorted([h1, h2]))) + r2 = transit.RelayV1Hint(tuple(sorted([h2, h1]))) + r3 = transit.RelayV1Hint(tuple(sorted([h1b, h2]))) + self.assertEqual(r1, r2) + self.assertEqual(r2, r3) + self.assertEqual(len(set([r1, r2, r3])), 1) + class Basic(unittest.TestCase): @inlineCallbacks diff --git a/src/wormhole/transit.py b/src/wormhole/transit.py index d851070..eaf81a6 100644 --- a/src/wormhole/transit.py +++ b/src/wormhole/transit.py @@ -95,9 +95,10 @@ def build_sided_relay_handshake(key, side): # * the rest of the connection contains transit data DirectTCPV1Hint = namedtuple("DirectTCPV1Hint", ["hostname", "port"]) TorTCPV1Hint = namedtuple("TorTCPV1Hint", ["hostname", "port"]) -# RelayV1Hint contains a list of DirectTCPV1Hint and TorTCPV1Hint hints. For -# each one, make the TCP connection, send the relay handshake, then complete -# the rest of the V1 protocol. Only one hint per relay is useful. +# RelayV1Hint contains a tuple of DirectTCPV1Hint and TorTCPV1Hint hints (we +# use a tuple rather than a list so they'll be hashable into a set). For each +# one, make the TCP connection, send the relay handshake, then complete the +# rest of the V1 protocol. Only one hint per relay is useful. RelayV1Hint = namedtuple("RelayV1Hint", ["hints"]) def describe_hint_obj(hint): @@ -582,7 +583,9 @@ class Common: if transit_relay: if not isinstance(transit_relay, type(u"")): raise InternalError - relay = RelayV1Hint(hints=[parse_hint_argv(transit_relay)]) + # TODO: allow multiple hints for a single relay + relay_hint = parse_hint_argv(transit_relay) + relay = RelayV1Hint(hints=(relay_hint,)) self._transit_relays = [relay] else: self._transit_relays = []