use midpoint id instead of random for refresh list

This commit is contained in:
Jack Robison 2018-11-13 09:36:38 -05:00
parent 6dbb00d8d5
commit 9bb91b126d
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -7,7 +7,6 @@
import random import random
import logging import logging
from binascii import unhexlify
from twisted.internet import defer from twisted.internet import defer
from lbrynet.dht import constants, kbucket from lbrynet.dht import constants, kbucket
@ -214,7 +213,7 @@ class TreeRoutingTable:
now = int(self._getTime()) now = int(self._getTime())
for bucket in self._buckets[startIndex:]: for bucket in self._buckets[startIndex:]:
if force or now - bucket.lastAccessed >= constants.refreshTimeout: if force or now - bucket.lastAccessed >= constants.refreshTimeout:
searchID = self._randomIDInBucketRange(bucketIndex) searchID = self.midpoint_id_in_bucket_range(bucketIndex)
refreshIDs.append(searchID) refreshIDs.append(searchID)
bucketIndex += 1 bucketIndex += 1
return refreshIDs return refreshIDs
@ -262,22 +261,25 @@ class TreeRoutingTable:
i += 1 i += 1
return i return i
def _randomIDInBucketRange(self, bucketIndex): def random_id_in_bucket_range(self, bucketIndex):
""" Returns a random ID in the specified k-bucket's range """ Returns a random ID in the specified k-bucket's range
@param bucketIndex: The index of the k-bucket to use @param bucketIndex: The index of the k-bucket to use
@type bucketIndex: int @type bucketIndex: int
""" """
idValue = random.randrange(
self._buckets[bucketIndex].rangeMin, self._buckets[bucketIndex].rangeMax) random_id = int(random.randrange(self._buckets[bucketIndex].rangeMin, self._buckets[bucketIndex].rangeMax))
randomID = hex(idValue)[2:] return random_id.to_bytes(constants.key_bits // 8, 'big')
if randomID[-1] == 'L':
randomID = randomID[:-1] def midpoint_id_in_bucket_range(self, bucketIndex):
if len(randomID) % 2 != 0: """ Returns the middle ID in the specified k-bucket's range
randomID = '0' + randomID
randomID = unhexlify(randomID) @param bucketIndex: The index of the k-bucket to use
randomID = ((constants.key_bits // 8) - len(randomID)) * b'\x00' + randomID @type bucketIndex: int
return randomID """
half = int((self._buckets[bucketIndex].rangeMax - self._buckets[bucketIndex].rangeMin) // 2)
return int(self._buckets[bucketIndex].rangeMin + half).to_bytes(constants.key_bits // 8, 'big')
def _splitBucket(self, oldBucketIndex): def _splitBucket(self, oldBucketIndex):
""" Splits the specified k-bucket into two new buckets which together """ Splits the specified k-bucket into two new buckets which together