Explorar o código

Support circuit relay v2, add tests (#537)

- switched to p2pd version 0.3.16
- added support for Circuit Relay V2 in all hivemind protocols
- added an option no_listen=False for testing purposes (hivemind.p2p.p2p_daemon)
- added a test that checks if a peer can connect to another peer through circuit relay

Co-authored-by: justheuristic <justheuristic@gmail.com>
(cherry picked from commit 9d89a7b8bf807a9bb250e2c6a5f22ee210404729)
Egiazarian Vage %!s(int64=2) %!d(string=hai) anos
pai
achega
ef80d4160f
Modificáronse 3 ficheiros con 71 adicións e 3 borrados
  1. 5 1
      hivemind/p2p/p2p_daemon.py
  2. 2 2
      setup.py
  3. 64 0
      tests/test_relays.py

+ 5 - 1
hivemind/p2p/p2p_daemon.py

@@ -104,6 +104,8 @@ class P2P:
         use_relay_hop: Optional[bool] = None,
         use_relay_discovery: Optional[bool] = None,
         check_if_identity_free: bool = True,
+        no_listen: bool = False,
+        trustedRelays: Optional[Sequence[Union[Multiaddr, str]]] = ("",),
     ) -> "P2P":
         """
         Start a new p2pd process and connect to it.
@@ -171,10 +173,12 @@ class P2P:
             ("bootstrapPeers", initial_peers),
             ("hostAddrs", host_maddrs),
             ("announceAddrs", announce_maddrs),
+            ("trustedRelays", trustedRelays),
         ]:
             if value:
                 process_kwargs[param] = self._maddrs_to_str(value)
-
+        if no_listen:
+            process_kwargs["noListenAddrs"] = 1
         if identity_path is not None:
             if os.path.isfile(identity_path):
                 if check_if_identity_free and need_bootstrap:

+ 2 - 2
setup.py

@@ -13,14 +13,14 @@ from setuptools import find_packages, setup
 from setuptools.command.build_py import build_py
 from setuptools.command.develop import develop
 
-P2PD_VERSION = "v0.3.14"
+P2PD_VERSION = "v0.3.16"
 
 P2PD_SOURCE_URL = f"https://github.com/learning-at-home/go-libp2p-daemon/archive/refs/tags/{P2PD_VERSION}.tar.gz"
 P2PD_BINARY_URL = f"https://github.com/learning-at-home/go-libp2p-daemon/releases/download/{P2PD_VERSION}/"
 
 # The value is sha256 of the binary from the release page
 EXECUTABLES = {
-    "p2pd": "c9e7cde84ca53925622ad52fef50e41f8fd8cb258bbd69d4ab560beb672bb255",
+    "p2pd": "057ec61edbe926cf049e9532d43ea9540da55db7b2d8c816d2bbdddce23f3cdf",
 }
 
 here = os.path.abspath(os.path.dirname(__file__))

+ 64 - 0
tests/test_relays.py

@@ -0,0 +1,64 @@
+import time
+from functools import partial
+
+import pytest
+
+import hivemind
+
+
+async def ping_to_client(dht, node, peer_id: str):
+    return await node.protocol.call_ping(hivemind.PeerID.from_base58(str(peer_id)))
+
+
+@pytest.mark.forked
+@pytest.mark.parametrize(
+    "use_auto_relay,use_relay",
+    [
+        (True, True),
+        (False, False),
+    ],
+)
+def test_autorelay(use_auto_relay: bool, use_relay: bool):
+    dht_first_peer = hivemind.DHT(
+        start=True,
+        use_auto_relay=use_auto_relay,
+        use_relay=use_relay,
+        force_reachability="public",
+    )
+    dht_first_peer_id = dht_first_peer.peer_id
+    initial_peers = dht_first_peer.get_visible_maddrs()
+    assert dht_first_peer_id is not None
+
+    dht_third_peer = hivemind.DHT(
+        initial_peers=initial_peers,
+        host_maddrs=[],
+        start=True,
+        no_listen=True,
+        use_relay=use_relay,
+        client_mode=False,
+        use_auto_relay=use_auto_relay,
+    )
+    time.sleep(5)
+    dht_second_peer = hivemind.DHT(
+        initial_peers=initial_peers,
+        start=True,
+        client_mode=False,
+        no_listen=False,
+        use_relay=use_relay,
+        use_auto_relay=use_auto_relay,
+    )
+
+    assert dht_first_peer.is_alive() and dht_second_peer.is_alive() and dht_third_peer.is_alive()
+
+    time_start = time.perf_counter()
+    while time.perf_counter() - time_start < 30:
+        reached_ip = dht_second_peer.run_coroutine(partial(ping_to_client, peer_id=dht_third_peer.peer_id))
+        if reached_ip:
+            assert use_relay
+            break
+        time.sleep(2)
+    else:
+        assert not use_relay
+
+    for peer in dht_first_peer, dht_second_peer, dht_third_peer:
+        peer.shutdown()