From c7e6bfe7ff7d525ed526d3d299dccc36296a9559 Mon Sep 17 00:00:00 2001 From: kaichaosun Date: Wed, 10 Sep 2025 12:04:10 +0800 Subject: [PATCH] chore: load rln library --- .gitignore | 1 + chat_sdk.nimble | 14 ++++++-- scripts/build_rln.sh | 79 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100755 scripts/build_rln.sh diff --git a/.gitignore b/.gitignore index 9a9c03f..02d3718 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ nimcache/ # Build artifacts *.o *.a +build/ # OS .DS_Store diff --git a/chat_sdk.nimble b/chat_sdk.nimble index 67c1204..9345671 100644 --- a/chat_sdk.nimble +++ b/chat_sdk.nimble @@ -7,7 +7,16 @@ license = "MIT" srcDir = "src" ### Dependencies -requires "nim >= 2.2.4", "chronicles", "chronos", "db_connector", "flatty", "waku#nimble-fix-next" +requires "nim >= 2.2.4", "chronicles", "chronos", "db_connector", "flatty", "waku#799793dba7ef78eba41582ed55db01a2d5d7500a" + +proc ensureRln(libFile: string = "build/librln.a", version = "v0.7.0") = + if not fileExists(libFile): + echo "Building RLN library..." + let buildDir = getCurrentDir() + let outFile = libFile + exec "bash ./scripts/build_rln.sh " & buildDir & " " & version & " " & outFile + else: + echo "RLN library already exists: " & libFile task buildSharedLib, "Build shared library for C bindings": exec "nim c --mm:refc --app:lib --out:../library/c-bindings/libchatsdk.so chat_sdk/chat_sdk.nim" @@ -22,7 +31,8 @@ task segment, "Run segmentation": exec "nim c -r chat_sdk/segmentation.nim" task subscribe, "Run waku subscription service": - exec "nim c -r apps/waku_subscriber.nim" + ensureRln() + exec "nim c --passL:build/librln.a --passL:-lm -r apps/waku_subscriber.nim" task publish, "Run waku publishing service": exec "nim c -r apps/waku_publisher.nim" diff --git a/scripts/build_rln.sh b/scripts/build_rln.sh new file mode 100755 index 0000000..a4c825e --- /dev/null +++ b/scripts/build_rln.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +# This script is used to build the rln library for the current platform, or download it from the +# release page if it is available. + +set -e + +# --- lock setup --- +lockdir="build/rln.lock" +mkdir -p build + +# try to acquire lock (atomic) +while ! mkdir "${lockdir}" 2>/dev/null; do + echo "Another process is building RLN, waiting..." + sleep 1 +done + +# cleanup on exit +cleanup() { rm -rf "${lockdir}"; } +trap cleanup EXIT + +# first argument is the build directory +build_dir=$1 +rln_version=$2 +output_filename=$3 + +[[ -z "${build_dir}" ]] && { echo "No build directory specified"; exit 1; } +[[ -z "${rln_version}" ]] && { echo "No rln version specified"; exit 1; } +[[ -z "${output_filename}" ]] && { echo "No output filename specified"; exit 1; } + +if [[ -f "${output_filename}" ]]; then + echo "RLN library already exists: ${output_filename}, skipping build." + exit 0 +fi + +# Get the host triplet +host_triplet=$(rustc --version --verbose | awk '/host:/{print $2}') + +tarball="${host_triplet}" + +# use arkzkey feature for v0.7.0 +# TODO: update this script in the future when arkzkey is default +if [[ "${rln_version}" == "v0.7.0" ]]; then + tarball+="-arkzkey-rln.tar.gz" +else + tarball+="-rln.tar.gz" +fi + +# Download the prebuilt rln library if it is available +if curl --silent --fail-with-body -L \ + "https://github.com/vacp2p/zerokit/releases/download/$rln_version/$tarball" \ + -o "${tarball}"; +then + echo "Downloaded ${tarball}" + tar -xzf "${tarball}" + mv "release/librln.a" "${output_filename}" + rm -rf "${tarball}" release +else + echo "Failed to download ${tarball}" + # Build rln instead + # first, check if submodule version = version in Makefile + cargo metadata --format-version=1 --no-deps --manifest-path "${build_dir}/rln/Cargo.toml" + + detected_OS=$(uname -s) + if [[ "$detected_OS" == MINGW* || "$detected_OS" == MSYS* ]]; then + submodule_version=$(cargo metadata --format-version=1 --no-deps --manifest-path "${build_dir}/rln/Cargo.toml" | sed -n 's/.*"name":"rln","version":"\([^"]*\)".*/\1/p') + else + submodule_version=$(cargo metadata --format-version=1 --no-deps --manifest-path "${build_dir}/rln/Cargo.toml" | jq -r '.packages[] | select(.name == "rln") | .version') + fi + + if [[ "v${submodule_version}" != "${rln_version}" ]]; then + echo "Submodule version (v${submodule_version}) does not match version in Makefile (${rln_version})" + echo "Please update the submodule to ${rln_version}" + exit 1 + fi + # if submodule version = version in Makefile, build rln + cargo build --release -p rln --manifest-path "${build_dir}/rln/Cargo.toml" --features arkzkey + cp "${build_dir}/target/release/librln.a" "${output_filename}" +fi