From 05a010a3df307018b1c962d1a91dc0665c234784 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Wed, 15 Feb 2023 12:47:39 +0100 Subject: [PATCH] Initial nim support --- multidim-interop/nim/v1.0/Dockerfile | 13 +++++ multidim-interop/nim/v1.0/Makefile | 11 ++++ multidim-interop/nim/v1.0/main.nim | 81 +++++++++++++++++++++++++++ multidim-interop/nim/v1.0/main.nimble | 13 +++++ 4 files changed, 118 insertions(+) create mode 100644 multidim-interop/nim/v1.0/Dockerfile create mode 100644 multidim-interop/nim/v1.0/Makefile create mode 100644 multidim-interop/nim/v1.0/main.nim create mode 100644 multidim-interop/nim/v1.0/main.nimble diff --git a/multidim-interop/nim/v1.0/Dockerfile b/multidim-interop/nim/v1.0/Dockerfile new file mode 100644 index 0000000..1d00929 --- /dev/null +++ b/multidim-interop/nim/v1.0/Dockerfile @@ -0,0 +1,13 @@ +ARG NimVersion="latest" +FROM nimlang/nim:${NimVersion}-alpine as builder + +WORKDIR /app + +COPY main.nimble . +RUN nim -v +RUN nimble install -dy + +COPY main.nim . +RUN nim c -d:chronicles_log_level=WARN --threads:off main.nim + +ENTRYPOINT ["main"] diff --git a/multidim-interop/nim/v1.0/Makefile b/multidim-interop/nim/v1.0/Makefile new file mode 100644 index 0000000..83f9e81 --- /dev/null +++ b/multidim-interop/nim/v1.0/Makefile @@ -0,0 +1,11 @@ +image_name := nim-v1.0 + +image.json: Dockerfile main.nim + IMAGE_NAME=${image_name} ../../dockerBuildWrapper.sh . + docker image inspect ${image_name} -f "{{.Id}}" | \ + xargs -I {} echo "{\"imageID\": \"{}\"}" > $@ + +.PHONY: clean + +clean: + rm image.json diff --git a/multidim-interop/nim/v1.0/main.nim b/multidim-interop/nim/v1.0/main.nim new file mode 100644 index 0000000..7430c20 --- /dev/null +++ b/multidim-interop/nim/v1.0/main.nim @@ -0,0 +1,81 @@ +import + std/[os, strutils], + chronos, redis, serialization, json_serialization, + libp2p, libp2p/protocols/ping, libp2p/transports/wstransport + +type + ResultJson = object + handshakePlusOneRTTMillis: float + pingRTTMilllis: float + +let + testTimeout = + try: seconds(parseInt(getEnv("test_timeout_seconds"))) + except CatchableError: 3.minutes + +proc main {.async.} = + let + transport = getEnv("transport") + muxer = getEnv("muxer") + secureChannel = getEnv("security") + isDialer = getEnv("is_dialer") == "true" + ip = getEnv("ip", "0.0.0.0") + redisAddr = getEnv("redis_addr", "redis:6379").split(":") + + # using synchronous redis because async redis is based on + # asyncdispatch instead of chronos + redisClient = open(redisAddr[0], Port(parseInt(redisAddr[1]))) + + switchBuilder = SwitchBuilder.new() + + case "transport": + of "tcp": + discard switchBuilder.withTcpTransport().withAddress( + MultiAddress.init("/ip4/" & ip & "/tcp/0").tryGet() + ) + of "ws": + discard switchBuilder.withTransport(proc (upgr: Upgrade): Transport = WsTransport.new(upgr)).withAddress( + MultiAddress.init("/ip4/" & ip & "/tcp/0/ws").tryGet() + ) + else: doAssert false + + case secureChannel: + of "noise": discard switchBuilder.withNoise() + else: doAssert false + + case muxer: + of "yamux": discard switchBuilder.withYamux() + of "mplex": discard switchBuilder.withMplex() + else: doAssert false + + let + rng = libp2p.newRng() + switch = switchBuilder.withRng(rng).build() + pingProtocol = Ping.new(rng = rng) + switch.mount(pingProtocol) + await switch.start() + defer: await switch.stop() + + if not isDialer: + discard redisClient.rPush("listenerAddr", $switch.peerInfo.fullAddrs.tryGet()[0]) + await sleepAsync(100.hours) # will get cancelled + else: + let + remoteAddr = MultiAddress.init(redisClient.bLPop(@["listenerAddr"], testTimeout.seconds.int)[1]).tryGet() + dialingStart = Moment.now() + remotePeerId = await switch.connect(remoteAddr) + stream = await switch.dial(remotePeerId, PingCodec) + pingDelay = await pingProtocol.ping(stream) + totalDelay = Moment.now() - dialingStart + await stream.close() + + echo Json.encode( + ResultJson( + handshakePlusOneRTTMillis: float(totalDelay.milliseconds), + pingRTTMilllis: float(pingDelay.milliseconds) + ) + ) + quit(0) + +discard waitFor(main().withTimeout(testTimeout)) +quit(1) diff --git a/multidim-interop/nim/v1.0/main.nimble b/multidim-interop/nim/v1.0/main.nimble new file mode 100644 index 0000000..d5ade6f --- /dev/null +++ b/multidim-interop/nim/v1.0/main.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "The author" +description = "Demo for another package" +license = "MIT" + + +# Dependencies + +requires "nim >= 1.2.2", + "libp2p == 1.0.0", + "redis == 0.4.0"