2022-11-28 14:56:01 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
testutils/unittests,
|
|
|
|
chronos,
|
|
|
|
libp2p,
|
2023-01-11 06:06:30 +00:00
|
|
|
libp2p/protocols/connectivity/autonat/client,
|
2022-11-28 14:56:01 +00:00
|
|
|
libp2p/protocols/connectivity/relay/relay,
|
|
|
|
libp2p/protocols/connectivity/relay/client,
|
|
|
|
stew/byteutils
|
2024-07-05 22:03:38 +00:00
|
|
|
import waku/node/waku_switch, ./testlib/common, ./testlib/wakucore
|
2022-11-28 14:56:01 +00:00
|
|
|
|
|
|
|
proc newCircuitRelayClientSwitch(relayClient: RelayClient): Switch =
|
2024-03-15 23:08:47 +00:00
|
|
|
SwitchBuilder
|
|
|
|
.new()
|
|
|
|
.withRng(rng())
|
|
|
|
.withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()])
|
|
|
|
.withTcpTransport()
|
|
|
|
.withMplex()
|
|
|
|
.withNoise()
|
|
|
|
.withCircuitRelay(relayClient)
|
|
|
|
.build()
|
2022-11-28 14:56:01 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
suite "Waku Switch":
|
2022-11-28 14:56:01 +00:00
|
|
|
asyncTest "Waku Switch works with AutoNat":
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
sourceSwitch = newTestSwitch()
|
2023-02-06 11:53:05 +00:00
|
|
|
wakuSwitch = newWakuSwitch(rng = rng())
|
2022-11-28 14:56:01 +00:00
|
|
|
await sourceSwitch.start()
|
|
|
|
await wakuSwitch.start()
|
|
|
|
|
|
|
|
## When
|
|
|
|
await sourceSwitch.connect(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs)
|
2024-03-15 23:08:47 +00:00
|
|
|
let ma = await AutonatClient.new().dialMe(
|
|
|
|
sourceSwitch, wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs
|
|
|
|
)
|
2022-11-28 14:56:01 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
ma == sourceSwitch.peerInfo.addrs[0]
|
|
|
|
|
|
|
|
## Teardown
|
|
|
|
await allFutures(sourceSwitch.stop(), wakuSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "Waku Switch acts as circuit relayer":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-02-06 11:53:05 +00:00
|
|
|
wakuSwitch = newWakuSwitch(rng = rng())
|
2022-11-28 14:56:01 +00:00
|
|
|
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
|
2024-03-15 23:08:47 +00:00
|
|
|
addrs = MultiAddress
|
|
|
|
.init(
|
|
|
|
$wakuSwitch.peerInfo.addrs[0] & "/p2p/" & $wakuSwitch.peerInfo.peerId &
|
|
|
|
"/p2p-circuit"
|
|
|
|
)
|
|
|
|
.get()
|
2022-11-28 14:56:01 +00:00
|
|
|
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.
|
2024-03-15 23:08:47 +00:00
|
|
|
let rsvp =
|
|
|
|
await destClient.reserve(wakuSwitch.peerInfo.peerId, wakuSwitch.peerInfo.addrs)
|
2022-11-28 14:56:01 +00:00
|
|
|
|
|
|
|
# sourceSwitch dial destSwitch using the relay
|
2024-03-15 23:08:47 +00:00
|
|
|
let conn =
|
|
|
|
await sourceSwitch.dial(destSwitch.peerInfo.peerId, @[addrs], customProtoCodec)
|
2022-11-28 14:56:01 +00:00
|
|
|
|
|
|
|
await conn.writeLp(msg)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
await completionFut.withTimeout(3.seconds)
|
|
|
|
|
|
|
|
## Teardown
|
|
|
|
await allFutures(wakuSwitch.stop(), sourceSwitch.stop(), destSwitch.stop())
|