Skip to content

Commit

Permalink
Merge pull request #377 from qstokkink/stupidify_tracker
Browse files Browse the repository at this point in the history
Simplified tracker plugin
  • Loading branch information
qstokkink authored Nov 22, 2018
2 parents 42fcf1b + 57077ce commit 00e8700
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ipv8/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions ipv8/test/test_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
38 changes: 22 additions & 16 deletions twisted/plugins/tracker_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:], ])
Expand All @@ -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)

Expand All @@ -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]]
Expand Down

0 comments on commit 00e8700

Please sign in to comment.