2020-01-07 14:14:34 +08:00
|
|
|
import anyio
|
|
|
|
from async_exit_stack import AsyncExitStack
|
2019-09-10 17:51:39 +08:00
|
|
|
from p2pclient.datastructures import StreamInfo
|
2020-01-07 14:14:34 +08:00
|
|
|
from p2pclient.utils import get_unused_tcp_port
|
2019-08-29 22:38:08 +08:00
|
|
|
import pytest
|
2020-01-07 14:14:34 +08:00
|
|
|
import trio
|
2019-08-29 22:38:08 +08:00
|
|
|
|
2019-09-10 17:51:39 +08:00
|
|
|
from libp2p.io.abc import ReadWriteCloser
|
2020-02-19 23:15:03 +08:00
|
|
|
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID
|
2020-02-26 16:07:01 +08:00
|
|
|
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
|
2020-01-07 14:14:34 +08:00
|
|
|
from libp2p.tools.factories import HostFactory, PubsubFactory
|
|
|
|
from libp2p.tools.interop.daemon import make_p2pd
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.tools.interop.utils import connect
|
2019-09-02 17:32:15 +08:00
|
|
|
|
2019-08-29 22:38:08 +08:00
|
|
|
|
2020-02-26 16:07:01 +08:00
|
|
|
@pytest.fixture(params=[PLAINTEXT_PROTOCOL_ID, NOISE_PROTOCOL_ID])
|
|
|
|
def security_protocol(request):
|
|
|
|
return request.param
|
2019-09-23 22:00:40 +08:00
|
|
|
|
|
|
|
|
2019-09-02 17:32:15 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def num_p2pds():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2019-09-03 16:49:00 +08:00
|
|
|
def is_gossipsub():
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2019-11-29 17:24:40 +08:00
|
|
|
def is_pubsub_signing():
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def is_pubsub_signing_strict():
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def p2pds(
|
2020-02-19 23:15:03 +08:00
|
|
|
num_p2pds,
|
|
|
|
security_protocol,
|
|
|
|
is_gossipsub,
|
|
|
|
is_pubsub_signing,
|
|
|
|
is_pubsub_signing_strict,
|
2019-11-29 17:24:40 +08:00
|
|
|
):
|
2020-01-07 14:14:34 +08:00
|
|
|
async with AsyncExitStack() as stack:
|
|
|
|
p2pds = [
|
|
|
|
await stack.enter_async_context(
|
|
|
|
make_p2pd(
|
|
|
|
get_unused_tcp_port(),
|
|
|
|
get_unused_tcp_port(),
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2020-01-07 14:14:34 +08:00
|
|
|
is_gossipsub=is_gossipsub,
|
|
|
|
is_pubsub_signing=is_pubsub_signing,
|
|
|
|
is_pubsub_signing_strict=is_pubsub_signing_strict,
|
|
|
|
)
|
2019-09-03 16:49:00 +08:00
|
|
|
)
|
|
|
|
for _ in range(num_p2pds)
|
2020-01-07 14:14:34 +08:00
|
|
|
]
|
|
|
|
try:
|
|
|
|
yield p2pds
|
|
|
|
finally:
|
|
|
|
for p2pd in p2pds:
|
|
|
|
await p2pd.close()
|
2019-09-03 16:49:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-19 23:15:03 +08:00
|
|
|
async def pubsubs(num_hosts, security_protocol, is_gossipsub, is_pubsub_signing_strict):
|
2019-09-03 16:49:00 +08:00
|
|
|
if is_gossipsub:
|
2020-01-07 14:14:34 +08:00
|
|
|
yield PubsubFactory.create_batch_with_gossipsub(
|
2020-02-19 23:15:03 +08:00
|
|
|
num_hosts,
|
|
|
|
security_protocol=security_protocol,
|
|
|
|
strict_signing=is_pubsub_signing_strict,
|
2020-01-07 14:14:34 +08:00
|
|
|
)
|
2019-09-03 16:49:00 +08:00
|
|
|
else:
|
2020-01-07 14:14:34 +08:00
|
|
|
yield PubsubFactory.create_batch_with_floodsub(
|
2020-02-19 23:15:03 +08:00
|
|
|
num_hosts, security_protocol, strict_signing=is_pubsub_signing_strict
|
2020-01-07 14:14:34 +08:00
|
|
|
)
|
2019-09-10 17:51:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DaemonStream(ReadWriteCloser):
|
|
|
|
stream_info: StreamInfo
|
2020-01-07 14:14:34 +08:00
|
|
|
stream: anyio.abc.SocketStream
|
|
|
|
|
|
|
|
def __init__(self, stream_info: StreamInfo, stream: anyio.abc.SocketStream) -> None:
|
2019-09-10 17:51:39 +08:00
|
|
|
self.stream_info = stream_info
|
2020-01-07 14:14:34 +08:00
|
|
|
self.stream = stream
|
2019-09-10 17:51:39 +08:00
|
|
|
|
|
|
|
async def close(self) -> None:
|
2020-01-07 14:14:34 +08:00
|
|
|
await self.stream.close()
|
2019-09-10 17:51:39 +08:00
|
|
|
|
2020-01-26 23:03:38 +08:00
|
|
|
async def read(self, n: int = None) -> bytes:
|
|
|
|
return await self.stream.receive_some(n)
|
2019-09-10 17:51:39 +08:00
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
async def write(self, data: bytes) -> None:
|
|
|
|
return await self.stream.send_all(data)
|
2019-09-10 17:51:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def is_to_fail_daemon_stream():
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-02-19 23:15:03 +08:00
|
|
|
async def py_to_daemon_stream_pair(p2pds, security_protocol, is_to_fail_daemon_stream):
|
|
|
|
async with HostFactory.create_batch_and_listen(
|
|
|
|
1, security_protocol=security_protocol
|
|
|
|
) as hosts:
|
2020-01-07 14:14:34 +08:00
|
|
|
assert len(p2pds) >= 1
|
|
|
|
host = hosts[0]
|
|
|
|
p2pd = p2pds[0]
|
|
|
|
protocol_id = "/protocol/id/123"
|
|
|
|
stream_py = None
|
|
|
|
stream_daemon = None
|
|
|
|
event_stream_handled = trio.Event()
|
|
|
|
await connect(host, p2pd)
|
|
|
|
|
|
|
|
async def daemon_stream_handler(stream_info, stream):
|
|
|
|
nonlocal stream_daemon
|
|
|
|
stream_daemon = DaemonStream(stream_info, stream)
|
|
|
|
event_stream_handled.set()
|
|
|
|
await trio.hazmat.checkpoint()
|
|
|
|
|
|
|
|
await p2pd.control.stream_handler(protocol_id, daemon_stream_handler)
|
|
|
|
# Sleep for a while to wait for the handler being registered.
|
|
|
|
await trio.sleep(0.01)
|
|
|
|
|
|
|
|
if is_to_fail_daemon_stream:
|
|
|
|
# FIXME: This is a workaround to make daemon reset the stream.
|
|
|
|
# We intentionally close the listener on the python side, it makes the connection from
|
|
|
|
# daemon to us fail, and therefore the daemon resets the opened stream on their side.
|
|
|
|
# Reference: https://github.com/libp2p/go-libp2p-daemon/blob/b95e77dbfcd186ccf817f51e95f73f9fd5982600/stream.go#L47-L50 # noqa: E501
|
|
|
|
# We need it because we want to test against `stream_py` after the remote side(daemon)
|
|
|
|
# is reset. This should be removed after the API `stream.reset` is exposed in daemon
|
|
|
|
# some day.
|
|
|
|
await p2pds[0].control.control.close()
|
|
|
|
stream_py = await host.new_stream(p2pd.peer_id, [protocol_id])
|
|
|
|
if not is_to_fail_daemon_stream:
|
|
|
|
await event_stream_handled.wait()
|
|
|
|
# NOTE: If `is_to_fail_daemon_stream == True`, then `stream_daemon == None`.
|
|
|
|
yield stream_py, stream_daemon
|