Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nim hole punching tests #322

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions hole-punch-interop/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
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 $(ALL_SUBDIRS)

.PHONY: rust-relay router all $(RUST_SUBDIRS) $(GO_SUBDIRS)
6 changes: 6 additions & 0 deletions hole-punch-interop/impl/nim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nim-libp2p-*.zip
nim-libp2p-*
nim-libp2p
nim-libp2p-*/*
image.json
hole_punching.nim
114 changes: 114 additions & 0 deletions hole-punch-interop/impl/nim/hole_punching.nim
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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)
18 changes: 18 additions & 0 deletions hole-punch-interop/impl/nim/v1.1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
29 changes: 29 additions & 0 deletions hole-punch-interop/impl/nim/v1.1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
image_name := nim-v1.1
commitSha := deb72c8580c5ab7419f1a07381164d64ff5f6005

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 image.json
rm -f hole_punching.nim
rm -rf nim-libp2p*

.PHONY: all clean
4 changes: 2 additions & 2 deletions hole-punch-interop/impl/rust/v0.53/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
rm -f rust-libp2p-*.zip
rm -rf rust-libp2p-*
2 changes: 1 addition & 1 deletion hole-punch-interop/router/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion hole-punch-interop/rust-relay/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions hole-punch-interop/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const versions: Array<Version> = [
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 {
Expand Down