Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions nim-test-node/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/nimble.paths
**/nimble.develop
**/nimbledeps
**/nimcache*
**/main
**/build
26 changes: 26 additions & 0 deletions nim-test-node/common/health_server.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import chronos
import chronos/apps/http/httpserver

proc startHealthServer*(port: Port): Future[HttpServerRef] {.async.} =
## Start a health server exposing GET /health and GET /ready.
proc handler(request: RequestFence): Future[HttpResponseRef] {.async.} =
if request.isErr():
return defaultResponse()

let req = request.get()
if req.meth == MethodGet and (req.uri.path == "/health" or req.uri.path == "/ready"):
return await req.respond(
Http200, "ok", HttpTable.init([("Content-Type", "text/plain")])
)

return await req.respond(Http404, "Not Found")

let serverRes = HttpServerRef.new(initTAddress("0.0.0.0:" & $port), handler)
if serverRes.isErr():
raise newException(
CatchableError, "Failed to create health HTTP server: " & $serverRes.error
)

let server = serverRes.get()
server.start()
return server
9 changes: 9 additions & 0 deletions nim-test-node/common/peer_ids.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import libp2p/peerid

proc randomPeerId*[R](rng: R): PeerId =
## Generate a random peer ID using the caller's random number generator.
PeerId.random(rng).get()

proc randomPeerIds*[R](rng: R, count: Natural): seq[PeerId] =
## Generate `count` random peer IDs using the same random number generator.
PeerId.random(count.uint, rng).get()
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chronos

proc waitShutdownSignal*() {.async.} =
## Wait until the process receives SIGINT or SIGTERM.
let
sigIntFut = waitSignal(SIGINT)
sigTermFut = waitSignal(SIGTERM)
Expand Down
11 changes: 6 additions & 5 deletions nim-test-node/connmanager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ RUN apt-get update && \
RUN curl https://nim-lang.org/choosenim/init.sh -sSf | sh -s -- -y
ENV PATH="/root/.nimble/bin:${PATH}"

WORKDIR /node
WORKDIR /node/connmanager

COPY test_node.nimble .
COPY connmanager/test_node.nimble .

RUN git config --global http.sslVerify false

RUN --mount=type=cache,target=/root/.nimble/pkgs \
--mount=type=cache,target=/root/.nimble/pkgs2 \
nimble install -dy
nimble install -dy --useSystemNim

COPY . .
COPY common /node/common
COPY connmanager/. .

RUN nimble c \
-d:chronicles_colors=None \
Expand All @@ -48,7 +49,7 @@ RUN apt-get update && \

WORKDIR /node

COPY --from=build /node/main /node/main
COPY --from=build /node/connmanager/main /node/main

RUN chmod +x main

Expand Down
3 changes: 2 additions & 1 deletion nim-test-node/connmanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ nimble c \

To build with Docker:
```
docker buildx build --platform linux/amd64 -t radiken/dst-test-node-connmanager:latest --push .
cd ..
docker buildx build --platform linux/amd64 -f connmanager/Dockerfile -t radiken/dst-test-node-connmanager:latest --push .
```

## Environment variables
Expand Down
10 changes: 4 additions & 6 deletions nim-test-node/connmanager/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import metrics, metrics/chronos_httpserver
import libp2p, libp2p/[multiaddress, crypto/secp]
from nativesockets import getHostname
import env
import ../common/shutdown

logScope:
topics = "dst"
Expand Down Expand Up @@ -92,8 +93,7 @@ proc runHub(cfg: HubConfig) {.async.} =
info "Dialing peer hub", target = hubAddr
asyncSpawn resolveAndConnect(switch, hubAddr)

while true:
await sleepAsync(1.hours)
await waitShutdownSignal()

proc runPeer(cfg: PeerConfig) {.async.} =
var builder = SwitchBuilder
Expand Down Expand Up @@ -142,11 +142,9 @@ proc runPeer(cfg: PeerConfig) {.async.} =
else:
for addr in cfg.hubAddrs:
await resolveAndConnect(switch, addr)
while true:
await sleepAsync(1.hours)
await waitShutdownSignal()
else:
while true:
await sleepAsync(1.hours)
await waitShutdownSignal()

proc main() {.async.} =
case getRole()
Expand Down
4 changes: 3 additions & 1 deletion nim-test-node/connmanager/test_node.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ description = "Connection manager test node"
license = "MIT"

