mirror of https://github.com/waku-org/nwaku.git
feat: enable AutoNAT and libp2p circuit relay (#1425)
* feat: enable libp2p circuit relay * feat: enable libp2p autonat
This commit is contained in:
parent
3474bef8e3
commit
8d69846aa4
|
@ -52,6 +52,7 @@ import
|
||||||
./v2/test_peer_exchange,
|
./v2/test_peer_exchange,
|
||||||
./v2/test_waku_noise,
|
./v2/test_waku_noise,
|
||||||
./v2/test_waku_noise_sessions,
|
./v2/test_waku_noise_sessions,
|
||||||
|
./v2/test_waku_switch,
|
||||||
# Utils
|
# Utils
|
||||||
./v2/test_utils_keyfile
|
./v2/test_utils_keyfile
|
||||||
|
|
||||||
|
|
|
@ -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())
|
|
@ -88,6 +88,8 @@ proc newWakuSwitch*(
|
||||||
.withTcpTransport(transportFlags)
|
.withTcpTransport(transportFlags)
|
||||||
.withNameResolver(nameResolver)
|
.withNameResolver(nameResolver)
|
||||||
.withSignedPeerRecord(sendSignedPeerRecord)
|
.withSignedPeerRecord(sendSignedPeerRecord)
|
||||||
|
.withCircuitRelay()
|
||||||
|
.withAutonat()
|
||||||
|
|
||||||
if peerStoreCapacity.isSome():
|
if peerStoreCapacity.isSome():
|
||||||
b = b.withPeerStore(peerStoreCapacity.get())
|
b = b.withPeerStore(peerStoreCapacity.get())
|
||||||
|
|
Loading…
Reference in New Issue