From 91a9957918d21f5a991e66302c0488470ed7ec88 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 1 Oct 2019 18:02:23 -0300 Subject: [PATCH] avoid peer mutability Mutability can be harmful here, specially on fields used for __hash__ It can also make the cache less efficient --- lbry/lbry/dht/node.py | 2 +- lbry/lbry/dht/peer.py | 11 ++--------- lbry/lbry/dht/protocol/protocol.py | 3 +-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/lbry/lbry/dht/node.py b/lbry/lbry/dht/node.py index af2b05003..152ff0b57 100644 --- a/lbry/lbry/dht/node.py +++ b/lbry/lbry/dht/node.py @@ -233,7 +233,7 @@ class Node: if not peer.udp_port: udp_port_to_try = peer.tcp_port if not peer.udp_port: - peer.update_udp_port(udp_port_to_try) + peer = make_kademlia_peer(peer.node_id, peer.address, udp_port_to_try, peer.tcp_port) self.loop.create_task(ping(peer)) else: log.debug("skip bad peer %s:%i for %s", peer.address, peer.tcp_port, blob_hash) diff --git a/lbry/lbry/dht/peer.py b/lbry/lbry/dht/peer.py index 54fcfa12f..2eddcbfe7 100644 --- a/lbry/lbry/dht/peer.py +++ b/lbry/lbry/dht/peer.py @@ -147,8 +147,8 @@ class KademliaPeer: address: str = field(hash=True) _node_id: typing.Optional[bytes] = field(hash=True) udp_port: typing.Optional[int] = field(hash=True) - tcp_port: typing.Optional[int] = field(compare=False) - protocol_version: typing.Optional[int] = field(default=1, compare=False) + tcp_port: typing.Optional[int] = field(compare=False, hash=False) + protocol_version: typing.Optional[int] = field(default=1, compare=False, hash=False) def __post_init__(self): if self._node_id is not None: @@ -164,13 +164,6 @@ class KademliaPeer: def update_tcp_port(self, tcp_port: int): self.tcp_port = tcp_port - def update_udp_port(self, udp_port: int): - self.udp_port = udp_port - - def set_id(self, node_id): - if not self._node_id: - self._node_id = node_id - @property def node_id(self) -> bytes: return self._node_id diff --git a/lbry/lbry/dht/protocol/protocol.py b/lbry/lbry/dht/protocol/protocol.py index 895f1d26c..2eecc5893 100644 --- a/lbry/lbry/dht/protocol/protocol.py +++ b/lbry/lbry/dht/protocol/protocol.py @@ -495,8 +495,7 @@ class KademliaProtocol(DatagramProtocol): elif response_datagram.node_id == self.node_id: df.set_exception(RemoteException("incoming message is from our node id")) return - peer.set_id(response_datagram.node_id) - peer.update_udp_port(address[1]) + peer = make_kademlia_peer(response_datagram.node_id, address[0], address[1]) self.peer_manager.report_last_replied(address[0], address[1]) self.peer_manager.update_contact_triple(peer.node_id, address[0], address[1]) if not df.cancelled():