requires "nim >= 2.2.0",
"nimcrypto 0.6.4", "libp2p#7cc4280e2efd5e6c2ebd732ce33d309376a9627e"
"nimcrypto >= 0.6.4",
"lsquic == 0.9.0",
"libp2p#7cc4280e2efd5e6c2ebd732ce33d309376a9627e"
9 changes: 5 additions & 4 deletions nim-test-node/gossipsub-queues/Dockerfile_amd64
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM debian:bookworm-slim AS builder

WORKDIR /node
WORKDIR /node/gossipsub-queues

COPY . .
COPY common /node/common
COPY gossipsub-queues/. .
Comment on lines +5 to +6

RUN apt-get update && apt-get install -y \
curl git build-essential ca-certificates \
Expand Down Expand Up @@ -57,10 +58,10 @@ RUN nimble c \
--passL:"-static-libgcc -static-libstdc++" \
main

RUN chmod +x /node/main
RUN chmod +x /node/gossipsub-queues/main
RUN nim --version > nim_version
RUN nimble --version > nimble_version

EXPOSE 5000 8008 8645

ENTRYPOINT ["/node/main"]
ENTRYPOINT ["/node/gossipsub-queues/main"]
9 changes: 5 additions & 4 deletions nim-test-node/gossipsub-queues/Dockerfile_arm64
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM nimlang/nim:latest AS builder

WORKDIR /node
WORKDIR /node/gossipsub-queues

COPY . .
COPY common /node/common
COPY gossipsub-queues/. .
Comment on lines +5 to +6

RUN apt-get update && apt-get install -y \
curl git build-essential ca-certificates \
Expand Down Expand Up @@ -40,10 +41,10 @@ RUN nimble c \
main


RUN chmod +x /node/main
RUN chmod +x /node/gossipsub-queues/main
RUN nim --version > nim_version
RUN nimble --version > nimble_version

EXPOSE 5000 8008 8645

ENTRYPOINT ["/node/main"]
ENTRYPOINT ["/node/gossipsub-queues/main"]
3 changes: 2 additions & 1 deletion nim-test-node/gossipsub-queues/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import sequtils, math, metrics, metrics/chronos_httpserver
from times import getTime, Time, toUnix, fromUnix, `-`, initTime, `$`, inMilliseconds
from times import getTime, toUnixFloat, `-`, initTime, `$`, inMilliseconds, Time
from nativesockets import getHostname
import ../common/shutdown

logScope:
topics = "dst"
Expand Down Expand Up @@ -511,6 +512,6 @@ proc main() {.async.} =
info "Starting listening endpoint for publish controller"
discard gossipSub.startHttpServer(myId)

await sleepAsync(2.days)
await waitShutdownSignal()

waitFor(main())
9 changes: 5 additions & 4 deletions nim-test-node/kad-dht/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ RUN apt-get update && \
RUN curl https://nim-lang.org/choosenim/init.sh -sSf | sh -s -- -y
ENV PATH="/root/.nimble/bin:${PATH}"

WORKDIR /node
COPY . .
WORKDIR /node/kad-dht
COPY common /node/common
COPY kad-dht/. .

RUN git config --global http.sslVerify false

Expand All @@ -41,10 +42,10 @@ RUN apt-get update && \

WORKDIR /node

COPY --from=build /node/main /node/main
COPY --from=build /node/kad-dht/main /node/main

RUN chmod +x main

EXPOSE 5000 8008 8645

ENTRYPOINT ["./main"]
ENTRYPOINT ["./main"]
2 changes: 2 additions & 0 deletions nim-test-node/kad-dht/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ This commit is already set in .nimble file.

