fixes and refactors from review

This commit is contained in:
Victor Shyba 2018-08-10 20:41:08 -03:00 committed by Jack Robison
parent 0841c90e6c
commit 3b88d2465d
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 12 additions and 26 deletions

View file

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

View file

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

View file

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