From 6dbb00d8d5594043c191702ade978d10c82b6c30 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 13 Nov 2018 09:32:56 -0500 Subject: [PATCH 1/3] populate buckets when joining the dht --- lbrynet/dht/node.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lbrynet/dht/node.py b/lbrynet/dht/node.py index fc5618d05..8fc590cb5 100644 --- a/lbrynet/dht/node.py +++ b/lbrynet/dht/node.py @@ -216,10 +216,10 @@ class Node(MockKademliaHelper): # find the closest peers to us closest = yield self._iterativeFind(self.node_id, shortlist if not self.contacts else None) yield _ping_contacts(closest) - # # query random hashes in our bucket key ranges to fill or split them - # random_ids_in_range = self._routingTable.getRefreshList() - # while random_ids_in_range: - # yield self.iterativeFindNode(random_ids_in_range.pop()) + # query random hashes in our bucket key ranges to fill or split them + random_ids_in_range = self._routingTable.getRefreshList() + while random_ids_in_range: + yield self.iterativeFindNode(random_ids_in_range.pop()) defer.returnValue(None) @defer.inlineCallbacks From 9bb91b126da48eb1505d40d669427b931e2f0198 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 13 Nov 2018 09:36:38 -0500 Subject: [PATCH 2/3] use midpoint id instead of random for refresh list --- lbrynet/dht/routingtable.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lbrynet/dht/routingtable.py b/lbrynet/dht/routingtable.py index dc8ff8089..c99003d33 100644 --- a/lbrynet/dht/routingtable.py +++ b/lbrynet/dht/routingtable.py @@ -7,7 +7,6 @@ import random import logging -from binascii import unhexlify from twisted.internet import defer from lbrynet.dht import constants, kbucket @@ -214,7 +213,7 @@ class TreeRoutingTable: now = int(self._getTime()) for bucket in self._buckets[startIndex:]: if force or now - bucket.lastAccessed >= constants.refreshTimeout: - searchID = self._randomIDInBucketRange(bucketIndex) + searchID = self.midpoint_id_in_bucket_range(bucketIndex) refreshIDs.append(searchID) bucketIndex += 1 return refreshIDs @@ -262,22 +261,25 @@ class TreeRoutingTable: i += 1 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 @param bucketIndex: The index of the k-bucket to use @type bucketIndex: int """ - idValue = random.randrange( - self._buckets[bucketIndex].rangeMin, self._buckets[bucketIndex].rangeMax) - randomID = hex(idValue)[2:] - if randomID[-1] == 'L': - randomID = randomID[:-1] - if len(randomID) % 2 != 0: - randomID = '0' + randomID - randomID = unhexlify(randomID) - randomID = ((constants.key_bits // 8) - len(randomID)) * b'\x00' + randomID - return randomID + + random_id = int(random.randrange(self._buckets[bucketIndex].rangeMin, self._buckets[bucketIndex].rangeMax)) + return random_id.to_bytes(constants.key_bits // 8, 'big') + + def midpoint_id_in_bucket_range(self, bucketIndex): + """ Returns the middle ID in the specified k-bucket's range + + @param bucketIndex: The index of the k-bucket to use + @type bucketIndex: int + """ + + 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): """ Splits the specified k-bucket into two new buckets which together From b0718d295e233b76bb47090f8a4e1ecc8ee9ff54 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 13 Nov 2018 09:36:52 -0500 Subject: [PATCH 3/3] fix empty buckets in routing_table_get --- lbrynet/extras/daemon/Daemon.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lbrynet/extras/daemon/Daemon.py b/lbrynet/extras/daemon/Daemon.py index 71e17c0c1..31dbc3c48 100644 --- a/lbrynet/extras/daemon/Daemon.py +++ b/lbrynet/extras/daemon/Daemon.py @@ -3114,6 +3114,7 @@ class Daemon(AuthJSONRPCServer): result['buckets'] = {} for i in range(len(self.dht_node._routingTable._buckets)): + result['buckets'][i] = [] for contact in self.dht_node._routingTable._buckets[i]._contacts: blobs = list(hosts.pop(contact)) if contact in hosts else [] blob_hashes.update(blobs) @@ -3123,7 +3124,7 @@ class Daemon(AuthJSONRPCServer): "node_id": hexlify(contact.id).decode(), "blobs": blobs, } - result['buckets'].setdefault(i, []).append(host) + result['buckets'][i].append(host) contact_set.add(hexlify(contact.id).decode()) result['contacts'] = list(contact_set)