If you want to build with Docker, you can do it with:
```
cd ..
docker buildx build \
--platform linux/amd64 \
-f kad-dht/Dockerfile \
-t docker.io/<your_registry>/<your_image_tag> \
--push \
.
Expand Down
12 changes: 6 additions & 6 deletions nim-test-node/kad-dht/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sequtils, math, metrics, metrics/chronos_httpserver
import chronicles
import env
import helpers
import ../common/peer_ids

logScope:
topics = "dst"
Expand All @@ -14,7 +15,7 @@ from times import getTime, Time, toUnix, fromUnix, `-`, initTime, `$`, inMillise

# --- Core Logic ---

proc runWarmup*(kad: KadDHT, selfId: PeerId) {.async.} =
proc runWarmup*[R](kad: KadDHT, selfId: PeerId, rng: R) {.async.} =
info "Starting warmup phase"

# 5x FIND_NODE(self)
Expand All @@ -30,21 +31,20 @@ proc runWarmup*(kad: KadDHT, selfId: PeerId) {.async.} =
await sleepAsync(1.seconds)

# 15x FIND_NODE(random)
for i in 1 .. 15:
let target = getRandomPeerId()
debug "Warmup: Finding random node", iteration = i, target = target
for i, target in randomPeerIds(rng, 15):
debug "Warmup: Finding random node", iteration = i + 1, target = target

let peers = await kad.findNode(target.toKey())

await sleepAsync(2.seconds)

info "Warmup complete"

proc runProbe*(kad: KadDHT) {.async.} =
proc runProbe*[R](kad: KadDHT, rng: R) {.async.} =
info "Starting probe loop"
while true:
let
targetPeer = getRandomPeerId()
targetPeer = randomPeerId(rng)
targetKey = targetPeer.toKey()

try:
Expand Down
34 changes: 1 addition & 33 deletions nim-test-node/kad-dht/helpers.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import libp2p
import chronos, chronos/apps/http/httpserver
import chronos
import chronicles
import libp2p/protocols/[kademlia, kad_disco]
import os
Expand All @@ -8,13 +8,6 @@ import env
logScope:
topics = "dst"

# --- Helpers ---

proc getRandomPeerId*(): PeerId =
# Generates a random peer ID for FIND_NODE targets
let rng = newRng()
return PeerId.init(PrivateKey.random(Secp256k1, rng[]).get()).get()

proc buildSwitch*(muxer: string, address: string): Switch =
var builder = SwitchBuilder
.new()
Expand Down Expand Up @@ -91,28 +84,3 @@ proc connectToBootstraps*(
)

ok(bootstraps)

proc startHealthServer*(port: Port): Future[HttpServerRef] {.async.} =
proc handler(request: RequestFence): Future[HttpResponseRef] {.async.} =
if request.isErr():
return defaultResponse()

let req = request.get()

if req.meth == MethodGet and (req.uri.path == "/health" or req.uri.path == "/ready"):
return await req.respond(
Http200, "ok", HttpTable.init([("Content-Type", "text/plain")])
)

return await req.respond(Http404, "Not Found")

let addrs = initTAddress("0.0.0.0:" & $port)
let serverRes = HttpServerRef.new(addrs, handler)
if serverRes.isErr():
raise newException(
CatchableError, "Failed to create health HTTP server: " & $serverRes.error
)

let server = serverRes.get()
server.start()
return server
14 changes: 6 additions & 8 deletions nim-test-node/kad-dht/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ from times import getTime, Time, toUnix, fromUnix, `-`, initTime, `$`, inMillise
from nativesockets import getHostname
import helpers
import core
import ../common/health_server
import ../common/shutdown

logScope:
topics = "dst"
Expand Down Expand Up @@ -40,8 +42,7 @@ proc main() {.async.} =
var kad = await mountDiscovery(switch, discovery, @[])
discard await startHealthServer(prometheusPort)
# Just stay alive and serve queries
while true:
await sleepAsync(1.hours)
await waitShutdownSignal()
of RoleNormal:
let jitter = myId * 200
if jitter > 0:
Expand All @@ -54,11 +55,10 @@ proc main() {.async.} =
quit(1)

var kad = await mountDiscovery(switch, discovery, bootAddresses)
await runWarmup(kad, selfId)
await runWarmup(kad, selfId, rng)
discard await startHealthServer(prometheusPort)
# Keep node alive for steady state refresh
while true:
await sleepAsync(1.hours)
await waitShutdownSignal()
of RoleProbe:
let jitter = myId * 200
if jitter > 0:
Expand All @@ -71,8 +71,6 @@ proc main() {.async.} =
quit(1)

var kad = await mountDiscovery(switch, discovery, bootAddresses)
await runProbe(kad)
while true:
await sleepAsync(1.hours)
await runProbe(kad, rng)

waitFor(main())
7 changes: 4 additions & 3 deletions nim-test-node/regression/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG NIM_BUILDPLATFORM=linux/amd64
FROM --platform=${NIM_BUILDPLATFORM} nimlang/nim:2.2.10 AS builder

WORKDIR /node
WORKDIR /node/regression

RUN apt-get update && apt-get install -y --no-install-recommends \
git \
Expand All @@ -21,10 +21,11 @@ RUN git config --global http.version HTTP/1.1
RUN nimble install -y 'nimble@#07caee397d628c9e93d81048268365c4c2414a80'
ENV PATH="/root/.nimble/bin:${PATH}"

COPY test_node.nimble nimble.lock config.nims ./
COPY regression/test_node.nimble regression/nimble.lock regression/config.nims ./
RUN nimble refresh && nimble setup

COPY . .
COPY common /node/common
COPY regression/. .

ARG TARGETARCH
ARG NODE_BINARY=node
Expand Down
Loading
Loading