|
| 1 | +# SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | +# Copyright (c) Status Research & Development GmbH |
| 3 | + |
| 4 | +import chronos, chronicles, sequtils, tables |
| 5 | +import |
| 6 | + ../../libp2p/[ |
| 7 | + builders, |
| 8 | + switch, |
| 9 | + multicodec, |
| 10 | + observedaddrmanager, |
| 11 | + services/hpservice, |
| 12 | + services/autorelayservice, |
| 13 | + protocols/connectivity/relay/client, |
| 14 | + protocols/connectivity/relay/relay, |
| 15 | + protocols/connectivity/autonat/service, |
| 16 | + protocols/ping, |
| 17 | + ] |
| 18 | +import ../../tests/stubs/autonatclientstub |
| 19 | +import ../unified_testing |
| 20 | + |
| 21 | +logScope: |
| 22 | + topics = "hp interop peer" |
| 23 | + |
| 24 | +proc createSwitch( |
| 25 | + config: BaseConfig, relayClient: Relay = nil, hpService: Service = nil |
| 26 | +): Switch = |
| 27 | + var s = buildBaseSwitch(config, tcpFlags = {ServerFlags.TcpNoDelay}) |
| 28 | + .withObservedAddrManager(ObservedAddrManager.new(maxSize = 1, minCount = 1)) |
| 29 | + .withAutonat() |
| 30 | + .withCircuitRelay(relayClient) |
| 31 | + .build() |
| 32 | + |
| 33 | + s.add(hpService) |
| 34 | + s.mount(Ping.new(rng = rng())) |
| 35 | + s |
| 36 | + |
| 37 | +proc isDirectlyConnected(switch: Switch, peerId: PeerId): bool = |
| 38 | + let conns = switch.connManager.getConnections() |
| 39 | + peerId in conns and conns[peerId].anyIt(not isRelayed(it.connection)) |
| 40 | + |
| 41 | +type HolePunchSwitches = object |
| 42 | + sw: Switch |
| 43 | + aux: Switch |
| 44 | + autonatService: AutonatService |
| 45 | + |
| 46 | +proc createSwitches(config: BaseConfig): HolePunchSwitches = |
| 47 | + # Setup relay |
| 48 | + let relayClient = RelayClient.new() |
| 49 | + let autoRelayService = AutoRelayService.new(1, relayClient, nil, rng()) |
| 50 | + |
| 51 | + # Setup autonat |
| 52 | + let autonatClientStub = AutonatClientStub.new(expectedDials = 1) |
| 53 | + autonatClientStub.answer = NotReachable |
| 54 | + |
| 55 | + # Setup hpservice |
| 56 | + let autonatService = AutonatService.new(autonatClientStub, rng(), maxQueueSize = 1) |
| 57 | + let hpservice = HPService.new(autonatService, autoRelayService) |
| 58 | + |
| 59 | + # Setup switches |
| 60 | + let switches = HolePunchSwitches( |
| 61 | + sw: createSwitch(config, relayClient, hpservice), |
| 62 | + aux: createSwitch(config), |
| 63 | + autonatService: autonatService, |
| 64 | + ) |
| 65 | + switches |
| 66 | + |
| 67 | +proc connectToRelay( |
| 68 | + config: BaseConfig, redisClient: Redis, switches: HolePunchSwitches |
| 69 | +): Future[MultiAddress] {.async.} = |
| 70 | + # Connect to aux switch for AutoNAT stub to report NotReachable |
| 71 | + await switches.sw.connect(switches.aux.peerInfo.peerId, switches.aux.peerInfo.addrs) |
| 72 | + |
| 73 | + # Wait for autonat to report NotReachable |
| 74 | + pollUntil( |
| 75 | + switches.autonatService.networkReachability == NetworkReachability.NotReachable, |
| 76 | + errorMsg = "Timeout waiting for AutoNAT NotReachable", |
| 77 | + ) |
| 78 | + info "AutoNAT reports NotReachable" |
| 79 | + |
| 80 | + # Connect to relay (triggers AutoRelay reservation) |
| 81 | + let relayMA = await fetchRelayMultiaddr(redisClient, config.testKey) |
| 82 | + info "Got relay address", relayMA |
| 83 | + |
| 84 | + try: |
| 85 | + info "Dialing relay", relayMA |
| 86 | + let relayId = await switches.sw.connect(relayMA).wait(30.seconds) |
| 87 | + info "Connected to relay", relayId |
| 88 | + except AsyncTimeoutError as e: |
| 89 | + raise newException(CatchableError, "Connection to relay timed out: " & e.msg, e) |
| 90 | + |
| 91 | + # Wait for our relay circuit address |
| 92 | + pollUntil( |
| 93 | + switches.sw.peerInfo.addrs.anyIt(it.contains(multiCodec("p2p-circuit")).tryGet()), |
| 94 | + errorMsg = "Timeout waiting for relay circuit address", |
| 95 | + ) |
| 96 | + info "Got relay circuit address" |
| 97 | + relayMA |
| 98 | + |
| 99 | +proc runDialer(config: BaseConfig) {.async.} = |
| 100 | + let |
| 101 | + redisClient = setupRedis(config.redisAddr) |
| 102 | + switches = createSwitches(config) |
| 103 | + |
| 104 | + await allFutures(switches.sw.start(), switches.aux.start()) |
| 105 | + defer: |
| 106 | + # Timeout the stop to avoid hanging on mplex teardown |
| 107 | + discard |
| 108 | + await allFutures(switches.sw.stop(), switches.aux.stop()).withTimeout(5.seconds) |
| 109 | + |
| 110 | + let relayMA = await connectToRelay(config, redisClient, switches) |
| 111 | + |
| 112 | + let listenerId = await fetchListenerPeerId(redisClient, config.testKey) |
| 113 | + info "Got listener peer ID", listenerId |
| 114 | + |
| 115 | + let listenerRelayAddr = MultiAddress.init($relayMA & "/p2p-circuit").tryGet() |
| 116 | + |
| 117 | + # Start DCUtR timer |
| 118 | + let dcutrStart = Moment.now() |
| 119 | + |
| 120 | + info "Dialing listener via relay", listenerRelayAddr |
| 121 | + await switches.sw.connect(listenerId, @[listenerRelayAddr]) |
| 122 | + |
| 123 | + # Wait for DCUtR to complete (direct connection established) |
| 124 | + # HPService handles DCUtR in the background when the listener receives |
| 125 | + # the relayed connection. Poll for a direct connection. |
| 126 | + pollUntil( |
| 127 | + switches.sw.isDirectlyConnected(listenerId), |
| 128 | + errorMsg = "DCUtR failed: no direct connection established within timeout", |
| 129 | + ) |
| 130 | + |
| 131 | + let dcutrElapsed = Moment.now() - dcutrStart |
| 132 | + info "Direct connection established via DCUtR" |
| 133 | + |
| 134 | + # Ping over the direct connection |
| 135 | + let channel = await switches.sw.dial(listenerId, PingCodec) |
| 136 | + defer: |
| 137 | + await channel.close() |
| 138 | + |
| 139 | + let pingDelay = await Ping.new(rng = rng()).ping(channel) |
| 140 | + let pingRttMs = pingDelay.toMs() |
| 141 | + |
| 142 | + printLatencyYaml(dcutrElapsed.toMs() + pingRttMs, pingRttMs) |
| 143 | + |
| 144 | +proc runListener(config: BaseConfig) {.async.} = |
| 145 | + let |
| 146 | + redisClient = setupRedis(config.redisAddr) |
| 147 | + switches = createSwitches(config) |
| 148 | + |
| 149 | + await allFutures(switches.sw.start(), switches.aux.start()) |
| 150 | + defer: |
| 151 | + # Timeout the stop to avoid hanging on mplex teardown |
| 152 | + discard |
| 153 | + await allFutures(switches.sw.stop(), switches.aux.stop()).withTimeout(5.seconds) |
| 154 | + |
| 155 | + discard await connectToRelay(config, redisClient, switches) |
| 156 | + |
| 157 | + let listenerPeerId = publishListenerPeerId(redisClient, config.testKey, switches.sw) |
| 158 | + info "Published listener peer ID to Redis", listenerPeerId |
| 159 | + |
| 160 | + # Wait to be killed (docker-compose will stop us after dialer exits) |
| 161 | + await sleepAsync(5.minutes) |
| 162 | + |
| 163 | +proc main() {.async.} = |
| 164 | + let config = readBaseConfig() |
| 165 | + info "Test configuration", config |
| 166 | + if config.isDialer: |
| 167 | + await runDialer(config) |
| 168 | + else: |
| 169 | + await runListener(config) |
| 170 | + |
| 171 | +let testTimeout = parseDurationEnv("TEST_TIMEOUT_SECS", 1.seconds, DefaultTestTimeout) |
| 172 | +runMain(main, testTimeout) |
0 commit comments