Skip to content

Commit d138328

Browse files
create waku_example
1 parent 1be749f commit d138328

File tree

4 files changed

+174
-17
lines changed

4 files changed

+174
-17
lines changed

.gitignore

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,86 @@
44
*.dSYM
55
nimble.develop
66
nimble.paths
7+
8+
9+
/nimcache
10+
11+
# Executables shall be put in an ignored build/ directory
12+
/build
13+
14+
# Nimble packages
15+
/vendor/.nimble
16+
17+
# Generated Files
18+
*.generated.nim
19+
20+
# ntags/ctags output
21+
/tags
22+
23+
# a symlink that can't be added to the repo because of Windows
24+
/nim_chat_poc.nims
25+
26+
# Ignore dynamic, static libs and libtool archive files
27+
*.so
28+
*.dylib
29+
*.a
30+
*.la
31+
*.exe
32+
*.dll
33+
34+
.DS_Store
35+
36+
# Ignore simulation generated metrics files
37+
/metrics/prometheus
38+
/metrics/waku-sim-all-nodes-grafana-dashboard.json
39+
40+
*.log
41+
/package-lock.json
42+
/package.json
43+
node_modules/
44+
/.update.timestamp
45+
46+
# Ignore Jetbrains IDE files
47+
.idea/
48+
49+
# ignore vscode files
50+
.vscode/
51+
52+
# RLN / keystore
53+
rlnKeystore.json
54+
*.tar.gz
55+
56+
# Nimbus Build System
57+
nimbus-build-system.paths
58+
59+
# sqlite db
60+
*.db
61+
*.db-shm
62+
*.db-wal
63+
*.sqlite3
64+
*.sqlite3-shm
65+
*.sqlite3-wal
66+
67+
/examples/nodejs/build/
68+
/examples/rust/target/
69+
70+
71+
# Coverage
72+
coverage_html_report/
73+
*.info
74+
75+
# Wildcard
76+
*.ignore.*
77+
78+
# Ignore all possible node runner directories
79+
**/keystore/
80+
**/rln_tree/
81+
**/certs/
82+
83+
# simple qt example
84+
.qmake.stash
85+
main-qt
86+
waku_handler.moc.cpp
87+
88+
# Nix build result
89+
result

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export BUILD_SYSTEM_DIR := vendor/nimbus-build-system
2-
export EXCLUDED_NIM_PACKAGES := vendor/nim-dnsdisc/vendor
2+
export EXCLUDED_NIM_PACKAGES := vendor/nwaku/vendor/nim-dnsdisc/vendor \
3+
vendor/nwaku/vendor/nimbus-build-system
34
LINK_PCRE := 0
45
FORMAT_MSG := "\\x1B[95mFormatting:\\x1B[39m"
56
# 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)\"
6061
.PHONY: build-waku-librln
6162

6263
build-waku-librln:
63-
make -C vendor/nwaku librln
64+
@echo "Start building waku librln"
65+
$(MAKE) -C vendor/nwaku librln
66+
$(eval NIM_PARAMS += --passL:./vendor/nwaku/librln_v0.7.0.a --passL:-lm)
67+
@echo "Completed building librln"
68+
69+
build-waku-nat:
70+
@echo "Start building waku nat-libs"
71+
$(MAKE) -C vendor/nwaku nat-libs
72+
@echo "Completed building nat-libs"
6473

6574
##########
6675
## Example ##
6776
##########
6877
.PHONY: waku_example
6978

70-
waku_example: | build-waku-librln nim_chat_poc.nims
79+
waku_example: | build-waku-librln build-waku-nat nim_chat_poc.nims
7180
echo -e $(BUILD_MSG) "build/$@" && \
7281
\
7382
$(ENV_SCRIPT) nim waku_example $(NIM_PARAMS) nim_chat_poc.nims

examples/waku_example.nim

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import
2+
std/[options, strutils, sequtils, net],
3+
chronicles,
4+
chronos,
5+
metrics,
6+
system/ansi_c,
7+
libp2p/crypto/crypto
8+
import waku as waku_module
9+
10+
logScope:
11+
topics = "waku example main"
12+
13+
const git_version* {.strdefine.} = "n/a"
14+
15+
proc mm() {.async.} =
16+
await sleepAsync(1000)
17+
echo "Hello, world!"
18+
19+
when isMainModule:
20+
const versionString = "version / git commit hash: " & waku.git_version
21+
22+
var wakuNodeConf = WakuNodeConf.load(version = versionString).valueOr:
23+
error "failure while loading the configuration", error = error
24+
quit(QuitFailure)
25+
26+
## Also called within Waku.new. The call to startRestServerEssentials needs the following line
27+
logging.setupLog(wakuNodeConf.logLevel, wakuNodeConf.logFormat)
28+
29+
let conf = wakuNodeConf.toWakuConf().valueOr:
30+
error "Waku configuration failed", error = error
31+
quit(QuitFailure)
32+
33+
var waku = (waitFor Waku.new(conf)).valueOr:
34+
error "Waku initialization failed", error = error
35+
quit(QuitFailure)
36+
37+
(waitFor startWaku(addr waku)).isOkOr:
38+
error "Starting waku failed", error = error
39+
quit(QuitFailure)
40+
41+
debug "Setting up shutdown hooks"
42+
proc asyncStopper(waku: Waku) {.async: (raises: [Exception]).} =
43+
await waku.stop()
44+
quit(QuitSuccess)
45+
46+
# Handle Ctrl-C SIGINT
47+
proc handleCtrlC() {.noconv.} =
48+
when defined(windows):
49+
# workaround for https://github.com/nim-lang/Nim/issues/4057
50+
setupForeignThreadGc()
51+
notice "Shutting down after receiving SIGINT"
52+
asyncSpawn asyncStopper(waku)
53+
54+
setControlCHook(handleCtrlC)
55+
56+
# Handle SIGTERM
57+
when defined(posix):
58+
proc handleSigterm(signal: cint) {.noconv.} =
59+
notice "Shutting down after receiving SIGTERM"
60+
asyncSpawn asyncStopper(waku)
61+
62+
c_signal(ansi_c.SIGTERM, handleSigterm)
63+
64+
info "Node setup complete"
65+
66+
runForever()

nim_chat_poc.nimble

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ bin = @["nim_chat_poc"]
99

1010
# Dependencies
1111

12-
requires "nim >= 2.2.4"
13-
14-
requires "protobuf_serialization"
15-
requires "secp256k1"
16-
requires "blake2"
17-
requires "chronicles"
18-
requires "libp2p"
19-
requires "nimchacha20poly1305" # TODO: remove
20-
requires "confutils"
21-
requires "eth"
22-
requires "regex"
23-
requires "web3"
24-
requires "https://github.com/jazzz/nim-sds#exports"
25-
requires "naawaku"
12+
requires "nim >= 2.2.4",
13+
"protobuf_serialization",
14+
"secp256k1",
15+
"blake2",
16+
"chronicles",
17+
"libp2p",
18+
"nimchacha20poly1305", # TODO: remove
19+
"confutils",
20+
"eth",
21+
"regex",
22+
"web3",
23+
"https://github.com/jazzz/nim-sds#exports",
24+
"waku"
2625

2726
proc buildBinary(name: string, srcDir = "./", params = "", lang = "c") =
2827
if not dirExists "build":

0 commit comments

Comments
 (0)