diff --git a/library/libwaku.nim b/library/libwaku.nim index ad3afa134..4bcddb598 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -6,7 +6,7 @@ when defined(linux): {.passl: "-Wl,-soname,libwaku.so".} import std/[json, atomics, strformat, options, atomics] -import chronicles, chronos, chronos/threadsync +import chronicles, chronos, chronos/threadsync, results import waku/common/base64, waku/waku_core/message/message, @@ -15,6 +15,8 @@ import waku/waku_core/topics/pubsub_topic, waku/waku_core/subscription/push_handler, waku/waku_relay, + waku/factory/waku, + ./libwaku_conf, ./events/json_message_event, ./waku_context, ./waku_thread_requests/requests/node_lifecycle_request, @@ -98,7 +100,24 @@ proc initializeLibrary() {.exported.} = ################################################################################ ################################################################################ -### Exported procs +### Nim native exported procs (Waku API) + +proc createNode*(config: LibWakuConf): Future[Result[Waku, string]] {.async.} = + let wakuConf = toWakuConf(config).valueOr: + return err("Failed to handle the configuration: " & error) + + ## We are not defining app callbacks at node creation + let wakuRes = (await Waku.new(wakuConf)).valueOr: + error "waku initialization failed", error = error + return err("Failed setting up Waku: " & $error) + + return ok(wakuRes) + +### End of Nim native exported procs (Waku API) +################################################################################ + +################################################################################ +### FFI Exported procs proc waku_new( configJson: cstring, callback: WakuCallback, userData: pointer @@ -849,5 +868,5 @@ proc waku_is_online( userData, ) -### End of exported procs +### End of FFI exported procs ################################################################################ diff --git a/tests/library/test_all.nim b/tests/library/test_all.nim index 561c2eb12..30797e8e0 100644 --- a/tests/library/test_all.nim +++ b/tests/library/test_all.nim @@ -1,3 +1,3 @@ {.used.} -import ./test_libwaku_conf +import ./test_libwaku_conf, ./test_libwaku diff --git a/tests/library/test_libwaku.nim b/tests/library/test_libwaku.nim new file mode 100644 index 000000000..2d2f2fe4a --- /dev/null +++ b/tests/library/test_libwaku.nim @@ -0,0 +1,144 @@ +{.used.} + +import std/options, results, chronos, results, testutils/unittests +import library/libwaku, library/libwaku_conf, waku/factory/waku + +suite "LibWaku - createNode": + asyncTest "Create node with minimal Relay configuration": + ## Given + let libConf = LibWakuConf( + mode: Relay, + networkConf: libwaku_conf.NetworkConf( + bootstrapNodes: @[], + staticStoreNodes: @[], + clusterId: 1, + shardingMode: StaticSharding, + autoShardingConf: none(AutoShardingConf), + messageValidation: none(MessageValidation), + ), + storeConfirmation: false, + ) + + ## When + let nodeRes = await createNode(libConf) + + ## Then + check nodeRes.isOk() + let node = nodeRes.get() + check: + not node.isNil() + node.conf.clusterId == 1 + node.conf.relay == true + + asyncTest "Create node with auto-sharding configuration": + ## Given + let libConf = LibWakuConf( + mode: Relay, + networkConf: libwaku_conf.NetworkConf( + bootstrapNodes: @[], + staticStoreNodes: @[], + clusterId: 42, + shardingMode: AutoSharding, + autoShardingConf: some(AutoShardingConf(numShardsInCluster: 8)), + messageValidation: none(MessageValidation), + ), + storeConfirmation: false, + ) + + ## When + let node = await createNode(libConf).valueOr: + raiseAssert error + + ## Then + check nodeRes.isOk() + let node = nodeRes.get() + check: + not node.isNil() + node.conf.clusterId == 42 + node.conf.shardingConf.numShardsInCluster == 8 + + asyncTest "Create node with message validation and RLN": + ## Given + let libConf = LibWakuConf( + mode: Relay, + networkConf: libwaku_conf.NetworkConf( + bootstrapNodes: @[], + staticStoreNodes: @[], + clusterId: 1, + shardingMode: StaticSharding, + autoShardingConf: none(AutoShardingConf), + messageValidation: some( + MessageValidation( + maxMessageSizeBytes: 200'u64 * 1024'u64, # 200KB + rlnConfig: some( + RlnConfig( + contractAddress: "0x1234567890123456789012345678901234567890", + chainId: 1'u, + epochSizeSec: 600'u64, + ) + ), + ) + ), + ), + storeConfirmation: false, + ) + + ## When + let nodeRes = await createNode(libConf) + + ## Then + check nodeRes.isOk() + let node = nodeRes.get() + check: + not node.isNil() + node.conf.maxMessageSizeBytes == 200'u64 * 1024'u64 + node.conf.rlnRelayConf.isSome() + + if node.conf.rlnRelayConf.isSome(): + let rlnConf = node.conf.rlnRelayConf.get() + check: + rlnConf.dynamic == true + rlnConf.ethContractAddress == "0x1234567890123456789012345678901234567890" + rlnConf.epochSizeSec == 600'u64 + + asyncTest "Create node with full configuration": + ## Given + let libConf = LibWakuConf( + mode: Relay, + networkConf: libwaku_conf.NetworkConf( + bootstrapNodes: + @[ + "enr:-QESuEC1p_s3xJzAC_XlOuuNrhVUETmfhbm1wxRGis0f7DlqGSw2FM-p2Vn7gmfkTTnAe8Ys2cgGBN8ufJnvzKQFZqFMBgmlkgnY0iXNlY3AyNTZrMaEDS8-D878DrdbNwcuY-3p1qdDp5MOoCurhdsNPJTXZ3c5g3RjcIJ2X4N1ZHCCd2g" + ], + staticStoreNodes: + @[ + "/ip4/127.0.0.1/tcp/60000/p2p/16Uuu2HBmAcHvhLqQKwSSbX6BG5JLWUDRcaLVrehUVqpw7fz1hbYc" + ], + clusterId: 99, + shardingMode: AutoSharding, + autoShardingConf: some(AutoShardingConf(numShardsInCluster: 16)), + messageValidation: some( + MessageValidation( + maxMessageSizeBytes: 1024'u64 * 1024'u64, # 1MB + rlnConfig: none(RlnConfig), + ) + ), + ), + storeConfirmation: true, + ) + + ## When + let nodeRes = await createNode(libConf) + + ## Then + check nodeRes.isOk() + let node = nodeRes.get() + check: + not node.isNil() + node.conf.clusterId == 99 + node.conf.shardingConf.numShardsInCluster == 16 + node.conf.maxMessageSizeBytes == 1024'u64 * 1024'u64 + node.conf.staticNodes.len == 1 + node.conf.relay == true + node.conf.lightPush == true + node.conf.peerExchangeService == true