feat: add observed_addr to identify protocol

This commit is contained in:
acul71
2025-02-25 04:58:47 +01:00
committed by Paul Robinson
parent bd8bd953ec
commit 798229cd3a
3 changed files with 60 additions and 13 deletions

View File

@ -1131,6 +1131,12 @@ class IHost(ABC):
""" """
@abstractmethod
def get_peerstore(self) -> IPeerStore:
"""
:return: the peerstore of the host
"""
@abstractmethod @abstractmethod
def get_connected_peers(self) -> list[ID]: def get_connected_peers(self) -> list[ID]:
""" """

View File

@ -1,4 +1,7 @@
import logging import logging
from typing import (
Optional,
)
from multiaddr import ( from multiaddr import (
Multiaddr, Multiaddr,
@ -15,9 +18,6 @@ from libp2p.custom_types import (
from libp2p.network.stream.exceptions import ( from libp2p.network.stream.exceptions import (
StreamClosed, StreamClosed,
) )
from libp2p.network.stream.net_stream_interface import (
INetStream,
)
from libp2p.utils import ( from libp2p.utils import (
get_agent_version, get_agent_version,
) )
@ -26,7 +26,9 @@ from .pb.identify_pb2 import (
Identify, Identify,
) )
logger = logging.getLogger("libp2p.identity.identify") # Not sure I can do this or I break a pattern
# logger = logging.getLogger("libp2p.identity.identify")
logger = logging.getLogger(__name__)
ID = TProtocol("/ipfs/id/1.0.0") ID = TProtocol("/ipfs/id/1.0.0")
PROTOCOL_VERSION = "ipfs/0.1.0" PROTOCOL_VERSION = "ipfs/0.1.0"
@ -37,28 +39,45 @@ def _multiaddr_to_bytes(maddr: Multiaddr) -> bytes:
return maddr.to_bytes() return maddr.to_bytes()
def _mk_identify_protobuf(host: IHost) -> Identify: def _mk_identify_protobuf(
host: IHost, observed_multiaddr: Optional[Multiaddr]
) -> Identify:
public_key = host.get_public_key() public_key = host.get_public_key()
laddrs = host.get_addrs() laddrs = host.get_addrs()
protocols = host.get_mux().get_protocols() protocols = host.get_mux().get_protocols()
observed_addr = observed_multiaddr.to_bytes() if observed_multiaddr else b""
return Identify( return Identify(
protocol_version=PROTOCOL_VERSION, protocol_version=PROTOCOL_VERSION,
agent_version=AGENT_VERSION, agent_version=AGENT_VERSION,
public_key=public_key.serialize(), public_key=public_key.serialize(),
listen_addrs=map(_multiaddr_to_bytes, laddrs), listen_addrs=map(_multiaddr_to_bytes, laddrs),
# TODO send observed address from ``stream`` observed_addr=observed_addr,
observed_addr=b"",
protocols=protocols, protocols=protocols,
) )
def identify_handler_for(host: IHost) -> StreamHandlerFn: def identify_handler_for(host: IHost) -> StreamHandlerFn:
async def handle_identify(stream: INetStream) -> None: async def handle_identify(stream: INetStream) -> None:
peer_id = stream.muxed_conn.peer_id # get observed address from ``stream``
# class Swarm(Service, INetworkService):
# TODO: Connection and `peer_id` are 1-1 mapping in our implementation,
# whereas in Go one `peer_id` may point to multiple connections.
# connections: dict[ID, INetConn]
# Luca: So I'm assuming that the connection is 1-1 mapping for now
peer_id = stream.muxed_conn.peer_id # remote peer_id
peer_store = host.get_peerstore() # get the peer store from the host
remote_peer_multiaddrs = peer_store.addrs(
peer_id
) # get the Multiaddrs for the remote peer_id
logger.debug("multiaddrs of remote peer is %s", remote_peer_multiaddrs)
logger.debug("received a request for %s from %s", ID, peer_id) logger.debug("received a request for %s from %s", ID, peer_id)
protobuf = _mk_identify_protobuf(host) # Select the first address if available, else None
observed_multiaddr = (
remote_peer_multiaddrs[0] if remote_peer_multiaddrs else None
)
protobuf = _mk_identify_protobuf(host, observed_multiaddr)
response = protobuf.SerializeToString() response = protobuf.SerializeToString()
try: try:

View File

@ -1,4 +1,9 @@
import logging
import pytest import pytest
from multiaddr import (
Multiaddr,
)
from libp2p.identity.identify.pb.identify_pb2 import ( from libp2p.identity.identify.pb.identify_pb2 import (
Identify, Identify,
@ -14,6 +19,8 @@ from tests.factories import (
host_pair_factory, host_pair_factory,
) )
logger = logging.getLogger("libp2p.identity.identify-test")
@pytest.mark.trio @pytest.mark.trio
async def test_identify_protocol(security_protocol): async def test_identify_protocol(security_protocol):
@ -28,8 +35,8 @@ async def test_identify_protocol(security_protocol):
identify_response = Identify() identify_response = Identify()
identify_response.ParseFromString(response) identify_response.ParseFromString(response)
# sanity check logger.debug("host_a: %s", host_a.get_addrs())
assert identify_response == _mk_identify_protobuf(host_a) logger.debug("host_b: %s", host_b.get_addrs())
# Check protocol version # Check protocol version
assert identify_response.protocol_version == PROTOCOL_VERSION assert identify_response.protocol_version == PROTOCOL_VERSION
@ -45,8 +52,23 @@ async def test_identify_protocol(security_protocol):
map(_multiaddr_to_bytes, host_a.get_addrs()) map(_multiaddr_to_bytes, host_a.get_addrs())
) )
# TODO: Check observed address # Check observed address
# assert identify_response.observed_addr == host_b.get_addrs()[0] host_b_addr = host_b.get_addrs()[0]
cleaned_addr = Multiaddr.join(
*(
host_b_addr.split()[:-1]
if str(host_b_addr.split()[-1]).startswith("/p2p/")
else host_b_addr.split()
)
)
logger.debug("observed_addr: %s", Multiaddr(identify_response.observed_addr))
logger.debug("host_b.get_addrs()[0]: %s", host_b.get_addrs()[0])
logger.debug("cleaned_addr= %s", cleaned_addr)
assert identify_response.observed_addr == _multiaddr_to_bytes(cleaned_addr)
# Check protocols # Check protocols
assert set(identify_response.protocols) == set(host_a.get_mux().get_protocols()) assert set(identify_response.protocols) == set(host_a.get_mux().get_protocols())
# sanity check
assert identify_response == _mk_identify_protobuf(host_a, cleaned_addr)