diff --git a/libp2p/host/defaults.py b/libp2p/host/defaults.py index a311132..a5d63ba 100644 --- a/libp2p/host/defaults.py +++ b/libp2p/host/defaults.py @@ -2,10 +2,16 @@ from collections import OrderedDict from typing import TYPE_CHECKING from libp2p.host.host_interface import IHost +from libp2p.host.ping import ID as PingID +from libp2p.host.ping import handle_ping +from libp2p.identity.identify.protocol import ID as IdentifyID +from libp2p.identity.identify.protocol import identify_handler_for if TYPE_CHECKING: from libp2p.typing import TProtocol, StreamHandlerFn def get_default_protocols(host: IHost) -> "OrderedDict[TProtocol, StreamHandlerFn]": - return OrderedDict() + return OrderedDict( + ((IdentifyID, identify_handler_for(host)), (PingID, handle_ping)) + ) diff --git a/libp2p/host/ping.py b/libp2p/host/ping.py index 46a28e0..9075f0b 100644 --- a/libp2p/host/ping.py +++ b/libp2p/host/ping.py @@ -4,8 +4,9 @@ import logging from libp2p.network.stream.exceptions import StreamEOF, StreamReset from libp2p.network.stream.net_stream_interface import INetStream from libp2p.peer.id import ID as PeerID +from libp2p.typing import TProtocol -ID = "/ipfs/ping/1.0.0" +ID = TProtocol("/ipfs/ping/1.0.0") PING_LENGTH = 32 RESP_TIMEOUT = 60 diff --git a/libp2p/identity/identify/protocol.py b/libp2p/identity/identify/protocol.py index b42e51a..390c0de 100644 --- a/libp2p/identity/identify/protocol.py +++ b/libp2p/identity/identify/protocol.py @@ -4,11 +4,11 @@ from multiaddr import Multiaddr from libp2p.host.host_interface import IHost from libp2p.network.stream.net_stream_interface import INetStream -from libp2p.typing import StreamHandlerFn +from libp2p.typing import StreamHandlerFn, TProtocol from .pb.identify_pb2 import Identify -ID = "/ipfs/id/1.0.0" +ID = TProtocol("/ipfs/id/1.0.0") PROTOCOL_VERSION = "ipfs/0.1.0" # TODO dynamically generate the agent version AGENT_VERSION = "py-libp2p/alpha" diff --git a/tests/host/test_basic_host.py b/tests/host/test_basic_host.py index 4d22222..f25f4e7 100644 --- a/tests/host/test_basic_host.py +++ b/tests/host/test_basic_host.py @@ -11,4 +11,6 @@ def test_default_protocols(): mux = host.get_mux() handlers = mux.handlers - assert handlers == get_default_protocols(host) + # NOTE: comparing keys for equality as handlers may be closures that do not compare in the way + # this test is concerned with + assert handlers.keys() == get_default_protocols(host).keys() diff --git a/tests/host/test_ping.py b/tests/host/test_ping.py index d6f0244..a5296f1 100644 --- a/tests/host/test_ping.py +++ b/tests/host/test_ping.py @@ -3,28 +3,16 @@ import secrets import pytest -from libp2p.host.ping import ID, PING_LENGTH, handle_ping +from libp2p.host.ping import ID, PING_LENGTH from libp2p.peer.peerinfo import info_from_p2p_addr from tests.utils import set_up_nodes_by_transport_opt -def _add_ping_to(host): - host.set_stream_handler(ID, handle_ping) - - @pytest.mark.asyncio async def test_ping_once(): transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) - # NOTE: this will fail after we add ping as a default handler - # as a forced reminder to fix this test by removing the calls to - # `_add_ping_to` - assert host_a.get_mux().handlers == {} - assert host_b.get_mux().handlers == {} - _add_ping_to(host_a) - _add_ping_to(host_b) - addr = host_a.get_addrs()[0] info = info_from_p2p_addr(addr) await host_b.connect(info) @@ -45,14 +33,6 @@ async def test_ping_several(): transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) - # NOTE: this will fail after we add ping as a default handler - # as a forced reminder to fix this test by removing the calls to - # `_add_ping_to` - assert host_a.get_mux().handlers == {} - assert host_b.get_mux().handlers == {} - _add_ping_to(host_a) - _add_ping_to(host_b) - addr = host_a.get_addrs()[0] info = info_from_p2p_addr(addr) await host_b.connect(info)