From 6c98070f878cef8d70f8afba8f91373465916158 Mon Sep 17 00:00:00 2001 From: qstokkink Date: Fri, 15 Feb 2019 11:11:03 +0100 Subject: [PATCH] Removed unused service_id usage --- ipv8/community.py | 19 +++++++------------ ipv8/overlay.py | 13 +++++++++++-- ipv8/peerdiscovery/churn.py | 4 ++-- ipv8/peerdiscovery/community.py | 2 +- ipv8/peerdiscovery/discovery.py | 14 +++++++------- ipv8/test/mocking/discovery.py | 2 +- ipv8/test/mocking/rest/ipv8.py | 5 ++--- ipv8_service.py | 5 ++--- stresstest/endpoint_stresstest.py | 2 +- twisted/plugins/tracker_plugin.py | 2 +- 10 files changed, 35 insertions(+), 33 deletions(-) diff --git a/ipv8/community.py b/ipv8/community.py index b5d3ada52..8dfd60a4f 100644 --- a/ipv8/community.py +++ b/ipv8/community.py @@ -71,7 +71,7 @@ class Community(EZPackOverlay): def __init__(self, my_peer, endpoint, network, max_peers=DEFAULT_MAX_PEERS): super(Community, self).__init__(self.master_peer, my_peer, endpoint, network) - self._prefix = b'\x00' + self.version + self.master_peer.key.key_to_hash() + self._prefix = b'\x00' + self.version + self.master_peer.mid self.logger.debug("Launching %s with prefix %s.", self.__class__.__name__, hexlify(self._prefix)) self.max_peers = max_peers @@ -323,18 +323,14 @@ def walk_to(self, address): packet = self.create_introduction_request(address) self.endpoint.send(address, packet) - def send_introduction_request(self, peer, service_id=None): + def send_introduction_request(self, peer): """ Send an introduction request to a specific peer. """ packet = self.create_introduction_request(peer.address) - - if service_id: - packet = packet[:2] + service_id + packet[22:] - self.endpoint.send(peer.address, packet) - def get_new_introduction(self, from_peer=None, service_id=None): + def get_new_introduction(self, from_peer=None): """ Get a new introduction, or bootstrap if there are no available peers. """ @@ -351,10 +347,6 @@ def get_new_introduction(self, from_peer=None, service_id=None): return packet = self.create_introduction_request(from_peer) - - if service_id: - packet = packet[:2] + service_id + packet[22:] - self.endpoint.send(from_peer, packet) def get_peer_for_introduction(self, exclude=None): @@ -364,5 +356,8 @@ def get_peer_for_introduction(self, exclude=None): available = [p for p in self.get_peers() if p != exclude] return choice(available) if available else None + def get_walkable_addresses(self): + return self.network.get_walkable_addresses(self.master_peer.mid) + def get_peers(self): - return self.network.get_peers_for_service(self.master_peer.key.key_to_hash()) + return self.network.get_peers_for_service(self.master_peer.mid) diff --git a/ipv8/overlay.py b/ipv8/overlay.py index a6cfa55b4..566f86a5c 100644 --- a/ipv8/overlay.py +++ b/ipv8/overlay.py @@ -104,12 +104,11 @@ def walk_to(self, address): pass @abc.abstractmethod - def get_new_introduction(self, from_peer=None, service_id=None): + def get_new_introduction(self, from_peer=None): """ Get a new IP address to walk to from a random, or selected peer. :param from_peer: the peer to ask for an introduction - :param service_id: try to get a new introduction for a certain service """ pass @@ -122,6 +121,16 @@ def get_peers(self): """ pass + @abc.abstractmethod + def get_walkable_addresses(self): + """ + Get the list of IPv4 addresses we can walk to on this overlay. + + :return: a list of IPv4 addresses + :rtype: [(str, int)] + """ + pass + def get_peer_for_introduction(self, exclude=None): """ Get a peer for introduction. diff --git a/ipv8/peerdiscovery/churn.py b/ipv8/peerdiscovery/churn.py index 4647d8267..ac8afe446 100644 --- a/ipv8/peerdiscovery/churn.py +++ b/ipv8/peerdiscovery/churn.py @@ -44,7 +44,7 @@ def is_inactive(self, peer): return False return time() > (peer.last_response + self.inactive_time) - def take_step(self, service_id=None): + def take_step(self): """ Select a new (set of) peer(s) to investigate liveness for. """ @@ -74,7 +74,7 @@ def __init__(self, overlay, ping_interval=25): super(PingChurn, self).__init__(overlay) self.ping_interval = ping_interval - def take_step(self, service_id=None): + def take_step(self): with self.walk_lock: self.overlay.routing_table.remove_bad_nodes() diff --git a/ipv8/peerdiscovery/community.py b/ipv8/peerdiscovery/community.py index 87eadae11..e4ff1951d 100644 --- a/ipv8/peerdiscovery/community.py +++ b/ipv8/peerdiscovery/community.py @@ -23,7 +23,7 @@ def __init__(self, overlay): super(PeriodicSimilarity, self).__init__(overlay) self.last_step = 0 - def take_step(self, service_id=None): + def take_step(self): now = time() if (now - self.last_step < 1.0) or not self.overlay.network.verified_peers: return diff --git a/ipv8/peerdiscovery/discovery.py b/ipv8/peerdiscovery/discovery.py index c2b2dc1f4..e2edafe0a 100644 --- a/ipv8/peerdiscovery/discovery.py +++ b/ipv8/peerdiscovery/discovery.py @@ -17,7 +17,7 @@ def __init__(self, overlay): self.walk_lock = Lock() @abc.abstractmethod - def take_step(self, service_id=None): + def take_step(self): pass @@ -45,7 +45,7 @@ def __init__(self, overlay, timeout=3.0, window_size=5, reset_chance=50): self.window_size = window_size self.reset_chance = reset_chance - def take_step(self, service_id=None): + def take_step(self): """ Walk to random walkable peer. """ @@ -63,7 +63,7 @@ def take_step(self, service_id=None): if self.window_size and self.window_size > 0 and len(self.intro_timeouts) >= self.window_size: return # Take step - known = self.overlay.network.get_walkable_addresses(service_id) + known = self.overlay.network.get_walkable_addresses() available = list(set(known) - set(self.intro_timeouts.keys())) # We can get stuck in an infinite loop of unreachable peers if we never contact the tracker again @@ -72,7 +72,7 @@ def take_step(self, service_id=None): self.overlay.walk_to(peer) self.intro_timeouts[peer] = time() else: - self.overlay.get_new_introduction(service_id=service_id) + self.overlay.get_new_introduction() class EdgeWalk(DiscoveryStrategy): @@ -102,7 +102,7 @@ def get_available_root(self): available = list(set(self._neighborhood) - set(self.under_construction.keys())) return choice(available) if available else None - def take_step(self, service_id=None): + def take_step(self): """ Attempt to grow an edge. """ @@ -111,7 +111,7 @@ def take_step(self, service_id=None): # Wait for our immediate neighborhood to be discovered self._neighborhood = self.overlay.get_peers()[:self.neighborhood_size] self.overlay.bootstrap() - for peer in self.overlay.network.get_walkable_addresses(service_id)[:self.neighborhood_size]: + for peer in self.overlay.get_walkable_addresses()[:self.neighborhood_size]: self.overlay.walk_to(peer) else: waiting_root = self.get_available_root() @@ -119,7 +119,7 @@ def take_step(self, service_id=None): if waiting_root: self.under_construction[waiting_root] = [waiting_root] self.last_edge_responses[waiting_root] = time() - self.overlay.get_new_introduction(waiting_root.address, service_id=service_id) + self.overlay.get_new_introduction(waiting_root.address) else: # Check if our introduced peer has answered yet completed = [] diff --git a/ipv8/test/mocking/discovery.py b/ipv8/test/mocking/discovery.py index 49114f278..77e90575d 100644 --- a/ipv8/test/mocking/discovery.py +++ b/ipv8/test/mocking/discovery.py @@ -5,6 +5,6 @@ class MockWalk(DiscoveryStrategy): - def take_step(self, service_id=None): + def take_step(self): for peer in self.overlay.network.verified_peers: self.overlay.walk_to(peer.address) diff --git a/ipv8/test/mocking/rest/ipv8.py b/ipv8/test/mocking/rest/ipv8.py index cd12a0508..41876dbf0 100644 --- a/ipv8/test/mocking/rest/ipv8.py +++ b/ipv8/test/mocking/rest/ipv8.py @@ -43,10 +43,9 @@ def on_tick(self): if self.endpoint.is_open(): with self.overlay_lock: for strategy, target_peers in self.strategies: - service = strategy.overlay.master_peer.mid - peer_count = len(self.network.get_peers_for_service(service)) + peer_count = len(strategy.overlay.get_peers()) if (target_peers == -1) or (peer_count < target_peers): - strategy.take_step(service) + strategy.take_step() def unload(self): # Make sure the state machine is running before closing it diff --git a/ipv8_service.py b/ipv8_service.py index a98fee488..cefc42620 100644 --- a/ipv8_service.py +++ b/ipv8_service.py @@ -133,13 +133,12 @@ def on_tick(self): smooth = self.state_machine_lc.interval//len(self.strategies) if self.strategies else 0 ticker = len(self.strategies) for strategy, target_peers in self.strategies: - service = strategy.overlay.master_peer.mid - peer_count = len(self.network.get_peers_for_service(service)) + peer_count = len(strategy.overlay.get_peers()) start_time = time.time() if (target_peers == -1) or (peer_count < target_peers): # We wrap the take_step into a general except as it is prone to programmer error. try: - strategy.take_step(service) + strategy.take_step() except: logging.error("Exception occurred while trying to walk!\n" + ''.join(format_exception(*sys.exc_info()))) diff --git a/stresstest/endpoint_stresstest.py b/stresstest/endpoint_stresstest.py index 42f339ea8..9d0317690 100644 --- a/stresstest/endpoint_stresstest.py +++ b/stresstest/endpoint_stresstest.py @@ -83,7 +83,7 @@ def on_packet(self, packet): def walk_to(self, address): pass - def get_new_introduction(self, from_peer=None, service_id=None): + def get_new_introduction(self, from_peer=None): return None def get_peers(self): diff --git a/twisted/plugins/tracker_plugin.py b/twisted/plugins/tracker_plugin.py index 8b73ca848..8b0d6502b 100644 --- a/twisted/plugins/tracker_plugin.py +++ b/twisted/plugins/tracker_plugin.py @@ -36,7 +36,7 @@ class SimpleChurn(DiscoveryStrategy): Remove peers every 120 seconds. """ - def take_step(self, service_id=None): + def take_step(self): with self.walk_lock: for peer in self.overlay.network.verified_peers[:]: if time.time() - peer.last_response > 120: