From 57077cef81a580f15c909a7cd19624594a1d20db Mon Sep 17 00:00:00 2001 From: qstokkink Date: Wed, 21 Nov 2018 14:54:49 +0100 Subject: [PATCH] Simplified tracker plugin --- ipv8/peer.py | 2 +- ipv8/test/test_peer.py | 4 ++-- twisted/plugins/tracker_plugin.py | 38 ++++++++++++++++++------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/ipv8/peer.py b/ipv8/peer.py index 0ae94739e..516f9a8c7 100644 --- a/ipv8/peer.py +++ b/ipv8/peer.py @@ -52,7 +52,7 @@ def __hash__(self): def __eq__(self, other): if not isinstance(other, Peer): return False - return (self.public_key.key_to_bin() == other.public_key.key_to_bin()) and (self.address == other.address) + return (self.public_key.key_to_bin() == other.public_key.key_to_bin()) def __str__(self): return 'Peer<%s:%d, %s>' % (self.address[0], self.address[1], b64encode(self.mid)) diff --git a/ipv8/test/test_peer.py b/ipv8/test/test_peer.py index 71654b93a..ebe0836cf 100644 --- a/ipv8/test/test_peer.py +++ b/ipv8/test/test_peer.py @@ -64,11 +64,11 @@ def test_peer_inequality_key(self): def test_peer_inequality_address(self): """ - Check if peers with the same key and a different address are not equal. + Check if peers with the same key and a different address are equal. """ other = Peer(self.peer.key) - self.assertNotEqual(self.peer, other) + self.assertEqual(self.peer, other) def test_to_string(self): """ diff --git a/twisted/plugins/tracker_plugin.py b/twisted/plugins/tracker_plugin.py index 30c7621ea..334a213e2 100644 --- a/twisted/plugins/tracker_plugin.py +++ b/twisted/plugins/tracker_plugin.py @@ -22,27 +22,40 @@ from twisted.python.log import msg from zope.interface import implements +from ipv8.deprecated.community import Community from ipv8.deprecated.payload import IntroductionRequestPayload from ipv8.keyvault.crypto import default_eccrypto from ipv8.messaging.interfaces.udp.endpoint import UDPEndpoint from ipv8.peer import Peer -from ipv8.peerdiscovery.churn import RandomChurn -from ipv8.peerdiscovery.community import DiscoveryCommunity +from ipv8.peerdiscovery.churn import DiscoveryStrategy from ipv8.peerdiscovery.network import Network -class EndpointServer(DiscoveryCommunity): +class SimpleChurn(DiscoveryStrategy): + """ + Remove peers every 120 seconds. + """ + + def take_step(self, service_id=None): + with self.walk_lock: + for peer in self.overlay.network.verified_peers[:]: + if time.time() - peer.last_response > 120: + self.overlay.network.remove_peer(peer) + + +class EndpointServer(Community): """ Make some small modifications to the Community to allow it a dynamic prefix. We will also only answer introduction requests. """ + master_peer = Peer(default_eccrypto.generate_key(u"very-low")) def __init__(self, endpoint): my_peer = Peer(default_eccrypto.generate_key(u"very-low")) + self.signature_length = default_eccrypto.get_signature_length(my_peer.public_key) super(EndpointServer, self).__init__(my_peer, endpoint, Network()) - self.churn_lc = self.register_task("churn", LoopingCall(self.do_churn)).start(5.0, now=False) - self.churn_strategy = RandomChurn(self) - self.crypto = default_eccrypto + self.churn_strategy = SimpleChurn(self) + self.churn_lc = self.register_task("churn", LoopingCall(self.churn_strategy.take_step)).start(30.0, now=False) def on_packet(self, packet, warn_unknown=False): source_address, data = packet @@ -61,6 +74,7 @@ def on_packet(self, packet, warn_unknown=False): def on_generic_introduction_request(self, source_address, data, prefix): auth, dist, payload = self._ez_unpack_auth(IntroductionRequestPayload, data) peer = Peer(auth.public_key_bin, source_address) + peer.last_response = time.time() self.network.add_verified_peer(peer) self.network.discover_services(peer, [prefix[2:], ]) @@ -74,9 +88,8 @@ def on_generic_introduction_request(self, source_address, data, prefix): packet = self.create_introduction_response(payload.destination_address, peer.address, payload.identifier, introduction=intro_peer) - signature_length = self.crypto.get_signature_length(self.my_peer.public_key) - packet = prefix + packet[22:-signature_length] - signature = self.crypto.create_signature(self.my_peer.key, packet) + packet = prefix + packet[22:-self.signature_length] + signature = default_eccrypto.create_signature(self.my_peer.key, packet) self.endpoint.send(peer.address, packet + signature) @@ -88,13 +101,6 @@ def get_peer_for_introduction(self, exclude=None): """ return None - def do_churn(self): - """ - Dynamically scale churn to check one fifth of our network every step. - """ - self.churn_strategy.sample_size = min(len(self.network.verified_peers)//5, 1) - self.churn_strategy.take_step() - class Options(usage.Options): optParameters = [["listen_port", None, None, None, int]]