Skip to content

Commit ce6645a

Browse files
authored
Refactor wakunode2 setup (#675)
* Refactor wakunode2 setup
1 parent c3d5701 commit ce6645a

File tree

7 files changed

+424
-225
lines changed

7 files changed

+424
-225
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This release contains the following:
1515
The filed numbers of the `HistoryResponse` are shifted up by one to match up the [13/WAKU2-STORE](https://rfc.vac.dev/spec/13/) specs.
1616
- Adds optional `timestamp` to `WakuRelayMessage`.
1717
#### General refactoring
18+
- `wakunode2` setup refactored into 6 distinct phases with improved logging and error handling
1819
#### Docs
1920
- Adds the database migration tutorial.
2021
#### Schema

waku/v2/node/jsonrpc/filter_api.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import
44
std/[tables,sequtils],
5+
chronicles,
56
json_rpc/rpcserver,
6-
eth/[common, rlp, keys, p2p],
77
../wakunode2,
88
./jsonrpc_types
99

waku/v2/node/jsonrpc/private_api.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import
44
std/[tables,sequtils],
5+
chronicles,
56
json_rpc/rpcserver,
67
nimcrypto/sysrand,
7-
eth/[common, rlp, keys, p2p],
8-
../wakunode2, ../waku_payload,
9-
./jsonrpc_types, ./jsonrpc_utils
8+
../wakunode2,
9+
../waku_payload,
10+
./jsonrpc_types,
11+
./jsonrpc_utils
1012

1113
export waku_payload, jsonrpc_types
1214

@@ -37,7 +39,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, rng: ref BrHm
3739

3840
let msg = message.toWakuMessage(version = 1,
3941
rng = rng,
40-
pubKey = none(keys.PublicKey),
42+
pubKey = none(waku_payload.PublicKey),
4143
symkey = some(symkey.toSymKey()))
4244

4345
if (await node.publish(topic, msg).withTimeout(futTimeout)):
@@ -60,7 +62,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, rng: ref BrHm
6062
# Clear cache before next call
6163
topicCache[topic] = @[]
6264
return msgs.mapIt(it.toWakuRelayMessage(symkey = some(symkey.toSymKey()),
63-
privateKey = none(keys.PrivateKey)))
65+
privateKey = none(waku_payload.PrivateKey)))
6466
else:
6567
# Not subscribed to this topic
6668
raise newException(ValueError, "Not subscribed to topic: " & topic)
@@ -71,7 +73,7 @@ proc installPrivateApiHandlers*(node: WakuNode, rpcsrv: RpcServer, rng: ref BrHm
7173
## Generates and returns a public/private key pair for asymmetric message encryption and decryption.
7274
debug "get_waku_v2_private_v1_asymmetric_keypair"
7375

74-
let privKey = keys.PrivateKey.random(rng[])
76+
let privKey = waku_payload.PrivateKey.random(rng[])
7577

7678
return WakuKeyPair(seckey: privKey, pubkey: privKey.toPublicKey())
7779

waku/v2/node/jsonrpc/relay_api.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import
44
std/[tables,sequtils],
5+
chronicles,
56
json_rpc/rpcserver,
67
libp2p/protocols/pubsub/pubsub,
7-
eth/[common, rlp, keys, p2p],
88
../wakunode2,
9-
./jsonrpc_types, ./jsonrpc_utils
9+
./jsonrpc_types,
10+
./jsonrpc_utils
1011

1112
export jsonrpc_types
1213

waku/v2/node/waku_setup.nim

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{.push raises: [Defect].}
2+
3+
## Collection of utilities commonly used
4+
## during the setup phase of a Waku v2 node
5+
6+
import
7+
std/tables,
8+
chronos,
9+
chronicles,
10+
json_rpc/rpcserver,
11+
metrics,
12+
metrics/chronos_httpserver,
13+
stew/results,
14+
stew/shims/net,
15+
./storage/sqlite,
16+
./storage/migration/migration_types,
17+
./jsonrpc/[admin_api,
18+
debug_api,
19+
filter_api,
20+
relay_api,
21+
store_api,
22+
private_api,
23+
debug_api],
24+
./config,
25+
./wakunode2
26+
27+
logScope:
28+
topics = "wakunode.setup"
29+
30+
type
31+
SetupResult*[T] = Result[T, string]
32+
33+
##########################
34+
# Setup helper functions #
35+
##########################
36+
37+
proc startRpc*(node: WakuNode, rpcIp: ValidIpAddress, rpcPort: Port, conf: WakuNodeConf)
38+
{.raises: [Defect, RpcBindError, CatchableError].} =
39+
# @TODO: API handlers still raise CatchableError
40+
41+
let
42+
ta = initTAddress(rpcIp, rpcPort)
43+
rpcServer = newRpcHttpServer([ta])
44+
installDebugApiHandlers(node, rpcServer)
45+
46+
# Install enabled API handlers:
47+
if conf.relay:
48+
let topicCache = newTable[string, seq[WakuMessage]]()
49+
installRelayApiHandlers(node, rpcServer, topicCache)
50+
if conf.rpcPrivate:
51+
# Private API access allows WakuRelay functionality that
52+
# is backwards compatible with Waku v1.
53+
installPrivateApiHandlers(node, rpcServer, node.rng, topicCache)
54+
55+
if conf.filter:
56+
let messageCache = newTable[ContentTopic, seq[WakuMessage]]()
57+
installFilterApiHandlers(node, rpcServer, messageCache)
58+
59+
if conf.store:
60+
installStoreApiHandlers(node, rpcServer)
61+
62+
if conf.rpcAdmin:
63+
installAdminApiHandlers(node, rpcServer)
64+
65+
rpcServer.start()
66+
info "RPC Server started", ta
67+
68+
proc startMetricsServer*(serverIp: ValidIpAddress, serverPort: Port) =
69+
info "Starting metrics HTTP server", serverIp, serverPort
70+
71+
try:
72+
startMetricsHttpServer($serverIp, serverPort)
73+
except Exception as e:
74+
raiseAssert("Exception while starting metrics HTTP server: " & e.msg)
75+
76+
info "Metrics HTTP server started", serverIp, serverPort
77+
78+
proc startMetricsLog*() =
79+
# https://github.com/nim-lang/Nim/issues/17369
80+
var logMetrics: proc(udata: pointer) {.gcsafe, raises: [Defect].}
81+
logMetrics = proc(udata: pointer) =
82+
{.gcsafe.}:
83+
# TODO: libp2p_pubsub_peers is not public, so we need to make this either
84+
# public in libp2p or do our own peer counting after all.
85+
var
86+
totalMessages = 0.float64
87+
88+
for key in waku_node_messages.metrics.keys():
89+
try:
90+
totalMessages = totalMessages + waku_node_messages.value(key)
91+
except KeyError:
92+
discard
93+
94+
info "Node metrics", totalMessages
95+
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
96+
discard setTimer(Moment.fromNow(2.seconds), logMetrics)
97+
98+
proc runMigrations*(sqliteDatabase: SqliteDatabase, conf: WakuNodeConf) =
99+
# Run migration scripts on persistent storage
100+
101+
var migrationPath: string
102+
if conf.persistPeers and conf.persistMessages:
103+
migrationPath = migration_types.ALL_STORE_MIGRATION_PATH
104+
elif conf.persistPeers:
105+
migrationPath = migration_types.PEER_STORE_MIGRATION_PATH
106+
elif conf.persistMessages:
107+
migrationPath = migration_types.MESSAGE_STORE_MIGRATION_PATH
108+
109+
# run migration
110+
info "running migration ... "
111+
let migrationResult = sqliteDatabase.migrate(migrationPath)
112+
if migrationResult.isErr:
113+
warn "migration failed"
114+
else:
115+
info "migration is done"

0 commit comments

Comments
 (0)