diff --git a/lbry/lbry/dht/peer.py b/lbry/lbry/dht/peer.py index 2eddcbfe7..83a26f65e 100644 --- a/lbry/lbry/dht/peer.py +++ b/lbry/lbry/dht/peer.py @@ -120,6 +120,8 @@ class PeerManager: previous_failure, most_recent_failure = self._rpc_failures.get((address, udp_port), (None, None)) last_requested = self._last_requested.get((address, udp_port)) last_replied = self._last_replied.get((address, udp_port)) + if node_id is None: + return None if most_recent_failure and last_replied: if delay < last_replied > most_recent_failure: return True diff --git a/lbry/tests/unit/dht/test_peer.py b/lbry/tests/unit/dht/test_peer.py index 4afd77b8e..a82a1a484 100644 --- a/lbry/tests/unit/dht/test_peer.py +++ b/lbry/tests/unit/dht/test_peer.py @@ -13,6 +13,16 @@ class PeerTest(AsyncioTestCase): self.first_contact = make_kademlia_peer(self.node_ids[1], '127.0.0.1', udp_port=1000) self.second_contact = make_kademlia_peer(self.node_ids[0], '192.168.0.1', udp_port=1000) + def test_peer_is_good_unknown_peer(self): + # Scenario: peer replied, but caller doesn't know the node_id. + # Outcome: We can't say it's good or bad. + # (yes, we COULD tell the node id, but not here. It would be + # a side effect and the caller is responsible to discover it) + peer = make_kademlia_peer(None, '1.2.3.4', 4444) + self.peer_manager.report_last_requested('1.2.3.4', 4444) + self.peer_manager.report_last_replied('1.2.3.4', 4444) + self.assertIsNone(self.peer_manager.peer_is_good(peer)) + def test_make_contact_error_cases(self): self.assertRaises(ValueError, make_kademlia_peer, self.node_ids[1], '192.168.1.20', 100000) self.assertRaises(ValueError, make_kademlia_peer, self.node_ids[1], '192.168.1.20.1', 1000)