Merge pull request #345 from ralexstokes/install-default-protocols

Install default protocols
This commit is contained in:
Alex Stokes 2019-11-08 00:05:05 +08:00 committed by GitHub
commit 071eccc995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 26 deletions

View File

@ -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))
)

View File

@ -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

View File

@ -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"

View File

@ -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()

View File

@ -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)