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
|
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/node/waku_switch,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore
|
2022-11-28 14:56:01 +00:00
|
|
|
|
|
|
|
proc newCircuitRelayClientSwitch(relayClient: RelayClient): Switch =
|
|
|
|
SwitchBuilder.new()
|
2023-02-13 10:43:49 +00:00
|
|
|
.withRng(rng())
|
2022-11-28 14:56:01 +00:00
|
|
|
.withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()])
|
|
|
|
.withTcpTransport()
|
|
|
|
.withMplex()
|
|
|
|
.withNoise()
|
|
|
|
.withCircuitRelay(relayClient)
|
|
|
|
.build()
|
|
|
|
|
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)
|
2023-01-11 06:06:30 +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
|
|
|
|
addrs = MultiAddress.init($wakuSwitch.peerInfo.addrs[0] & "/p2p/" &
|
2023-02-10 15:44:06 +00:00
|
|
|
$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.
|
|
|
|
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())
|