2024-10-10 08:40:09 -04:00
|
|
|
import std/[options, json, strutils, net]
|
2025-12-19 17:00:43 +01:00
|
|
|
import chronos, chronicles, results, confutils, confutils/std/net, ffi
|
2024-05-10 10:56:17 +02:00
|
|
|
|
2023-07-31 09:52:04 +02:00
|
|
|
import
|
2025-11-15 23:31:09 +01:00
|
|
|
waku/node/peer_manager/peer_manager,
|
|
|
|
|
tools/confutils/cli_args,
|
|
|
|
|
waku/factory/waku,
|
|
|
|
|
waku/factory/node_factory,
|
|
|
|
|
waku/factory/app_callbacks,
|
2025-12-19 17:00:43 +01:00
|
|
|
waku/rest_api/endpoint/builder,
|
|
|
|
|
library/declare_lib
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-12-13 17:38:16 +01:00
|
|
|
proc createWaku(
|
|
|
|
|
configJson: cstring, appCallbacks: AppCallbacks = nil
|
|
|
|
|
): Future[Result[Waku, string]] {.async.} =
|
2024-05-10 10:56:17 +02:00
|
|
|
var conf = defaultWakuNodeConf().valueOr:
|
|
|
|
|
return err("Failed creating node: " & error)
|
|
|
|
|
|
2024-02-20 16:00:03 -04:00
|
|
|
var errorResp: string
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-05-21 18:37:50 +02:00
|
|
|
var jsonNode: JsonNode
|
2023-12-14 07:16:39 +01:00
|
|
|
try:
|
2024-05-21 18:37:50 +02:00
|
|
|
jsonNode = parseJson($configJson)
|
|
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"exception in createWaku when calling parseJson: " & getCurrentExceptionMsg() &
|
|
|
|
|
" configJson string: " & $configJson
|
|
|
|
|
)
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2024-05-21 18:37:50 +02:00
|
|
|
for confField, confValue in fieldPairs(conf):
|
|
|
|
|
if jsonNode.contains(confField):
|
|
|
|
|
# Make sure string doesn't contain the leading or trailing " character
|
|
|
|
|
let formattedString = ($jsonNode[confField]).strip(chars = {'\"'})
|
|
|
|
|
# Override conf field with the value set in the json-string
|
|
|
|
|
try:
|
2024-05-10 10:56:17 +02:00
|
|
|
confValue = parseCmdArg(typeof(confValue), formattedString)
|
2024-05-21 18:37:50 +02:00
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"exception in createWaku when parsing configuration. exc: " &
|
|
|
|
|
getCurrentExceptionMsg() & ". string that could not be parsed: " &
|
|
|
|
|
formattedString & ". expected type: " & $typeof(confValue)
|
|
|
|
|
)
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2025-02-05 18:16:37 +02:00
|
|
|
# Don't send relay app callbacks if relay is disabled
|
|
|
|
|
if not conf.relay and not appCallbacks.isNil():
|
|
|
|
|
appCallbacks.relayHandler = nil
|
|
|
|
|
appCallbacks.topicHealthChangeHandler = nil
|
|
|
|
|
|
2025-05-08 07:05:35 +10:00
|
|
|
# TODO: Convert `confJson` directly to `WakuConf`
|
2025-06-16 18:44:21 +02:00
|
|
|
var wakuConf = conf.toWakuConf().valueOr:
|
2025-05-08 07:05:35 +10:00
|
|
|
return err("Configuration error: " & $error)
|
|
|
|
|
|
2025-06-16 18:44:21 +02:00
|
|
|
wakuConf.restServerConf = none(RestServerConf) ## don't want REST in libwaku
|
|
|
|
|
|
2025-07-10 19:49:47 +03:00
|
|
|
let wakuRes = (await Waku.new(wakuConf, appCallbacks)).valueOr:
|
2024-05-03 14:07:15 +02:00
|
|
|
error "waku initialization failed", error = error
|
|
|
|
|
return err("Failed setting up Waku: " & $error)
|
2024-03-27 10:08:53 -04:00
|
|
|
|
2024-05-03 14:07:15 +02:00
|
|
|
return ok(wakuRes)
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
registerReqFFI(CreateNodeRequest, ctx: ptr FFIContext[Waku]):
|
|
|
|
|
proc(
|
|
|
|
|
configJson: cstring, appCallbacks: AppCallbacks
|
|
|
|
|
): Future[Result[string, string]] {.async.} =
|
|
|
|
|
ctx.myLib[] = (await createWaku(configJson, cast[AppCallbacks](appCallbacks))).valueOr:
|
|
|
|
|
error "CreateNodeRequest failed", error = error
|
2025-05-14 11:05:02 +02:00
|
|
|
return err($error)
|
2023-07-31 09:52:04 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
return ok("")
|
|
|
|
|
|
|
|
|
|
proc waku_start(
|
|
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
) {.ffi.} =
|
|
|
|
|
(await startWaku(ctx[].myLib)).isOkOr:
|
|
|
|
|
error "START_NODE failed", error = error
|
|
|
|
|
return err("failed to start: " & $error)
|
|
|
|
|
return ok("")
|
|
|
|
|
|
|
|
|
|
proc waku_stop(
|
|
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
) {.ffi.} =
|
|
|
|
|
try:
|
|
|
|
|
await ctx.myLib[].stop()
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
error "STOP_NODE failed", error = exc.msg
|
|
|
|
|
return err("failed to stop: " & exc.msg)
|
2023-07-31 09:52:04 +02:00
|
|
|
return ok("")
|