From 3b88d2465dd0db8dd473b410f90a020f67a93328 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 10 Aug 2018 20:41:08 -0300 Subject: [PATCH] fixes and refactors from review --- lbrynet/core/client/ConnectionManager.py | 3 ++- lbrynet/daemon/Daemon.py | 33 +++++++----------------- lbrynet/dht/iterativefind.py | 2 +- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/lbrynet/core/client/ConnectionManager.py b/lbrynet/core/client/ConnectionManager.py index 9f2cafdbf..f202922a5 100644 --- a/lbrynet/core/client/ConnectionManager.py +++ b/lbrynet/core/client/ConnectionManager.py @@ -96,7 +96,8 @@ class ConnectionManager: d.addBoth(lambda _: disconnect_peer(p)) return d - closing_deferreds = [close_connection(peer) for peer in list(self._peer_connections.keys())] + # fixme: stop modifying dict during iteration + closing_deferreds = [close_connection(peer) for peer in list(self._peer_connections)] return defer.DeferredList(closing_deferreds) @defer.inlineCallbacks diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index c12f78360..9169b29f3 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -2895,46 +2895,31 @@ class Daemon(AuthJSONRPCServer): """ result = {} data_store = self.dht_node._dataStore - datastore_len = len(data_store) hosts = {} - if datastore_len: - for k, v in data_store.items(): - for contact, value, lastPublished, originallyPublished, originalPublisherID in v: - if contact in hosts: - blobs = hosts[contact] - else: - blobs = [] - blobs.append(hexlify(k)) - hosts[contact] = blobs + for k, v in data_store.items(): + for contact, _ in v: + hosts.setdefault(contact, []).append(hexlify(k).decode()) contact_set = set() - blob_hashes = [] + blob_hashes = set() result['buckets'] = {} for i in range(len(self.dht_node._routingTable._buckets)): for contact in self.dht_node._routingTable._buckets[i]._contacts: - contacts = result['buckets'].get(i, []) - if contact in hosts: - blobs = hosts[contact] - del hosts[contact] - else: - blobs = [] + blobs = [hexlify(raw_hash).decode() for raw_hash in hosts.pop(contact)] if contact in hosts else [] + blob_hashes.update(blobs) host = { "address": contact.address, "port": contact.port, "node_id": hexlify(contact.id).decode(), "blobs": blobs, } - for blob_hash in blobs: - if blob_hash not in blob_hashes: - blob_hashes.append(blob_hash) - contacts.append(host) - result['buckets'][i] = contacts + result['buckets'].setdefault(i, []).append(host) contact_set.add(hexlify(contact.id).decode()) - result['contacts'] = contact_set - result['blob_hashes'] = blob_hashes + result['contacts'] = list(contact_set) + result['blob_hashes'] = list(blob_hashes) result['node_id'] = hexlify(self.dht_node.node_id).decode() return self._render_response(result) diff --git a/lbrynet/dht/iterativefind.py b/lbrynet/dht/iterativefind.py index 218d5a8f5..26608ead6 100644 --- a/lbrynet/dht/iterativefind.py +++ b/lbrynet/dht/iterativefind.py @@ -16,7 +16,7 @@ def get_contact(contact_list, node_id, address, port): def expand_peer(compact_peer_info): - host = ".".join([str(d) for d in compact_peer_info[:4]]) + host = "{}.{}.{}.{}".format(*compact_peer_info[:4]) port, = struct.unpack('>H', compact_peer_info[4:6]) peer_node_id = compact_peer_info[6:] return (peer_node_id, host, port)