From 088699bfbb0047b1e0718736840e372a73db90ed Mon Sep 17 00:00:00 2001 From: cpacia Date: Wed, 3 Jun 2015 18:05:20 -0400 Subject: [PATCH] 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)