From 2a15861edeb40ff2603d4385057b0316ad37a746 Mon Sep 17 00:00:00 2001 From: Jan Segre Date: Tue, 23 Jul 2024 23:34:14 +0200 Subject: [PATCH] fix(p2p): status regression after entrypoint refactor --- hathor/p2p/peer_id.py | 9 ++++++--- hathor/p2p/resources/status.py | 4 ++-- hathor/p2p/states/peer_id.py | 2 +- hathor/p2p/states/ready.py | 2 +- tests/p2p/test_protocol.py | 22 +++++++++++++++++++++- tests/resources/p2p/test_status.py | 4 ++++ 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/hathor/p2p/peer_id.py b/hathor/p2p/peer_id.py index 23b0f0c70..d2aef2634 100644 --- a/hathor/p2p/peer_id.py +++ b/hathor/p2p/peer_id.py @@ -91,12 +91,15 @@ def __init__(self, auto_generate_keys: bool = True) -> None: self.generate_keys() def __str__(self): - entrypoints = [str(entrypoint) for entrypoint in self.entrypoints] return ( - f'PeerId(id={self.id}, entrypoints={entrypoints}, retry_timestamp={self.retry_timestamp}, ' + f'PeerId(id={self.id}, entrypoints={self.entrypoints_as_str()}, retry_timestamp={self.retry_timestamp}, ' f'retry_interval={self.retry_interval})' ) + def entrypoints_as_str(self) -> list[str]: + """Return a list of entrypoints serialized as str""" + return list(map(str, self.entrypoints)) + def merge(self, other: 'PeerId') -> None: """ Merge two PeerId objects, checking that they have the same id, public_key, and private_key. The entrypoints are merged without @@ -251,7 +254,7 @@ def to_json(self, include_private_key: bool = False) -> dict[str, Any]: result = { 'id': self.id, 'pubKey': base64.b64encode(public_der).decode('utf-8'), - 'entrypoints': [str(ep) for ep in self.entrypoints], + 'entrypoints': self.entrypoints_as_str(), } if include_private_key: assert self.private_key is not None diff --git a/hathor/p2p/resources/status.py b/hathor/p2p/resources/status.py index 544484f46..225665930 100644 --- a/hathor/p2p/resources/status.py +++ b/hathor/p2p/resources/status.py @@ -83,7 +83,7 @@ def render_GET(self, request): for peer in self.manager.connections.peer_storage.values(): known_peers.append({ 'id': peer.id, - 'entrypoints': peer.entrypoints, + 'entrypoints': peer.entrypoints_as_str(), 'last_seen': now - peer.last_seen, 'flags': [flag.value for flag in peer.flags], }) @@ -107,7 +107,7 @@ def render_GET(self, request): 'state': self.manager.state.value, 'network': self.manager.network, 'uptime': now - self.manager.start_time, - 'entrypoints': self.manager.connections.my_peer.entrypoints, + 'entrypoints': self.manager.connections.my_peer.entrypoints_as_str(), }, 'peers_whitelist': self.manager.peers_whitelist, 'known_peers': known_peers, diff --git a/hathor/p2p/states/peer_id.py b/hathor/p2p/states/peer_id.py index 525303adf..8d68b669b 100644 --- a/hathor/p2p/states/peer_id.py +++ b/hathor/p2p/states/peer_id.py @@ -70,7 +70,7 @@ def send_peer_id(self) -> None: hello = { 'id': my_peer.id, 'pubKey': my_peer.get_public_key(), - 'entrypoints': my_peer.entrypoints, + 'entrypoints': my_peer.entrypoints_as_str(), } self.send_message(ProtocolMessages.PEER_ID, json_dumps(hello)) diff --git a/hathor/p2p/states/ready.py b/hathor/p2p/states/ready.py index 57a01c6d4..35802b877 100644 --- a/hathor/p2p/states/ready.py +++ b/hathor/p2p/states/ready.py @@ -166,7 +166,7 @@ def send_peers(self, peer_list: Iterable['PeerId']) -> None: if peer.entrypoints: data.append({ 'id': peer.id, - 'entrypoints': [str(entrypoint) for entrypoint in peer.entrypoints], + 'entrypoints': peer.entrypoints_as_str(), }) self.send_message(ProtocolMessages.PEERS, json_dumps(data)) self.log.debug('send peers', peers=data) diff --git a/tests/p2p/test_protocol.py b/tests/p2p/test_protocol.py index a834f9e20..317675c81 100644 --- a/tests/p2p/test_protocol.py +++ b/tests/p2p/test_protocol.py @@ -5,10 +5,11 @@ from twisted.internet.protocol import Protocol from twisted.python.failure import Failure +from hathor.p2p.entrypoint import Entrypoint from hathor.p2p.peer_id import PeerId from hathor.p2p.protocol import HathorLineReceiver, HathorProtocol from hathor.simulator import FakeConnection -from hathor.util import json_dumps +from hathor.util import json_dumps, json_loadb from tests import unittest @@ -69,6 +70,25 @@ def _check_cmd_and_value(self, result: bytes, expected: tuple[bytes, bytes]) -> def test_on_connect(self) -> None: self._check_result_only_cmd(self.conn.peek_tr1_value(), b'HELLO') + def test_peer_id_with_entrypoint(self) -> None: + entrypoint_str = 'tcp://192.168.1.1:54321' + entrypoint = Entrypoint.parse(entrypoint_str) + self.peer_id1.entrypoints.append(entrypoint) + self.peer_id2.entrypoints.append(entrypoint) + self.conn.run_one_step() # HELLO + + msg1 = self.conn.peek_tr1_value() + cmd1, val1 = msg1.split(b' ', 1) + data1 = json_loadb(val1) + self.assertEqual(cmd1, b'PEER-ID') + self.assertEqual(data1['entrypoints'], [entrypoint_str]) + + msg2 = self.conn.peek_tr2_value() + cmd2, val2 = msg2.split(b' ', 1) + data2 = json_loadb(val2) + self.assertEqual(cmd2, b'PEER-ID') + self.assertEqual(data2['entrypoints'], [entrypoint_str]) + def test_invalid_command(self) -> None: self._send_cmd(self.conn.proto1, 'INVALID-CMD') self.conn.proto1.state.handle_error('') diff --git a/tests/resources/p2p/test_status.py b/tests/resources/p2p/test_status.py index ea80ece6e..7ab42ae68 100644 --- a/tests/resources/p2p/test_status.py +++ b/tests/resources/p2p/test_status.py @@ -3,6 +3,7 @@ import hathor from hathor.conf.unittests import SETTINGS +from hathor.p2p.entrypoint import Entrypoint from hathor.p2p.resources import StatusResource from hathor.simulator import FakeConnection from tests import unittest @@ -15,8 +16,11 @@ class BaseStatusTest(_BaseResourceTest._ResourceTest): def setUp(self): super().setUp() self.web = StubSite(StatusResource(self.manager)) + self.entrypoint = Entrypoint.parse('tcp://192.168.1.1:54321') + self.manager.connections.my_peer.entrypoints.append(self.entrypoint) self.manager2 = self.create_peer('testnet') + self.manager2.connections.my_peer.entrypoints.append(self.entrypoint) self.conn1 = FakeConnection(self.manager, self.manager2) @inlineCallbacks