Skip to content

fix(p2p): status regression after entrypoint refactor #1089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions hathor/p2p/peer_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions hathor/p2p/resources/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion hathor/p2p/states/peer_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion hathor/p2p/states/ready.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion tests/p2p/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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('')
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/p2p/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading