From a307bf225d90db76a73096ffd88b01852ca9c5b4 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Fri, 26 Sep 2025 17:04:32 +0200 Subject: [PATCH] create waku_example --- .gitignore | 83 +++++++++++++++++++++++++++++++++++++++ Makefile | 15 +++++-- examples/waku_example.nim | 66 +++++++++++++++++++++++++++++++ nim_chat_poc.nimble | 27 ++++++------- 4 files changed, 174 insertions(+), 17 deletions(-) create mode 100644 examples/waku_example.nim diff --git a/.gitignore b/.gitignore index 9233f0e..49dea5b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,86 @@ *.dSYM nimble.develop nimble.paths + + +/nimcache + +# Executables shall be put in an ignored build/ directory +/build + +# Nimble packages +/vendor/.nimble + +# Generated Files +*.generated.nim + +# ntags/ctags output +/tags + +# a symlink that can't be added to the repo because of Windows +/nim_chat_poc.nims + +# Ignore dynamic, static libs and libtool archive files +*.so +*.dylib +*.a +*.la +*.exe +*.dll + +.DS_Store + +# Ignore simulation generated metrics files +/metrics/prometheus +/metrics/waku-sim-all-nodes-grafana-dashboard.json + +*.log +/package-lock.json +/package.json +node_modules/ +/.update.timestamp + +# Ignore Jetbrains IDE files +.idea/ + +# ignore vscode files +.vscode/ + +# RLN / keystore +rlnKeystore.json +*.tar.gz + +# Nimbus Build System +nimbus-build-system.paths + +# sqlite db +*.db +*.db-shm +*.db-wal +*.sqlite3 +*.sqlite3-shm +*.sqlite3-wal + +/examples/nodejs/build/ +/examples/rust/target/ + + +# Coverage +coverage_html_report/ +*.info + +# Wildcard +*.ignore.* + +# Ignore all possible node runner directories +**/keystore/ +**/rln_tree/ +**/certs/ + +# simple qt example +.qmake.stash +main-qt +waku_handler.moc.cpp + +# Nix build result +result diff --git a/Makefile b/Makefile index 3575899..0a2539f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ export BUILD_SYSTEM_DIR := vendor/nimbus-build-system -export EXCLUDED_NIM_PACKAGES := vendor/nim-dnsdisc/vendor +export EXCLUDED_NIM_PACKAGES := vendor/nwaku/vendor/nim-dnsdisc/vendor \ + vendor/nwaku/vendor/nimbus-build-system LINK_PCRE := 0 FORMAT_MSG := "\\x1B[95mFormatting:\\x1B[39m" # we don't want an error here, so we can handle things later, in the ".DEFAULT" target @@ -60,14 +61,22 @@ NIM_PARAMS := $(NIM_PARAMS) -d:git_version=\"$(GIT_VERSION)\" .PHONY: build-waku-librln build-waku-librln: - make -C vendor/nwaku librln + @echo "Start building waku librln" + $(MAKE) -C vendor/nwaku librln + $(eval NIM_PARAMS += --passL:./vendor/nwaku/librln_v0.7.0.a --passL:-lm) + @echo "Completed building librln" + +build-waku-nat: + @echo "Start building waku nat-libs" + $(MAKE) -C vendor/nwaku nat-libs + @echo "Completed building nat-libs" ########## ## Example ## ########## .PHONY: waku_example -waku_example: | build-waku-librln nim_chat_poc.nims +waku_example: | build-waku-librln build-waku-nat nim_chat_poc.nims echo -e $(BUILD_MSG) "build/$@" && \ \ $(ENV_SCRIPT) nim waku_example $(NIM_PARAMS) nim_chat_poc.nims diff --git a/examples/waku_example.nim b/examples/waku_example.nim new file mode 100644 index 0000000..4f79546 --- /dev/null +++ b/examples/waku_example.nim @@ -0,0 +1,66 @@ +import + std/[options, strutils, sequtils, net], + chronicles, + chronos, + metrics, + system/ansi_c, + libp2p/crypto/crypto +import waku as waku_module + +logScope: + topics = "waku example main" + +const git_version* {.strdefine.} = "n/a" + +proc mm() {.async.} = + await sleepAsync(1000) + echo "Hello, world!" + +when isMainModule: + const versionString = "version / git commit hash: " & waku.git_version + + var wakuNodeConf = WakuNodeConf.load(version = versionString).valueOr: + error "failure while loading the configuration", error = error + quit(QuitFailure) + + ## Also called within Waku.new. The call to startRestServerEssentials needs the following line + logging.setupLog(wakuNodeConf.logLevel, wakuNodeConf.logFormat) + + let conf = wakuNodeConf.toWakuConf().valueOr: + error "Waku configuration failed", error = error + quit(QuitFailure) + + var waku = (waitFor Waku.new(conf)).valueOr: + error "Waku initialization failed", error = error + quit(QuitFailure) + + (waitFor startWaku(addr waku)).isOkOr: + error "Starting waku failed", error = error + quit(QuitFailure) + + debug "Setting up shutdown hooks" + proc asyncStopper(waku: Waku) {.async: (raises: [Exception]).} = + await waku.stop() + quit(QuitSuccess) + + # Handle Ctrl-C SIGINT + proc handleCtrlC() {.noconv.} = + when defined(windows): + # workaround for https://github.com/nim-lang/Nim/issues/4057 + setupForeignThreadGc() + notice "Shutting down after receiving SIGINT" + asyncSpawn asyncStopper(waku) + + setControlCHook(handleCtrlC) + + # Handle SIGTERM + when defined(posix): + proc handleSigterm(signal: cint) {.noconv.} = + notice "Shutting down after receiving SIGTERM" + asyncSpawn asyncStopper(waku) + + c_signal(ansi_c.SIGTERM, handleSigterm) + + info "Node setup complete" + + runForever() diff --git a/nim_chat_poc.nimble b/nim_chat_poc.nimble index 5ee9771..8c93d91 100644 --- a/nim_chat_poc.nimble +++ b/nim_chat_poc.nimble @@ -9,20 +9,19 @@ bin = @["nim_chat_poc"] # Dependencies -requires "nim >= 2.2.4" - -requires "protobuf_serialization" -requires "secp256k1" -requires "blake2" -requires "chronicles" -requires "libp2p" -requires "nimchacha20poly1305" # TODO: remove -requires "confutils" -requires "eth" -requires "regex" -requires "web3" -requires "https://github.com/jazzz/nim-sds#exports" -requires "naawaku" +requires "nim >= 2.2.4", + "protobuf_serialization", + "secp256k1", + "blake2", + "chronicles", + "libp2p", + "nimchacha20poly1305", # TODO: remove + "confutils", + "eth", + "regex", + "web3", + "https://github.com/jazzz/nim-sds#exports", + "waku" proc buildBinary(name: string, srcDir = "./", params = "", lang = "c") = if not dirExists "build":