nim-libp2p/tests/testautorelay.nim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

163 lines
5.6 KiB
Nim
Raw Normal View History

2023-01-17 15:18:38 +00:00
{.used.}
# Nim-Libp2p
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
2023-01-17 15:18:38 +00:00
import chronos, options
import ../libp2p
import
../libp2p/[
crypto/crypto,
protocols/connectivity/relay/relay,
protocols/connectivity/relay/client,
services/autorelayservice,
]
import ./helpers
proc createSwitch(r: Relay, autorelay: Service = nil): Switch =
var builder = SwitchBuilder
.new()
.withRng(newRng())
2024-06-12 14:31:09 +00:00
.withAddresses(@[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()])
2023-01-17 15:18:38 +00:00
.withTcpTransport()
.withMplex()
.withNoise()
.withCircuitRelay(r)
if not autorelay.isNil():
builder = builder.withServices(@[autorelay])
builder.build()
2024-06-12 14:31:09 +00:00
proc buildRelayMA(switchRelay: Switch, switchClient: Switch): seq[MultiAddress] =
result = newSeq[MultiAddress]()
for i in 0 ..< switchRelay.peerInfo.addrs.len():
result.add(
MultiAddress
.init(
$switchRelay.peerInfo.addrs[i] & "/p2p/" & $switchRelay.peerInfo.peerId &
"/p2p-circuit"
)
.get()
)
2023-01-17 15:18:38 +00:00
suite "Autorelay":
asyncTeardown:
checkTrackers()
var
switchRelay {.threadvar.}: Switch
switchClient {.threadvar.}: Switch
relayClient {.threadvar.}: RelayClient
autorelay {.threadvar.}: AutoRelayService
asyncTest "Simple test":
switchRelay = createSwitch(Relay.new())
relayClient = RelayClient.new()
let fut = newFuture[void]()
proc checkMA(addresses: seq[MultiAddress]) =
check:
2024-06-12 14:31:09 +00:00
addresses == buildRelayMA(switchRelay, switchClient)
2023-01-17 15:18:38 +00:00
fut.complete()
2023-01-17 15:18:38 +00:00
autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng())
switchClient = createSwitch(relayClient, autorelay)
await allFutures(switchClient.start(), switchRelay.start())
await switchClient.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs)
await fut.wait(1.seconds)
let addresses = autorelay.getAddresses()
check:
2024-06-12 14:31:09 +00:00
addresses == buildRelayMA(switchRelay, switchClient)
2023-01-17 15:18:38 +00:00
await allFutures(switchClient.stop(), switchRelay.stop())
asyncTest "Connect after starting switches":
switchRelay = createSwitch(Relay.new())
relayClient = RelayClient.new()
let fut = newFuture[void]()
proc checkMA(address: seq[MultiAddress]) =
check:
2024-06-12 14:31:09 +00:00
address == buildRelayMA(switchRelay, switchClient)
2023-01-17 15:18:38 +00:00
fut.complete()
2023-01-17 15:18:38 +00:00
let autorelay = AutoRelayService.new(3, relayClient, checkMA, newRng())
switchClient = createSwitch(relayClient, autorelay)
await allFutures(switchClient.start(), switchRelay.start())
await sleepAsync(500.millis)
await switchClient.connect(switchRelay.peerInfo.peerId, switchRelay.peerInfo.addrs)
await fut.wait(1.seconds)
let addresses = autorelay.getAddresses()
2024-06-12 14:31:09 +00:00
2023-01-17 15:18:38 +00:00
check:
2024-06-12 14:31:09 +00:00
addresses == buildRelayMA(switchRelay, switchClient)
for address in addresses:
check address in switchClient.peerInfo.addrs
2023-01-17 15:18:38 +00:00
2024-06-12 14:31:09 +00:00
await allFutures(switchClient.stop(), switchRelay.stop())
2023-01-17 15:18:38 +00:00
asyncTest "Three relays connections":
2024-06-12 14:31:09 +00:00
type RelayReservationState = enum
Relay1Reserved
Relay1AndRelay2Reserved
# Although switchClient is connected to rel3, rel3 isn't reserved due to the maximum number of relays set to 2.
Relay2UnreservedAndRelay1Reserved
Relay1AndRelay3Reserved
var state = Relay1Reserved
2023-01-17 15:18:38 +00:00
let
rel1 = createSwitch(Relay.new())
rel2 = createSwitch(Relay.new())
rel3 = createSwitch(Relay.new())
2024-06-12 14:31:09 +00:00
rel1Checked = newFuture[void]()
rel1And2Checked = newFuture[void]()
allChecksCompleted = newFuture[void]()
2023-01-17 15:18:38 +00:00
relayClient = RelayClient.new()
proc checkMA(addresses: seq[MultiAddress]) =
2024-06-12 14:31:09 +00:00
if state == Relay1Reserved or state == Relay2UnreservedAndRelay1Reserved:
let relayMAs = buildRelayMA(rel1, switchClient)
for relayMA in relayMAs:
check:
relayMA in addresses
if state == Relay1Reserved:
state = Relay1AndRelay2Reserved
rel1Checked.complete()
elif state == Relay2UnreservedAndRelay1Reserved:
state = Relay1AndRelay3Reserved
elif state == Relay1AndRelay2Reserved:
let relay1MAs = buildRelayMA(rel1, switchClient)
for relayMA in relay1MAs:
check:
relayMA in addresses
let relay2MAs = buildRelayMA(rel2, switchClient)
for relayMA in relay2MAs:
check:
relayMA in addresses
state = Relay2UnreservedAndRelay1Reserved
rel1And2Checked.complete()
elif state == Relay1AndRelay3Reserved:
let relay1MAs = buildRelayMA(rel1, switchClient)
for relayMA in relay1MAs:
check:
relayMA in addresses
let relay3MAs = buildRelayMA(rel3, switchClient)
for relayMA in relay3MAs:
check:
relayMA in addresses
allChecksCompleted.complete()
2024-06-12 14:31:09 +00:00
let autorelay =
AutoRelayService.new(maxNumRelays = 2, relayClient, checkMA, newRng())
2023-01-17 15:18:38 +00:00
switchClient = createSwitch(relayClient, autorelay)
await allFutures(switchClient.start(), rel1.start(), rel2.start(), rel3.start())
await switchClient.connect(rel1.peerInfo.peerId, rel1.peerInfo.addrs)
2024-06-12 14:31:09 +00:00
await rel1Checked.wait(500.millis)
2023-01-17 15:18:38 +00:00
await switchClient.connect(rel2.peerInfo.peerId, rel2.peerInfo.addrs)
await switchClient.connect(rel3.peerInfo.peerId, rel3.peerInfo.addrs)
2024-06-12 14:31:09 +00:00
await rel1And2Checked.wait(500.millis)
2023-01-17 15:18:38 +00:00
await rel2.stop()
2024-06-12 14:31:09 +00:00
await allChecksCompleted.wait(1.seconds)
2023-01-17 15:18:38 +00:00
await allFutures(switchClient.stop(), rel1.stop(), rel3.stop())