From 8d69846aa49781b3c5476744f8cb855ef609f940 Mon Sep 17 00:00:00 2001 From: Hanno Cornelius <68783915+jm-clius@users.noreply.github.com> Date: Mon, 28 Nov 2022 16:56:01 +0200 Subject: [PATCH] feat: enable AutoNAT and libp2p circuit relay (#1425) * feat: enable libp2p circuit relay * feat: enable libp2p autonat --- tests/all_tests_v2.nim | 1 + tests/v2/test_waku_switch.nim | 102 ++++++++++++++++++++++++++++++++++ waku/v2/node/wakuswitch.nim | 2 + 3 files changed, 105 insertions(+) create mode 100644 tests/v2/test_waku_switch.nim diff --git a/tests/all_tests_v2.nim b/tests/all_tests_v2.nim index f01832b2b..e2fe3a57c 100644 --- a/tests/all_tests_v2.nim +++ b/tests/all_tests_v2.nim @@ -52,6 +52,7 @@ import ./v2/test_peer_exchange, ./v2/test_waku_noise, ./v2/test_waku_noise_sessions, + ./v2/test_waku_switch, # Utils ./v2/test_utils_keyfile diff --git a/tests/v2/test_waku_switch.nim b/tests/v2/test_waku_switch.nim new file mode 100644 index 000000000..d56536876 --- /dev/null +++ b/tests/v2/test_waku_switch.nim @@ -0,0 +1,102 @@ +{.used.} + +import + testutils/unittests, + chronos, + libp2p, + libp2p/protocols/connectivity/autonat, + libp2p/protocols/connectivity/relay/relay, + libp2p/protocols/connectivity/relay/client, + stew/byteutils +import + ../../waku/v2/node/wakuswitch, + ./testlib/switch + +proc newCircuitRelayClientSwitch(relayClient: RelayClient): Switch = + SwitchBuilder.new() + .withRng(newRng()) + .withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()]) + .withTcpTransport() + .withMplex() + .withNoise() + .withCircuitRelay(relayClient) + .build() + +procSuite "Waku Switch": + + asyncTest "Waku Switch works with AutoNat": + ## Given + let + sourceSwitch = newTestSwitch() + wakuSwitch = newWakuSwitch() + await sourceSwitch.start() + await wakuSwitch.start() + + ## When + await sourceSwitch.connect(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs) + let ma = await Autonat.new(sourceSwitch).dialMe(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs) + + ## Then + check: + ma == sourceSwitch.peerInfo.addrs[0] + + ## Teardown + await allFutures(sourceSwitch.stop(), wakuSwitch.stop()) + + asyncTest "Waku Switch acts as circuit relayer": + ## Setup + let + wakuSwitch = newWakuSwitch() + sourceClient = RelayClient.new() + destClient = RelayClient.new() + sourceSwitch = newCircuitRelayClientSwitch(sourceClient) + destSwitch = newCircuitRelayClientSwitch(destClient) + + # Setup client relays + sourceClient.setup(sourceSwitch) + destClient.setup(destSwitch) + + await allFutures(wakuSwitch.start(), sourceSwitch.start(), destSwitch.start()) + + ## Given + let + # Create a relay address to destSwitch using wakuSwitch as the relay + addrs = MultiAddress.init($wakuSwitch.peerInfo.addrs[0] & "/p2p/" & + $wakuSwitch.peerInfo.peerId & "/p2p-circuit/p2p/" & + $destSwitch.peerInfo.peerId).get() + msg = "Just one relay away..." + + # Create a custom protocol + let customProtoCodec = "/vac/waku/test/1.0.0" + var + completionFut = newFuture[bool]() + proto = new LPProtocol + proto.codec = customProtoCodec + proto.handler = proc(conn: Connection, proto: string) {.async.} = + assert (await conn.readLp(1024)) == msg.toBytes() + completionFut.complete(true) + + await proto.start() + destSwitch.mount(proto) + + ## When + # Connect destSwitch to the relay + await destSwitch.connect(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs) + + # Connect sourceSwitch to the relay + await sourceSwitch.connect(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs) + + # destClient reserves a slot on the relay. + let rsvp = await destClient.reserve(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs) + + # sourceSwitch dial destSwitch using the relay + let conn = await sourceSwitch.dial(destSwitch.peerInfo.peerId, @[addrs], customProtoCodec) + + await conn.writeLp(msg) + + ## Then + check: + await completionFut.withTimeout(3.seconds) + + ## Teardown + await allFutures(wakuSwitch.stop(), sourceSwitch.stop(), destSwitch.stop()) diff --git a/waku/v2/node/wakuswitch.nim b/waku/v2/node/wakuswitch.nim index 4bf2c2c05..ca11abcf1 100644 --- a/waku/v2/node/wakuswitch.nim +++ b/waku/v2/node/wakuswitch.nim @@ -88,6 +88,8 @@ proc newWakuSwitch*( .withTcpTransport(transportFlags) .withNameResolver(nameResolver) .withSignedPeerRecord(sendSignedPeerRecord) + .withCircuitRelay() + .withAutonat() if peerStoreCapacity.isSome(): b = b.withPeerStore(peerStoreCapacity.get())