File size: 4,728 Bytes
f0f4f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from contextlib import (
    AsyncExitStack,
)

import anyio
from p2pclient.datastructures import (
    StreamInfo,
)
from p2pclient.utils import (
    get_unused_tcp_port,
)
import pytest
import trio

from libp2p.io.abc import (
    ReadWriteCloser,
)
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
from libp2p.security.secio.transport import ID as SECIO_PROTOCOL_ID
from libp2p.tools.factories import (
    HostFactory,
    PubsubFactory,
)
from libp2p.tools.interop.daemon import (
    make_p2pd,
)
from libp2p.tools.interop.utils import (
    connect,
)


@pytest.fixture(params=[NOISE_PROTOCOL_ID, SECIO_PROTOCOL_ID])
def security_protocol(request):
    return request.param


@pytest.fixture
def num_p2pds():
    return 1


@pytest.fixture
def is_gossipsub():
    return True


@pytest.fixture
def is_pubsub_signing():
    return True


@pytest.fixture
def is_pubsub_signing_strict():
    return True


@pytest.fixture
async def p2pds(
    num_p2pds,
    security_protocol,
    is_gossipsub,
    is_pubsub_signing,
    is_pubsub_signing_strict,
):
    async with AsyncExitStack() as stack:
        p2pds = [
            await stack.enter_async_context(
                make_p2pd(
                    get_unused_tcp_port(),
                    get_unused_tcp_port(),
                    security_protocol,
                    is_gossipsub=is_gossipsub,
                    is_pubsub_signing=is_pubsub_signing,
                    is_pubsub_signing_strict=is_pubsub_signing_strict,
                )
            )
            for _ in range(num_p2pds)
        ]
        try:
            yield p2pds
        finally:
            for p2pd in p2pds:
                await p2pd.close()


@pytest.fixture
async def pubsubs(num_hosts, security_protocol, is_gossipsub, is_pubsub_signing_strict):
    if is_gossipsub:
        yield PubsubFactory.create_batch_with_gossipsub(
            num_hosts,
            security_protocol=security_protocol,
            strict_signing=is_pubsub_signing_strict,
        )
    else:
        yield PubsubFactory.create_batch_with_floodsub(
            num_hosts, security_protocol, strict_signing=is_pubsub_signing_strict
        )


class DaemonStream(ReadWriteCloser):
    stream_info: StreamInfo
    stream: anyio.abc.SocketStream

    def __init__(self, stream_info: StreamInfo, stream: anyio.abc.SocketStream) -> None:
        self.stream_info = stream_info
        self.stream = stream

    async def close(self) -> None:
        await self.stream.close()

    async def read(self, n: int = None) -> bytes:
        return await self.stream.receive_some(n)

    async def write(self, data: bytes) -> None:
        return await self.stream.send_all(data)


@pytest.fixture
async def is_to_fail_daemon_stream():
    return False


@pytest.fixture
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:
        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.lowlevel.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