fix remaining length of close nodes list

This commit is contained in:
Jack Robison 2017-10-27 21:46:17 -04:00
parent ee90c877c7
commit e89f3db31b
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -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