-
Notifications
You must be signed in to change notification settings - Fork 78
feat: add nim hole punching tests #322
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
Open
diegomrsantos
wants to merge
19
commits into
libp2p:master
Choose a base branch
from
diegomrsantos:feat/add-nim-hole-punch-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c830079
add nim test basic version
diegomrsantos 774a7c7
add ping
diegomrsantos 186e77a
Advancing the test
diegomrsantos 6a4d45e
finishing test
diegomrsantos fef9abc
gitignore
diegomrsantos 92da7e8
send logs to stderr
diegomrsantos 2453ca1
update nim version
diegomrsantos ea34a0c
update nim version
diegomrsantos 2f6a472
update nim version
diegomrsantos aa8b4aa
update nim version
diegomrsantos 8eb175e
update nim version
diegomrsantos dc544a1
remove flag
diegomrsantos 772fd3a
improve docker cache
diegomrsantos 53fb81f
update nim-libp2p version
diegomrsantos c9c2555
remove wrong clean
diegomrsantos 4d0b61e
improve clean
diegomrsantos 21fcab2
doesn't fail if file doesn't exist
diegomrsantos b7c59bf
move test to nim-libp2p
diegomrsantos b4787bf
improve make file and use new nim-libp2p version
diegomrsantos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.