From 154702e6ddcc4652f642ab2b5ab22e5d54648452 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 20 Feb 2019 19:24:44 -0500 Subject: [PATCH] fix KBucket.add_peer --- lbrynet/dht/protocol/protocol.py | 4 ++++ lbrynet/dht/protocol/routing_table.py | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lbrynet/dht/protocol/protocol.py b/lbrynet/dht/protocol/protocol.py index 4e7a212bb..c772b4adf 100644 --- a/lbrynet/dht/protocol/protocol.py +++ b/lbrynet/dht/protocol/protocol.py @@ -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) diff --git a/lbrynet/dht/protocol/routing_table.py b/lbrynet/dht/protocol/routing_table.py index 600abc7bf..b21b86559 100644 --- a/lbrynet/dht/protocol/routing_table.py +++ b/lbrynet/dht/protocol/routing_table.py @@ -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: