mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
refactor creation of identify response to make testing easier
This commit is contained in:
@ -19,24 +19,28 @@ def _multiaddr_to_bytes(maddr: Multiaddr) -> bytes:
|
|||||||
return maddr.to_bytes()
|
return maddr.to_bytes()
|
||||||
|
|
||||||
|
|
||||||
|
def _mk_identify_protobuf(host: IHost) -> Identify:
|
||||||
|
public_key = host.get_public_key()
|
||||||
|
laddrs = host.get_addrs()
|
||||||
|
protocols = host.get_mux().get_protocols()
|
||||||
|
|
||||||
|
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"",
|
||||||
|
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
|
peer_id = stream.muxed_conn.peer_id
|
||||||
logger.debug("received a request for %s from %s", ID, peer_id)
|
logger.debug("received a request for %s from %s", ID, peer_id)
|
||||||
|
|
||||||
public_key = host.get_public_key()
|
protobuf = _mk_identify_protobuf(host)
|
||||||
laddrs = host.get_addrs()
|
|
||||||
protocols = host.get_mux().get_protocols()
|
|
||||||
|
|
||||||
protobuf = 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"",
|
|
||||||
protocols=protocols,
|
|
||||||
)
|
|
||||||
response = protobuf.SerializeToString()
|
response = protobuf.SerializeToString()
|
||||||
|
|
||||||
await stream.write(response)
|
await stream.write(response)
|
||||||
|
|||||||
Reference in New Issue
Block a user