diff --git a/lbrynet/dht/routingtable.py b/lbrynet/dht/routingtable.py index ec2ee75e0..a23d68370 100644 --- a/lbrynet/dht/routingtable.py +++ b/lbrynet/dht/routingtable.py @@ -135,17 +135,21 @@ class TreeRoutingTable(object): i = 1 canGoLower = bucketIndex - i >= 0 canGoHigher = bucketIndex + i < len(self._buckets) + + def get_remain(closest): + return min(count, constants.k) - len(closest) + # Fill up the node list to k nodes, starting with the closest neighbouring nodes known while len(closestNodes) < min(count, constants.k) and (canGoLower or canGoHigher): # TODO: this may need to be optimized - remain = min(count, constants.k) - len(closestNodes) - if canGoLower: + if canGoLower and len(closestNodes) < min(count, constants.k): closestNodes.extend( - self._buckets[bucketIndex - i].getContacts(remain, _rpcNodeID)) + self._buckets[bucketIndex - i].getContacts(get_remain(closestNodes), + _rpcNodeID)) canGoLower = bucketIndex - (i + 1) >= 0 - if canGoHigher: - closestNodes.extend( - self._buckets[bucketIndex + i].getContacts(remain, _rpcNodeID)) + if canGoHigher and len(closestNodes) < min(count, constants.k): + closestNodes.extend(self._buckets[bucketIndex + i].getContacts( + get_remain(closestNodes), _rpcNodeID)) canGoHigher = bucketIndex + (i + 1) < len(self._buckets) i += 1 return closestNodes