Merge branch 'populate-buckets'

This commit is contained in:
Jack Robison 2018-11-16 11:44:25 -05:00
commit 0a8d44f12f
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 21 additions and 18 deletions

View file

@ -216,10 +216,10 @@ class Node(MockKademliaHelper):
# find the closest peers to us # find the closest peers to us
closest = yield self._iterativeFind(self.node_id, shortlist if not self.contacts else None) closest = yield self._iterativeFind(self.node_id, shortlist if not self.contacts else None)
yield _ping_contacts(closest) yield _ping_contacts(closest)
# # query random hashes in our bucket key ranges to fill or split them # query random hashes in our bucket key ranges to fill or split them
# random_ids_in_range = self._routingTable.getRefreshList() random_ids_in_range = self._routingTable.getRefreshList()
# while random_ids_in_range: while random_ids_in_range:
# yield self.iterativeFindNode(random_ids_in_range.pop()) yield self.iterativeFindNode(random_ids_in_range.pop())
defer.returnValue(None) defer.returnValue(None)
@defer.inlineCallbacks @defer.inlineCallbacks

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

View file

@ -3114,6 +3114,7 @@ class Daemon(AuthJSONRPCServer):
result['buckets'] = {} result['buckets'] = {}
for i in range(len(self.dht_node._routingTable._buckets)): for i in range(len(self.dht_node._routingTable._buckets)):
result['buckets'][i] = []
for contact in self.dht_node._routingTable._buckets[i]._contacts: for contact in self.dht_node._routingTable._buckets[i]._contacts:
blobs = list(hosts.pop(contact)) if contact in hosts else [] blobs = list(hosts.pop(contact)) if contact in hosts else []
blob_hashes.update(blobs) blob_hashes.update(blobs)
@ -3123,7 +3124,7 @@ class Daemon(AuthJSONRPCServer):
"node_id": hexlify(contact.id).decode(), "node_id": hexlify(contact.id).decode(),
"blobs": blobs, "blobs": blobs,
} }
result['buckets'].setdefault(i, []).append(host) result['buckets'][i].append(host)
contact_set.add(hexlify(contact.id).decode()) contact_set.add(hexlify(contact.id).decode())
result['contacts'] = list(contact_set) result['contacts'] = list(contact_set)