mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-09 22:50:54 +00:00
feat: add observed_addr to identify protocol
This commit is contained in:
@ -1131,6 +1131,12 @@ class IHost(ABC):
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_peerstore(self) -> IPeerStore:
|
||||
"""
|
||||
:return: the peerstore of the host
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_connected_peers(self) -> list[ID]:
|
||||
"""
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
import logging
|
||||
from typing import (
|
||||
Optional,
|
||||
)
|
||||
|
||||
from multiaddr import (
|
||||
Multiaddr,
|
||||
@ -15,9 +18,6 @@ from libp2p.custom_types import (
|
||||
from libp2p.network.stream.exceptions import (
|
||||
StreamClosed,
|
||||
)
|
||||
from libp2p.network.stream.net_stream_interface import (
|
||||
INetStream,
|
||||
)
|
||||
from libp2p.utils import (
|
||||
get_agent_version,
|
||||
)
|
||||
@ -26,7 +26,9 @@ from .pb.identify_pb2 import (
|
||||
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")
|
||||
PROTOCOL_VERSION = "ipfs/0.1.0"
|
||||
@ -37,28 +39,45 @@ def _multiaddr_to_bytes(maddr: Multiaddr) -> 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()
|
||||
laddrs = host.get_addrs()
|
||||
protocols = host.get_mux().get_protocols()
|
||||
|
||||
observed_addr = observed_multiaddr.to_bytes() if observed_multiaddr else b""
|
||||
return Identify(
|
||||
protocol_version=PROTOCOL_VERSION,
|
||||
agent_version=AGENT_VERSION,
|
||||
public_key=public_key.serialize(),
|
||||
listen_addrs=map(_multiaddr_to_bytes, laddrs),
|
||||
# TODO send observed address from ``stream``
|
||||
observed_addr=b"",
|
||||
observed_addr=observed_addr,
|
||||
protocols=protocols,
|
||||
)
|
||||
|
||||
|
||||
def identify_handler_for(host: IHost) -> StreamHandlerFn:
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user