From 088699bfbb0047b1e0718736840e372a73db90ed Mon Sep 17 00:00:00 2001 From: cpacia Date: Wed, 3 Jun 2015 18:05:20 -0400 Subject: [PATCH 1/2] Bug fix in NodeHeap When running with a large number of nodes it fails to send the store request to the k closest nodes and instead ends up sending multiple store requests to the same 4 or 5 nodes. It appears the cause is a bug in NodeHeap which allows the same node to be pushed onto the heap multiple times bumping out other legimate nodes. This pull request just adds some logic to make sure the node isn't already in the heap before pushing. --- kademlia/node.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kademlia/node.py b/kademlia/node.py index 6d90493..8ea2c22 100644 --- a/kademlia/node.py +++ b/kademlia/node.py @@ -94,8 +94,9 @@ class NodeHeap(object): nodes = [nodes] for node in nodes: - distance = self.node.distanceTo(node) - heapq.heappush(self.heap, (distance, node)) + if node.id not in [n.id for n in self]: + distance = self.node.distanceTo(node) + heapq.heappush(self.heap, (distance, node)) def __len__(self): return min(len(self.heap), self.maxsize) From 0338af9b83108a5ec49628114697825a89aeee4f Mon Sep 17 00:00:00 2001 From: cpacia Date: Thu, 4 Jun 2015 16:06:25 -0400 Subject: [PATCH 2/2] added __contains__ method to NodeHeap --- kademlia/node.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kademlia/node.py b/kademlia/node.py index 8ea2c22..5f82c29 100644 --- a/kademlia/node.py +++ b/kademlia/node.py @@ -94,7 +94,7 @@ class NodeHeap(object): nodes = [nodes] for node in nodes: - if node.id not in [n.id for n in self]: + if node not in self: distance = self.node.distanceTo(node) heapq.heappush(self.heap, (distance, node)) @@ -105,5 +105,11 @@ class NodeHeap(object): nodes = heapq.nsmallest(self.maxsize, self.heap) return iter(map(itemgetter(1), nodes)) + def __contains__(self, node): + for distance, n in self.heap: + if node.id == n.id: + return True + return False + def getUncontacted(self): return [n for n in self if n.id not in self.contacted]