fix KBucket.add_peer

This commit is contained in:
Jack Robison 2019-02-20 19:24:44 -05:00
parent d06d998c81
commit 154702e6dd
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 12 additions and 1 deletions

View file

@ -313,9 +313,13 @@ class KademliaProtocol(DatagramProtocol):
return args, {}
async def _add_peer(self, peer: 'KademliaPeer'):
for p in self.routing_table.get_peers():
if (p.address, p.udp_port) == (peer.address, peer.udp_port) and p.node_id != peer.node_id:
self.routing_table.remove_peer(p)
bucket_index = self.routing_table.kbucket_index(peer.node_id)
if self.routing_table.buckets[bucket_index].add_peer(peer):
return True
# The bucket is full; see if it can be split (by checking if its range includes the host node's node_id)
if self.routing_table.should_split(bucket_index, peer.node_id):
self.routing_table.split_bucket(bucket_index)

View file

@ -49,7 +49,14 @@ class KBucket:
self.peers.remove(peer)
self.peers.append(peer)
return True
elif len(self.peers) < constants.k:
else:
for i in range(len(self.peers)):
p = self.peers[i]
if p.node_id == peer.node_id:
self.peers.remove(p)
self.peers.append(peer)
return True
if len(self.peers) < constants.k:
self.peers.append(peer)
return True
else: