From d877ba247580dfd3a44bbfb4b7b14d6e2fda8ab2 Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Fri, 16 Jun 2017 00:40:10 +0300 Subject: [PATCH] eliminate unnecessary calls to __decode_nodes in __on_FIND_NODE_resp. fixes #102, thanks @Glandos! --- magneticod/magneticod/dht.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/magneticod/magneticod/dht.py b/magneticod/magneticod/dht.py index d914d94..92c305a 100644 --- a/magneticod/magneticod/dht.py +++ b/magneticod/magneticod/dht.py @@ -153,6 +153,11 @@ class SybilNode(asyncio.DatagramProtocol): self._transport.close() def __on_FIND_NODE_response(self, message: bencode.KRPCDict) -> None: # pylint: disable=invalid-name + # Well, we are not really interested in your response if our routing table is already full; sorry. + # (Thanks to Glandos@GitHub for the heads up!) + if len(self._routing_table) >= self.__n_max_neighbours: + return + try: nodes_arg = message[b"r"][b"nodes"] assert type(nodes_arg) is bytes and len(nodes_arg) % 26 == 0 @@ -164,12 +169,8 @@ class SybilNode(asyncio.DatagramProtocol): except AssertionError: return - # Ignore nodes with port 0. - nodes = [n for n in nodes if n[1][1] != 0] - - # Add new found nodes to the routing table, assuring that we have no more than n_max_neighbours in total. - if len(self._routing_table) < self.__n_max_neighbours: - self._routing_table.update(nodes[:self.__n_max_neighbours - len(self._routing_table)]) + nodes = [n for n in nodes if n[1][1] != 0] # Ignore nodes with port 0. + self._routing_table.update(nodes[:self.__n_max_neighbours - len(self._routing_table)]) def __on_GET_PEERS_query(self, message: bencode.KRPCDict, addr: NodeAddress) -> None: # pylint: disable=invalid-name try: