From c830079c7f845cbf2a6d57965d9f9472acac89b6 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 3 Nov 2023 14:01:00 +0100 Subject: [PATCH 01/19] add nim test basic version --- hole-punch-interop/Makefile | 21 ++-- hole-punch-interop/dockerBuildWrapper.sh | 1 + hole-punch-interop/impl/nim/hole_punching.nim | 100 ++++++++++++++++++ hole-punch-interop/impl/nim/v1.1/Dockerfile | 25 +++++ hole-punch-interop/impl/nim/v1.1/Makefile | 28 +++++ hole-punch-interop/versions.ts | 4 + 6 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 hole-punch-interop/impl/nim/hole_punching.nim create mode 100644 hole-punch-interop/impl/nim/v1.1/Dockerfile create mode 100644 hole-punch-interop/impl/nim/v1.1/Makefile diff --git a/hole-punch-interop/Makefile b/hole-punch-interop/Makefile index b88717757..aba1ade02 100644 --- a/hole-punch-interop/Makefile +++ b/hole-punch-interop/Makefile @@ -1,19 +1,26 @@ RUST_SUBDIRS := $(wildcard impl/rust/*/.) GO_SUBDIRS := $(wildcard impl/go/*/.) +NIM_SUBDIRS := $(wildcard impl/nim/*/.) + +# Combine all sub-directory lists into a single list +ALL_SUBDIRS := $(RUST_SUBDIRS) $(GO_SUBDIRS) $(NIM_SUBDIRS) + +all: rust-relay router $(ALL_SUBDIRS) -all: rust-relay router $(RUST_SUBDIRS) $(GO_SUBDIRS) rust-relay: $(MAKE) -C rust-relay + router: $(MAKE) -C router -$(RUST_SUBDIRS): - $(MAKE) -C $@ -$(GO_SUBDIRS): + +$(ALL_SUBDIRS): $(MAKE) -C $@ + clean: $(MAKE) -C rust-relay clean $(MAKE) -C router clean - $(MAKE) -C $(RUST_SUBDIRS) clean - $(MAKE) -C $(GO_SUBDIRS) clean + for dir in $(ALL_SUBDIRS); do \ + $(MAKE) -C $$dir clean; \ + done -.PHONY: rust-relay router all $(RUST_SUBDIRS) $(GO_SUBDIRS) +.PHONY: rust-relay router all $(ALL_SUBDIRS) clean diff --git a/hole-punch-interop/dockerBuildWrapper.sh b/hole-punch-interop/dockerBuildWrapper.sh index 087a4e8c9..377956ce5 100755 --- a/hole-punch-interop/dockerBuildWrapper.sh +++ b/hole-punch-interop/dockerBuildWrapper.sh @@ -10,4 +10,5 @@ fi docker buildx build \ --load \ + --progress=plain \ -t $IMAGE_NAME $CACHING_OPTIONS "$@" diff --git a/hole-punch-interop/impl/nim/hole_punching.nim b/hole-punch-interop/impl/nim/hole_punching.nim new file mode 100644 index 000000000..ddd7ff4f5 --- /dev/null +++ b/hole-punch-interop/impl/nim/hole_punching.nim @@ -0,0 +1,100 @@ +import std/[os, options] +import redis +import chronos, metrics, chronicles +import libp2p/[builders, + switch, + observedaddrmanager, + services/hpservice, + services/autorelayservice, + protocols/connectivity/autonat/client as aclient, + protocols/connectivity/relay/relay, + protocols/connectivity/autonat/service] +import libp2p/protocols/connectivity/relay/client as rclient +import tests/stubs/autonatclientstub + +proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = + var builder = SwitchBuilder.new() + .withRng(newRng()) + .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) + .withObservedAddrManager(ObservedAddrManager.new(minCount = 1)) + .withTcpTransport() + .withYamux() + .withAutonat() + .withNoise() + + if hpService != nil: + builder = builder.withServices(@[hpService]) + + if r != nil: + builder = builder.withCircuitRelay(r) + + return builder.build() + +proc main() {.async.} = + let relayClient = RelayClient.new() + let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) + let autonatClientStub = AutonatClientStub.new(expectedDials = 1) + autonatClientStub.answer = NotReachable + let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) + let hpservice = HPService.new(autonatService, autoRelayService) + + let switch = createSwitch(relayClient, hpservice) + await switch.start() + + let + isListener = getEnv("MODE") == "listen" + redisClient = open("redis", 6379.Port) + + debug "Connected to redis" + + if isListener: + let relayAddr = + try: + redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 20) + except Exception as e: + raise newException(CatchableError, e.msg) + debug "Got relay address" + + let relayMA = MultiAddress.init(relayAddr[1]).tryGet() + let relayId = await switch.connect(relayMA) + debug "Connected to relay", relayId + + await sleepAsync(20.seconds) + let listenerPeerId = switch.peerInfo.peerId + discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId) + debug "Pushed listener client peer id to redis", listenerPeerId + else: + let listenerId = + try: + redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 20) + except Exception as e: + raise newException(CatchableError, e.msg) + # var i = 1 + # var flags = Flags(transport: "tcp") + # while i < paramCount(): + # case paramStr(i) + # of "--run-server": flags.runServer = true + # of "--server-ip-address": + # flags.serverIpAddress = initTAddress(paramStr(i + 1)) + # i += 1 + # of "--transport": + # flags.transport = paramStr(i + 1) + # i += 1 + # of "--upload-bytes": + # flags.uploadBytes = parseUInt(paramStr(i + 1)) + # i += 1 + # of "--download-bytes": + # flags.downloadBytes = parseUInt(paramStr(i + 1)) + # i += 1 + # else: discard + # i += 1 + # + # if flags.runServer: + # await runServer(flags.serverIpAddress) + # else: + # await runClient(flags) +try: + discard waitFor(main().withTimeout(2.minutes)) +except Exception as e: + error "Unexpected error", msg = e.msg +quit(1) \ No newline at end of file diff --git a/hole-punch-interop/impl/nim/v1.1/Dockerfile b/hole-punch-interop/impl/nim/v1.1/Dockerfile new file mode 100644 index 000000000..f8044a8da --- /dev/null +++ b/hole-punch-interop/impl/nim/v1.1/Dockerfile @@ -0,0 +1,25 @@ +# syntax=docker/dockerfile:1.5-labs +FROM nimlang/nim:1.6.14 as builder + +# Run with access to the target cache to speed up builds +WORKDIR /workspace + +COPY nim-libp2p nim-libp2p + +# Cache the nimcache and nimble directories to speed up builds +RUN cd nim-libp2p && nimble install_pinned && nimble install redis -y + +COPY ../hole_punching.nim nim-libp2p/hole_punching.nim + +RUN --mount=type=cache,target=/root/.nimble \ + --mount=type=cache,target=/root/.cache/nim \ + cd nim-libp2p && nim c --nimcache:root/.cache/nim -d:chronicles_log_level=DEBUG --threads:off -d:release -o:hole-punching-tests hole_punching.nim + +RUN ls -la /root/.cache/nim + +RUN echo $(dpkg --print-architecture) + +FROM --platform=linux/amd64 debian:bookworm-slim +RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y dnsutils jq curl tcpdump iproute2 +COPY --from=builder /workspace/nim-libp2p/hole-punching-tests /usr/bin/hole-punch-client +ENV RUST_BACKTRACE=1 diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile new file mode 100644 index 000000000..15e5cda1c --- /dev/null +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -0,0 +1,28 @@ +image_name := nim-v1.1 +commitSha := 9abe5e34ce39a06d861043be2076d194f2dba653 + +all: image.json + +image.json: hole_punching.nim nim-libp2p Dockerfile + IMAGE_NAME=${image_name} ../../../dockerBuildWrapper.sh . + docker image inspect ${image_name} -f "{{.Id}}" | \ + xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ + +hole_punching.nim: ../hole_punching.nim + cp ../hole_punching.nim hole_punching.nim + +nim-libp2p: nim-libp2p-${commitSha} + rm -rf nim-libp2p + ln -s nim-libp2p-${commitSha} nim-libp2p + +nim-libp2p-${commitSha}: nim-libp2p-${commitSha}.zip + unzip -o nim-libp2p-${commitSha}.zip + +nim-libp2p-${commitSha}.zip: + wget -O $@ "https://github.com/status-im/nim-libp2p/archive/${commitSha}.zip" + +clean: + rm -f nim-libp2p + rm -rf nim-libp2p-* + +.PHONY: all clean \ No newline at end of file diff --git a/hole-punch-interop/versions.ts b/hole-punch-interop/versions.ts index 9d35b6de9..82533e09b 100644 --- a/hole-punch-interop/versions.ts +++ b/hole-punch-interop/versions.ts @@ -13,6 +13,10 @@ export const versions: Array = [ id: "rust-v0.53", transports: ["tcp", "quic"] } as Version, + { + id: "nim-v1.1", + transports: ["tcp"], + }, ].map((v: Version) => (typeof v.containerImageID === "undefined" ? ({ ...v, containerImageID: readImageId(canonicalImagePath(v.id)) }) : v)) function readImageId(path: string): string { From 774a7c7b5a2b8c1494d297a1f842f104f80b0896 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 3 Nov 2023 17:33:36 +0100 Subject: [PATCH 02/19] add ping --- hole-punch-interop/impl/nim/hole_punching.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hole-punch-interop/impl/nim/hole_punching.nim b/hole-punch-interop/impl/nim/hole_punching.nim index ddd7ff4f5..ea5fa0f9f 100644 --- a/hole-punch-interop/impl/nim/hole_punching.nim +++ b/hole-punch-interop/impl/nim/hole_punching.nim @@ -11,10 +11,12 @@ import libp2p/[builders, protocols/connectivity/autonat/service] import libp2p/protocols/connectivity/relay/client as rclient import tests/stubs/autonatclientstub +import libp2p/protocols/ping proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = + let rng = newRng() var builder = SwitchBuilder.new() - .withRng(newRng()) + .withRng(rng) .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) .withObservedAddrManager(ObservedAddrManager.new(minCount = 1)) .withTcpTransport() @@ -28,7 +30,9 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = if r != nil: builder = builder.withCircuitRelay(r) - return builder.build() + let s = builder.build() + s.mount(Ping.new(rng=rng)) + return s proc main() {.async.} = let relayClient = RelayClient.new() From 186e77ad5d34a65dfce6b0d347a1a9ed5f2af36d Mon Sep 17 00:00:00 2001 From: Diego Date: Mon, 6 Nov 2023 18:22:57 +0100 Subject: [PATCH 03/19] Advancing the test --- hole-punch-interop/impl/nim/hole_punching.nim | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/hole-punch-interop/impl/nim/hole_punching.nim b/hole-punch-interop/impl/nim/hole_punching.nim index ea5fa0f9f..584325d91 100644 --- a/hole-punch-interop/impl/nim/hole_punching.nim +++ b/hole-punch-interop/impl/nim/hole_punching.nim @@ -12,6 +12,7 @@ import libp2p/[builders, import libp2p/protocols/connectivity/relay/client as rclient import tests/stubs/autonatclientstub import libp2p/protocols/ping +import libp2p/utils/heartbeat proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = let rng = newRng() @@ -19,7 +20,7 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = .withRng(rng) .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) .withObservedAddrManager(ObservedAddrManager.new(minCount = 1)) - .withTcpTransport() + .withTcpTransport({ServerFlags.TcpNoDelay}) .withYamux() .withAutonat() .withNoise() @@ -34,6 +35,12 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = s.mount(Ping.new(rng=rng)) return s + +proc ping(conn: Connection) {.async.} = + let pingProtocol = Ping.new() + heartbeat "Ping background proc", 30.seconds: + discard await pingProtocol.ping(conn) + proc main() {.async.} = let relayClient = RelayClient.new() let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) @@ -48,31 +55,46 @@ proc main() {.async.} = let isListener = getEnv("MODE") == "listen" redisClient = open("redis", 6379.Port) - debug "Connected to redis" - if isListener: - let relayAddr = - try: - redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 20) - except Exception as e: - raise newException(CatchableError, e.msg) - debug "Got relay address" + let relayAddr = + try: + redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 0) + except Exception as e: + raise newException(CatchableError, e.msg) + let relayMA = MultiAddress.init(relayAddr[1]).tryGet() + debug "Got relay address", relayMA - let relayMA = MultiAddress.init(relayAddr[1]).tryGet() + if isListener: let relayId = await switch.connect(relayMA) debug "Connected to relay", relayId - await sleepAsync(20.seconds) + let conn = await switch.dial(relayId, @[relayMA], PingCodec) + asyncSpawn conn.ping() + + while switch.peerInfo.addrs.len == 0: + debug "Waiting for addresses" + await sleepAsync(200.milliseconds) + let listenerPeerId = switch.peerInfo.peerId discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId) + debug "Addresses", addrs = $(switch.peerInfo.addrs) debug "Pushed listener client peer id to redis", listenerPeerId + await sleepAsync(2.minutes) + await conn.close() else: - let listenerId = - try: - redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 20) - except Exception as e: - raise newException(CatchableError, e.msg) + let listenerId = + try: + PeerId.init(redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 0)[1]).tryGet() + except Exception as e: + raise newException(CatchableError, e.msg) + debug "Got listener peer id", listenerId + let listenerRelayAddrStr = $relayMA & "/p2p-circuit" + debug "Listener relay address string", listenerRelayAddrStr + let listenerRelayAddr = MultiAddress.init(listenerRelayAddrStr).tryGet() + debug "Dialing listener relay address", listenerRelayAddr + await switch.connect(listenerId, @[listenerRelayAddr]) + await sleepAsync(2.minutes) # var i = 1 # var flags = Flags(transport: "tcp") # while i < paramCount(): @@ -98,7 +120,7 @@ proc main() {.async.} = # else: # await runClient(flags) try: - discard waitFor(main().withTimeout(2.minutes)) + discard waitFor(main().withTimeout(4.minutes)) except Exception as e: error "Unexpected error", msg = e.msg quit(1) \ No newline at end of file From 6a4d45e1c058b52d81a41ec2ecd0c40830793e28 Mon Sep 17 00:00:00 2001 From: Diego Date: Sun, 19 Nov 2023 02:27:56 +0100 Subject: [PATCH 04/19] finishing test --- hole-punch-interop/impl/nim/hole_punching.nim | 156 ++++++++---------- 1 file changed, 72 insertions(+), 84 deletions(-) diff --git a/hole-punch-interop/impl/nim/hole_punching.nim b/hole-punch-interop/impl/nim/hole_punching.nim index 584325d91..6b4fab547 100644 --- a/hole-punch-interop/impl/nim/hole_punching.nim +++ b/hole-punch-interop/impl/nim/hole_punching.nim @@ -1,4 +1,4 @@ -import std/[os, options] +import std/[os, options, strformat] import redis import chronos, metrics, chronicles import libp2p/[builders, @@ -12,14 +12,13 @@ import libp2p/[builders, import libp2p/protocols/connectivity/relay/client as rclient import tests/stubs/autonatclientstub import libp2p/protocols/ping -import libp2p/utils/heartbeat proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = let rng = newRng() var builder = SwitchBuilder.new() .withRng(rng) .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) - .withObservedAddrManager(ObservedAddrManager.new(minCount = 1)) + .withObservedAddrManager(ObservedAddrManager.new(maxSize = 1, minCount = 1)) .withTcpTransport({ServerFlags.TcpNoDelay}) .withYamux() .withAutonat() @@ -35,92 +34,81 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = s.mount(Ping.new(rng=rng)) return s +proc main() {.async.} = + try: + let relayClient = RelayClient.new() + let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) + let autonatClientStub = AutonatClientStub.new(expectedDials = 1) + autonatClientStub.answer = NotReachable + let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) + let hpservice = HPService.new(autonatService, autoRelayService) -proc ping(conn: Connection) {.async.} = - let pingProtocol = Ping.new() - heartbeat "Ping background proc", 30.seconds: - discard await pingProtocol.ping(conn) + let + isListener = getEnv("MODE") == "listen" + switch = createSwitch(relayClient, hpservice) + auxSwitch = createSwitch() + redisClient = open("redis", 6379.Port) -proc main() {.async.} = - let relayClient = RelayClient.new() - let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) - let autonatClientStub = AutonatClientStub.new(expectedDials = 1) - autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) - let hpservice = HPService.new(autonatService, autoRelayService) - - let switch = createSwitch(relayClient, hpservice) - await switch.start() - - let - isListener = getEnv("MODE") == "listen" - redisClient = open("redis", 6379.Port) - debug "Connected to redis" - - let relayAddr = - try: - redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 0) - except Exception as e: - raise newException(CatchableError, e.msg) - let relayMA = MultiAddress.init(relayAddr[1]).tryGet() - debug "Got relay address", relayMA - - if isListener: - let relayId = await switch.connect(relayMA) - debug "Connected to relay", relayId + debug "Connected to redis" - let conn = await switch.dial(relayId, @[relayMA], PingCodec) - asyncSpawn conn.ping() + await switch.start() + await auxSwitch.start() - while switch.peerInfo.addrs.len == 0: - debug "Waiting for addresses" - await sleepAsync(200.milliseconds) - - let listenerPeerId = switch.peerInfo.peerId - discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId) - debug "Addresses", addrs = $(switch.peerInfo.addrs) - debug "Pushed listener client peer id to redis", listenerPeerId - await sleepAsync(2.minutes) - await conn.close() - else: - let listenerId = + let relayAddr = try: - PeerId.init(redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 0)[1]).tryGet() + redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 0) except Exception as e: raise newException(CatchableError, e.msg) - debug "Got listener peer id", listenerId - let listenerRelayAddrStr = $relayMA & "/p2p-circuit" - debug "Listener relay address string", listenerRelayAddrStr - let listenerRelayAddr = MultiAddress.init(listenerRelayAddrStr).tryGet() - debug "Dialing listener relay address", listenerRelayAddr - await switch.connect(listenerId, @[listenerRelayAddr]) - await sleepAsync(2.minutes) - # var i = 1 - # var flags = Flags(transport: "tcp") - # while i < paramCount(): - # case paramStr(i) - # of "--run-server": flags.runServer = true - # of "--server-ip-address": - # flags.serverIpAddress = initTAddress(paramStr(i + 1)) - # i += 1 - # of "--transport": - # flags.transport = paramStr(i + 1) - # i += 1 - # of "--upload-bytes": - # flags.uploadBytes = parseUInt(paramStr(i + 1)) - # i += 1 - # of "--download-bytes": - # flags.downloadBytes = parseUInt(paramStr(i + 1)) - # i += 1 - # else: discard - # i += 1 - # - # if flags.runServer: - # await runServer(flags.serverIpAddress) - # else: - # await runClient(flags) -try: - discard waitFor(main().withTimeout(4.minutes)) -except Exception as e: - error "Unexpected error", msg = e.msg + + # This is necessary to make the autonat service work. It will ask this peer for our reachability which the autonat + # client stub will answer NotReachable. + await switch.connect(auxSwitch.peerInfo.peerId, auxSwitch.peerInfo.addrs) + + # Wait for autonat to be NotReachable + while autonatService.networkReachability != NetworkReachability.NotReachable: + await sleepAsync(100.milliseconds) + + # This will trigger the autonat relay service to make a reservation. + let relayMA = MultiAddress.init(relayAddr[1]).tryGet() + debug "Got relay address", relayMA + let relayId = await switch.connect(relayMA) + debug "Connected to relay", relayId + + # Wait for our relay address to be published + while switch.peerInfo.addrs.len == 0: + await sleepAsync(100.milliseconds) + + if isListener: + let listenerPeerId = switch.peerInfo.peerId + discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId) + debug "Pushed listener client peer id to redis", listenerPeerId + + # Nothing to do anymore, wait to be killed + await sleepAsync(2.minutes) + else: + let listenerId = + try: + PeerId.init(redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 0)[1]).tryGet() + except Exception as e: + raise newException(CatchableError, e.msg) + + debug "Got listener peer id", listenerId + let listenerRelayAddr = MultiAddress.init($relayMA & "/p2p-circuit").tryGet() + + debug "Dialing listener relay address", listenerRelayAddr + await switch.connect(listenerId, @[listenerRelayAddr]) + + # wait for hole-punching to complete in the background + await sleepAsync(5000.milliseconds) + + let conn = switch.connManager.selectMuxer(listenerId).connection + let channel = await switch.dial(listenerId, @[listenerRelayAddr], PingCodec) + let delay = await Ping.new().ping(channel) + await allFuturesThrowing(channel.close(), conn.close(), switch.stop(), auxSwitch.stop()) + echo &"""{{"rtt_to_holepunched_peer_millis":{delay.millis}}}""" + quit(0) + except Exception as e: + error "Unexpected error", msg = e.msg + +discard waitFor(main().withTimeout(4.minutes)) quit(1) \ No newline at end of file From fef9abc5c5c315669a92dba4089fdaa12c812d8b Mon Sep 17 00:00:00 2001 From: Diego Date: Sun, 19 Nov 2023 02:34:16 +0100 Subject: [PATCH 05/19] gitignore --- hole-punch-interop/impl/nim/.gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 hole-punch-interop/impl/nim/.gitignore diff --git a/hole-punch-interop/impl/nim/.gitignore b/hole-punch-interop/impl/nim/.gitignore new file mode 100644 index 000000000..c3a35de68 --- /dev/null +++ b/hole-punch-interop/impl/nim/.gitignore @@ -0,0 +1,6 @@ +nim-libp2p-*.zip +nim-libp2p-* +nim-libp2p +nim-libp2p-*/* +image.json +hole_punching.nim From 92da7e8e8ae46a5e2ff2d857132ade856eab779c Mon Sep 17 00:00:00 2001 From: Diego Date: Sun, 19 Nov 2023 02:53:56 +0100 Subject: [PATCH 06/19] send logs to stderr --- hole-punch-interop/impl/nim/v1.1/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Dockerfile b/hole-punch-interop/impl/nim/v1.1/Dockerfile index f8044a8da..e1d1602d8 100644 --- a/hole-punch-interop/impl/nim/v1.1/Dockerfile +++ b/hole-punch-interop/impl/nim/v1.1/Dockerfile @@ -13,7 +13,7 @@ COPY ../hole_punching.nim nim-libp2p/hole_punching.nim RUN --mount=type=cache,target=/root/.nimble \ --mount=type=cache,target=/root/.cache/nim \ - cd nim-libp2p && nim c --nimcache:root/.cache/nim -d:chronicles_log_level=DEBUG --threads:off -d:release -o:hole-punching-tests hole_punching.nim + cd nim-libp2p && nim c --nimcache:root/.cache/nim -d:chronicles_log_level=DEBUG -d:chronicles_default_output_device=stderr --threads:off -d:release -o:hole-punching-tests hole_punching.nim RUN ls -la /root/.cache/nim From 2453ca18f1439117ecf68aa045429a12ffe5b9cc Mon Sep 17 00:00:00 2001 From: Diego Date: Sun, 19 Nov 2023 03:18:13 +0100 Subject: [PATCH 07/19] update nim version --- hole-punch-interop/impl/nim/v1.1/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 15e5cda1c..4a344f461 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := 9abe5e34ce39a06d861043be2076d194f2dba653 +commitSha := 60f96ec85948cc401a2c8e23e658fce33f7294ef all: image.json @@ -25,4 +25,4 @@ clean: rm -f nim-libp2p rm -rf nim-libp2p-* -.PHONY: all clean \ No newline at end of file +.PHONY: all clean From ea34a0ceec3e5b2a6e2b7f1a3b792df9cba3cebf Mon Sep 17 00:00:00 2001 From: Diego Date: Tue, 21 Nov 2023 18:25:41 +0100 Subject: [PATCH 08/19] update nim version --- hole-punch-interop/impl/nim/v1.1/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 4a344f461..26cdc303d 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := 60f96ec85948cc401a2c8e23e658fce33f7294ef +commitSha := ce0685c272e41614e97583f0a8dec2b6506913fd all: image.json From 2f6a472934219dfb1453f73336b2d41096ded9c1 Mon Sep 17 00:00:00 2001 From: Diego Date: Wed, 22 Nov 2023 01:03:13 +0100 Subject: [PATCH 09/19] update nim version --- hole-punch-interop/impl/nim/v1.1/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 26cdc303d..4f0b9e711 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := ce0685c272e41614e97583f0a8dec2b6506913fd +commitSha := daaa99fd5af02878bbb8e3f8dfc6ec291329cebc all: image.json From aa8b4aa2e868b518aa127901724325e7f705e270 Mon Sep 17 00:00:00 2001 From: Diego Date: Wed, 22 Nov 2023 01:09:26 +0100 Subject: [PATCH 10/19] update nim version --- hole-punch-interop/impl/nim/v1.1/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 4f0b9e711..5bbb689c8 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := daaa99fd5af02878bbb8e3f8dfc6ec291329cebc +commitSha := 5bd55bedf2fd3f78b884d2a7581129cdd924e786 all: image.json From 8eb175eaa3ff9a3da0ca0af776c8fdba7a011623 Mon Sep 17 00:00:00 2001 From: Diego Date: Wed, 29 Nov 2023 13:24:00 +0100 Subject: [PATCH 11/19] update nim version --- hole-punch-interop/impl/nim/v1.1/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 5bbb689c8..997087f74 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := 5bd55bedf2fd3f78b884d2a7581129cdd924e786 +commitSha := 9d0cfd555fecd45d9d26995236e6e2c74f8567d2 all: image.json From dc544a1ac60c66ada2c21111922539895157841e Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 18:49:26 +0100 Subject: [PATCH 12/19] remove flag --- hole-punch-interop/dockerBuildWrapper.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/hole-punch-interop/dockerBuildWrapper.sh b/hole-punch-interop/dockerBuildWrapper.sh index 377956ce5..087a4e8c9 100755 --- a/hole-punch-interop/dockerBuildWrapper.sh +++ b/hole-punch-interop/dockerBuildWrapper.sh @@ -10,5 +10,4 @@ fi docker buildx build \ --load \ - --progress=plain \ -t $IMAGE_NAME $CACHING_OPTIONS "$@" From 772fd3a98becce3b4a1cf96d6a1f395d66878619 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 22:22:33 +0100 Subject: [PATCH 13/19] improve docker cache --- hole-punch-interop/impl/nim/v1.1/Dockerfile | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Dockerfile b/hole-punch-interop/impl/nim/v1.1/Dockerfile index e1d1602d8..228a0247d 100644 --- a/hole-punch-interop/impl/nim/v1.1/Dockerfile +++ b/hole-punch-interop/impl/nim/v1.1/Dockerfile @@ -1,24 +1,17 @@ # syntax=docker/dockerfile:1.5-labs FROM nimlang/nim:1.6.14 as builder -# Run with access to the target cache to speed up builds WORKDIR /workspace -COPY nim-libp2p nim-libp2p +COPY nim-libp2p/.pinned nim-libp2p/libp2p.nimble nim-libp2p/ -# Cache the nimcache and nimble directories to speed up builds RUN cd nim-libp2p && nimble install_pinned && nimble install redis -y -COPY ../hole_punching.nim nim-libp2p/hole_punching.nim +COPY nim-libp2p ../hole_punching.nim nim-libp2p/ -RUN --mount=type=cache,target=/root/.nimble \ - --mount=type=cache,target=/root/.cache/nim \ +RUN --mount=type=cache,target=/root/.cache/nim \ cd nim-libp2p && nim c --nimcache:root/.cache/nim -d:chronicles_log_level=DEBUG -d:chronicles_default_output_device=stderr --threads:off -d:release -o:hole-punching-tests hole_punching.nim -RUN ls -la /root/.cache/nim - -RUN echo $(dpkg --print-architecture) - FROM --platform=linux/amd64 debian:bookworm-slim RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y dnsutils jq curl tcpdump iproute2 COPY --from=builder /workspace/nim-libp2p/hole-punching-tests /usr/bin/hole-punch-client From 53fb81fa29879c837fac25ebea705e71e3448765 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 22:23:47 +0100 Subject: [PATCH 14/19] update nim-libp2p version --- hole-punch-interop/impl/nim/v1.1/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 997087f74..bd866ad64 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,5 +1,5 @@ image_name := nim-v1.1 -commitSha := 9d0cfd555fecd45d9d26995236e6e2c74f8567d2 +commitSha := deb72c8580c5ab7419f1a07381164d64ff5f6005 all: image.json From c9c2555d5ebabd4bf000e9d20d52896b7718f360 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 23:00:38 +0100 Subject: [PATCH 15/19] remove wrong clean --- hole-punch-interop/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hole-punch-interop/Makefile b/hole-punch-interop/Makefile index aba1ade02..787e23184 100644 --- a/hole-punch-interop/Makefile +++ b/hole-punch-interop/Makefile @@ -23,4 +23,5 @@ clean: $(MAKE) -C $$dir clean; \ done -.PHONY: rust-relay router all $(ALL_SUBDIRS) clean +.PHONY: rust-relay router all $(ALL_SUBDIRS) + From 4d0b61e7a630822fcde2d84f9a99d616561537dc Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 23:23:29 +0100 Subject: [PATCH 16/19] improve clean --- hole-punch-interop/impl/nim/v1.1/Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index bd866ad64..1f58003a8 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -22,7 +22,8 @@ nim-libp2p-${commitSha}.zip: wget -O $@ "https://github.com/status-im/nim-libp2p/archive/${commitSha}.zip" clean: - rm -f nim-libp2p - rm -rf nim-libp2p-* + rm -f image.json + rm -f hole_punching.nim + rm -rf nim-libp2p* .PHONY: all clean From 21fcab2345ef80b30d7ce393b465bbfc4987147f Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 30 Nov 2023 23:24:03 +0100 Subject: [PATCH 17/19] doesn't fail if file doesn't exist --- hole-punch-interop/impl/rust/v0.53/Makefile | 4 ++-- hole-punch-interop/router/Makefile | 2 +- hole-punch-interop/rust-relay/Makefile | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/hole-punch-interop/impl/rust/v0.53/Makefile b/hole-punch-interop/impl/rust/v0.53/Makefile index 500107636..240b14a92 100644 --- a/hole-punch-interop/impl/rust/v0.53/Makefile +++ b/hole-punch-interop/impl/rust/v0.53/Makefile @@ -15,6 +15,6 @@ rust-libp2p-${commitSha}.zip: wget -O $@ "https://github.com/libp2p/rust-libp2p/archive/${commitSha}.zip" clean: - rm image.json - rm rust-libp2p-*.zip + rm -f image.json + rm -f rust-libp2p-*.zip rm -rf rust-libp2p-* diff --git a/hole-punch-interop/router/Makefile b/hole-punch-interop/router/Makefile index 3117f4b4f..8dc2128fb 100644 --- a/hole-punch-interop/router/Makefile +++ b/hole-punch-interop/router/Makefile @@ -7,4 +7,4 @@ image.json: Dockerfile run.sh docker image inspect ${image_name} -f "{{.Id}}" | \ xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ clean: - rm image.json + rm -f image.json diff --git a/hole-punch-interop/rust-relay/Makefile b/hole-punch-interop/rust-relay/Makefile index fd6c21ac8..f30c89832 100644 --- a/hole-punch-interop/rust-relay/Makefile +++ b/hole-punch-interop/rust-relay/Makefile @@ -7,4 +7,4 @@ image.json: Cargo.lock src/** Dockerfile docker image inspect ${image_name} -f "{{.Id}}" | \ xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ clean: - rm image.json + rm -f image.json From b7c59bf34325be1ee78de2914d95a8df7c7ba9c7 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 1 Dec 2023 18:00:13 +0100 Subject: [PATCH 18/19] move test to nim-libp2p --- hole-punch-interop/impl/nim/hole_punching.nim | 114 ------------------ hole-punch-interop/impl/nim/v1.1/Dockerfile | 18 --- hole-punch-interop/impl/nim/v1.1/Makefile | 8 +- 3 files changed, 2 insertions(+), 138 deletions(-) delete mode 100644 hole-punch-interop/impl/nim/hole_punching.nim delete mode 100644 hole-punch-interop/impl/nim/v1.1/Dockerfile diff --git a/hole-punch-interop/impl/nim/hole_punching.nim b/hole-punch-interop/impl/nim/hole_punching.nim deleted file mode 100644 index 6b4fab547..000000000 --- a/hole-punch-interop/impl/nim/hole_punching.nim +++ /dev/null @@ -1,114 +0,0 @@ -import std/[os, options, strformat] -import redis -import chronos, metrics, chronicles -import libp2p/[builders, - switch, - observedaddrmanager, - services/hpservice, - services/autorelayservice, - protocols/connectivity/autonat/client as aclient, - protocols/connectivity/relay/relay, - protocols/connectivity/autonat/service] -import libp2p/protocols/connectivity/relay/client as rclient -import tests/stubs/autonatclientstub -import libp2p/protocols/ping - -proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch = - let rng = newRng() - var builder = SwitchBuilder.new() - .withRng(rng) - .withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ]) - .withObservedAddrManager(ObservedAddrManager.new(maxSize = 1, minCount = 1)) - .withTcpTransport({ServerFlags.TcpNoDelay}) - .withYamux() - .withAutonat() - .withNoise() - - if hpService != nil: - builder = builder.withServices(@[hpService]) - - if r != nil: - builder = builder.withCircuitRelay(r) - - let s = builder.build() - s.mount(Ping.new(rng=rng)) - return s - -proc main() {.async.} = - try: - let relayClient = RelayClient.new() - let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng()) - let autonatClientStub = AutonatClientStub.new(expectedDials = 1) - autonatClientStub.answer = NotReachable - let autonatService = AutonatService.new(autonatClientStub, newRng(), maxQueueSize = 1) - let hpservice = HPService.new(autonatService, autoRelayService) - - let - isListener = getEnv("MODE") == "listen" - switch = createSwitch(relayClient, hpservice) - auxSwitch = createSwitch() - redisClient = open("redis", 6379.Port) - - debug "Connected to redis" - - await switch.start() - await auxSwitch.start() - - let relayAddr = - try: - redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 0) - except Exception as e: - raise newException(CatchableError, e.msg) - - # This is necessary to make the autonat service work. It will ask this peer for our reachability which the autonat - # client stub will answer NotReachable. - await switch.connect(auxSwitch.peerInfo.peerId, auxSwitch.peerInfo.addrs) - - # Wait for autonat to be NotReachable - while autonatService.networkReachability != NetworkReachability.NotReachable: - await sleepAsync(100.milliseconds) - - # This will trigger the autonat relay service to make a reservation. - let relayMA = MultiAddress.init(relayAddr[1]).tryGet() - debug "Got relay address", relayMA - let relayId = await switch.connect(relayMA) - debug "Connected to relay", relayId - - # Wait for our relay address to be published - while switch.peerInfo.addrs.len == 0: - await sleepAsync(100.milliseconds) - - if isListener: - let listenerPeerId = switch.peerInfo.peerId - discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId) - debug "Pushed listener client peer id to redis", listenerPeerId - - # Nothing to do anymore, wait to be killed - await sleepAsync(2.minutes) - else: - let listenerId = - try: - PeerId.init(redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 0)[1]).tryGet() - except Exception as e: - raise newException(CatchableError, e.msg) - - debug "Got listener peer id", listenerId - let listenerRelayAddr = MultiAddress.init($relayMA & "/p2p-circuit").tryGet() - - debug "Dialing listener relay address", listenerRelayAddr - await switch.connect(listenerId, @[listenerRelayAddr]) - - # wait for hole-punching to complete in the background - await sleepAsync(5000.milliseconds) - - let conn = switch.connManager.selectMuxer(listenerId).connection - let channel = await switch.dial(listenerId, @[listenerRelayAddr], PingCodec) - let delay = await Ping.new().ping(channel) - await allFuturesThrowing(channel.close(), conn.close(), switch.stop(), auxSwitch.stop()) - echo &"""{{"rtt_to_holepunched_peer_millis":{delay.millis}}}""" - quit(0) - except Exception as e: - error "Unexpected error", msg = e.msg - -discard waitFor(main().withTimeout(4.minutes)) -quit(1) \ No newline at end of file diff --git a/hole-punch-interop/impl/nim/v1.1/Dockerfile b/hole-punch-interop/impl/nim/v1.1/Dockerfile deleted file mode 100644 index 228a0247d..000000000 --- a/hole-punch-interop/impl/nim/v1.1/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -# syntax=docker/dockerfile:1.5-labs -FROM nimlang/nim:1.6.14 as builder - -WORKDIR /workspace - -COPY nim-libp2p/.pinned nim-libp2p/libp2p.nimble nim-libp2p/ - -RUN cd nim-libp2p && nimble install_pinned && nimble install redis -y - -COPY nim-libp2p ../hole_punching.nim nim-libp2p/ - -RUN --mount=type=cache,target=/root/.cache/nim \ - cd nim-libp2p && nim c --nimcache:root/.cache/nim -d:chronicles_log_level=DEBUG -d:chronicles_default_output_device=stderr --threads:off -d:release -o:hole-punching-tests hole_punching.nim - -FROM --platform=linux/amd64 debian:bookworm-slim -RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y dnsutils jq curl tcpdump iproute2 -COPY --from=builder /workspace/nim-libp2p/hole-punching-tests /usr/bin/hole-punch-client -ENV RUST_BACKTRACE=1 diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 1f58003a8..61961d0a6 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,16 +1,13 @@ image_name := nim-v1.1 -commitSha := deb72c8580c5ab7419f1a07381164d64ff5f6005 +commitSha := 2daeb6e0c61454b57b25f6ac1d8a9102ed26334c all: image.json -image.json: hole_punching.nim nim-libp2p Dockerfile +image.json: nim-libp2p Dockerfile IMAGE_NAME=${image_name} ../../../dockerBuildWrapper.sh . docker image inspect ${image_name} -f "{{.Id}}" | \ xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ -hole_punching.nim: ../hole_punching.nim - cp ../hole_punching.nim hole_punching.nim - nim-libp2p: nim-libp2p-${commitSha} rm -rf nim-libp2p ln -s nim-libp2p-${commitSha} nim-libp2p @@ -23,7 +20,6 @@ nim-libp2p-${commitSha}.zip: clean: rm -f image.json - rm -f hole_punching.nim rm -rf nim-libp2p* .PHONY: all clean From b4787bfd009027eda8a23c002bc9809c55a8365b Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 1 Dec 2023 21:12:13 +0100 Subject: [PATCH 19/19] improve make file and use new nim-libp2p version --- hole-punch-interop/impl/nim/v1.1/Makefile | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/hole-punch-interop/impl/nim/v1.1/Makefile b/hole-punch-interop/impl/nim/v1.1/Makefile index 61961d0a6..220078b2d 100644 --- a/hole-punch-interop/impl/nim/v1.1/Makefile +++ b/hole-punch-interop/impl/nim/v1.1/Makefile @@ -1,17 +1,13 @@ image_name := nim-v1.1 -commitSha := 2daeb6e0c61454b57b25f6ac1d8a9102ed26334c +commitSha := 93925ac28b04cd94d06a332412973e90f80f35df all: image.json -image.json: nim-libp2p Dockerfile - IMAGE_NAME=${image_name} ../../../dockerBuildWrapper.sh . +image.json: nim-libp2p-${commitSha} + cd nim-libp2p-${commitSha} && IMAGE_NAME=${image_name} ../../../../dockerBuildWrapper.sh -f tests/hole-punching-interop/Dockerfile . docker image inspect ${image_name} -f "{{.Id}}" | \ xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ -nim-libp2p: nim-libp2p-${commitSha} - rm -rf nim-libp2p - ln -s nim-libp2p-${commitSha} nim-libp2p - nim-libp2p-${commitSha}: nim-libp2p-${commitSha}.zip unzip -o nim-libp2p-${commitSha}.zip