From ef74e1afd24df0d48429288ebcb98a04e7172464 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Sat, 29 Mar 2025 17:01:19 +0530 Subject: [PATCH] lightpush with config and metrics --- Dockerfile.lightpushWithMix.compile | 57 ++++++++++++++++++++ examples/lightpush_publisher_mix.nim | 23 ++++---- examples/lightpush_publisher_mix_config.nim | 12 +++++ examples/lightpush_publisher_mix_metrics.nim | 7 +++ 4 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 Dockerfile.lightpushWithMix.compile create mode 100644 examples/lightpush_publisher_mix_metrics.nim diff --git a/Dockerfile.lightpushWithMix.compile b/Dockerfile.lightpushWithMix.compile new file mode 100644 index 000000000..fc7e01d0d --- /dev/null +++ b/Dockerfile.lightpushWithMix.compile @@ -0,0 +1,57 @@ +# BUILD NIM APP ---------------------------------------------------------------- +FROM rust:1.77.1-alpine3.18 AS nim-build + +ARG NIMFLAGS +ARG MAKE_TARGET=lightpushwithmix +ARG NIM_COMMIT +ARG LOG_LEVEL=TRACE + +# Get build tools and required header files +RUN apk add --no-cache bash git build-base openssl-dev pcre-dev linux-headers curl jq + +WORKDIR /app +COPY . . + +# workaround for alpine issue: https://github.com/alpinelinux/docker-alpine/issues/383 +RUN apk update && apk upgrade + +# Ran separately from 'make' to avoid re-doing +RUN git submodule update --init --recursive + +# Slowest build step for the sake of caching layers +RUN make -j$(nproc) deps QUICK_AND_DIRTY_COMPILER=1 ${NIM_COMMIT} + +# Build the final node binary +RUN make -j$(nproc) ${NIM_COMMIT} $MAKE_TARGET LOG_LEVEL=${LOG_LEVEL} NIMFLAGS="${NIMFLAGS}" + + +# REFERENCE IMAGE as BASE for specialized PRODUCTION IMAGES---------------------------------------- +FROM alpine:3.18 AS base_lpt + +ARG MAKE_TARGET=lightpushwithmix + +LABEL maintainer="prem@waku.org" +LABEL source="https://github.com/waku-org/nwaku" +LABEL description="Lite Push With Mix: Waku light-client" +LABEL commit="unknown" +LABEL version="unknown" + +# DevP2P, LibP2P, and JSON RPC ports +EXPOSE 30303 60000 8545 + +# Referenced in the binary +RUN apk add --no-cache libgcc pcre-dev libpq-dev \ + wget \ + iproute2 \ + python3 + +# Fix for 'Error loading shared library libpcre.so.3: No such file or directory' +RUN ln -s /usr/lib/libpcre.so /usr/lib/libpcre.so.3 + +COPY --from=nim-build /app/build/lightpush_publisher_mix /usr/bin/ +RUN chmod +x /usr/bin/lightpush_publisher_mix + +# Standalone image to be used manually and in lpt-runner ------------------------------------------- +FROM base_lpt AS standalone_lpt + +ENTRYPOINT ["/usr/bin/lightpush_publisher_mix"] diff --git a/examples/lightpush_publisher_mix.nim b/examples/lightpush_publisher_mix.nim index ed9be1b00..461e0c83a 100644 --- a/examples/lightpush_publisher_mix.nim +++ b/examples/lightpush_publisher_mix.nim @@ -10,7 +10,8 @@ import libp2p/crypto/curve25519, libp2p/multiaddress, eth/keys, - eth/p2p/discoveryv5/enr + eth/p2p/discoveryv5/enr, + metrics import mix/entry_connection, mix/protocol @@ -26,7 +27,8 @@ import factory/builder, waku_lightpush/client ], - ./lightpush_publisher_mix_config + ./lightpush_publisher_mix_config, + ./lightpush_publisher_mix_metrics proc now*(): Timestamp = @@ -88,12 +90,7 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = @[MultiAddress.init(conf.destPeerAddr).get()], ) node.peerManager.addServicePeer(pxPeerInfo, WakuPeerExchangeCodec) -#[ let pxPeerInfo2 = RemotePeerInfo.init( - "16Uiu2HAmRhxmCHBYdXt1RibXrjAUNJbduAhzaTHwFCZT4qWnqZAu", - @[MultiAddress.init("/ip4/127.0.0.1/tcp/60005").get()], - ) - node.peerManager.addServicePeer(pxPeerInfo2, WakuPeerExchangeCodec) - ]# + ( await node.mountMix( intoCurve25519Key( @@ -105,7 +102,6 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = ).isOkOr: error "failed to mount waku mix protocol: ", error = $error return - #discard node.setMixBootStrapNodes() let destPeerId = PeerId.init(conf.destPeerId).valueOr: error "Failed to initialize PeerId", err = error @@ -125,15 +121,14 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = (await node.fetchPeerExchangePeers()).isOkOr: warn "Cannot fetch peers from peer exchange", cause = error - while node.getMixNodePoolSize() < 3: + while node.getMixNodePoolSize() < conf.minMixPoolSize: info "waiting for mix nodes to be discovered", currentpoolSize = node.getMixNodePoolSize() await sleepAsync(1000) - notice "publisher service started" - var numMsgs = 4 + notice "publisher service started with mix node pool size ", currentpoolSize = node.getMixNodePoolSize() var i = 0 - while i < numMsgs: + while i < conf.numMsgs: i = i + 1 let text = "hi there i'm a publisher using mix, this is msg number " & $i let message = WakuMessage( @@ -148,6 +143,7 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = ) if res.isOk: + lp_mix_success.inc() notice "published message", text = text, timestamp = message.timestamp, @@ -155,6 +151,7 @@ proc setupAndPublish(rng: ref HmacDrbgContext, conf: LPMixConf) {.async.} = contentTopic = LightpushContentTopic else: error "failed to publish message", error = res.error + lp_mix_failed.inc(labelValues = ["publish_error"]) await sleepAsync(1000) diff --git a/examples/lightpush_publisher_mix_config.nim b/examples/lightpush_publisher_mix_config.nim index 2a9d5f33b..41bfcd7fc 100644 --- a/examples/lightpush_publisher_mix_config.nim +++ b/examples/lightpush_publisher_mix_config.nim @@ -19,4 +19,16 @@ type desc: "Port to listen on.", defaultValue: 50000, name: "port", + }: int + + numMsgs* {. + desc: "Number of messages to send.", + defaultValue: 1, + name: "num-msgs", + }: int + + minMixPoolSize* {. + desc: "Number of messages to wait for before sending.", + defaultValue: 3, + name: "mix-mix-pool-size", }: int \ No newline at end of file diff --git a/examples/lightpush_publisher_mix_metrics.nim b/examples/lightpush_publisher_mix_metrics.nim new file mode 100644 index 000000000..541678071 --- /dev/null +++ b/examples/lightpush_publisher_mix_metrics.nim @@ -0,0 +1,7 @@ +{.push raises: [].} + +import metrics + +declarePublicGauge lp_mix_success, "number of lightpush messages sent via mix" + +declarePublicGauge lp_mix_failed, "number of lightpush messages failed via mix", labels = ["error"]