Skip to content

Commit

Permalink
Merge pull request #445 from qstokkink/remove_service_id
Browse files Browse the repository at this point in the history
Removed unused service_id usage
  • Loading branch information
qstokkink authored Feb 15, 2019
2 parents f051b7e + 6c98070 commit 553396b
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 33 deletions.
19 changes: 7 additions & 12 deletions ipv8/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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):
Expand All @@ -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)
13 changes: 11 additions & 2 deletions ipv8/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions ipv8/peerdiscovery/churn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion ipv8/peerdiscovery/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions ipv8/peerdiscovery/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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.
"""
Expand All @@ -111,15 +111,15 @@ 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()
# Make sure we have as many outstanding/actively growing edges as roots
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 = []
Expand Down
2 changes: 1 addition & 1 deletion ipv8/test/mocking/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 2 additions & 3 deletions ipv8/test/mocking/rest/ipv8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions ipv8_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
Expand Down
2 changes: 1 addition & 1 deletion stresstest/endpoint_stresstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion twisted/plugins/tracker_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 553396b

Please sign in to comment.