2026-06-15 09:56:15 -03:00
|
|
|
import logos_delivery/waku/compat/option_valueor
|
2026-06-10 09:09:22 -03:00
|
|
|
import std/[net, options]
|
|
|
|
|
|
|
|
|
|
import chronicles, chronos, libp2p/peerid, results
|
2025-10-01 16:31:34 +10:00
|
|
|
|
2026-06-08 13:37:53 +02:00
|
|
|
import logos_delivery/waku/factory/waku
|
|
|
|
|
import logos_delivery/messaging/messaging_client
|
2026-06-19 08:06:11 +02:00
|
|
|
import logos_delivery/channels/reliable_channel_manager
|
2026-06-19 12:07:28 +02:00
|
|
|
import
|
|
|
|
|
logos_delivery/api/messaging_client_interface
|
|
|
|
|
# brings the interface `send` method into scope: the impl's `method send` in the
|
|
|
|
|
# BrokerImplement block is not exported, so the call below dispatches through the
|
|
|
|
|
# MessagingClientInterface method instead.
|
2026-06-08 13:37:53 +02:00
|
|
|
import logos_delivery/waku/[requests/health_requests, waku_core, waku_node]
|
|
|
|
|
import logos_delivery/messaging/delivery_service/send_service
|
|
|
|
|
import logos_delivery/waku/node/subscription_manager
|
2026-02-17 10:38:35 +01:00
|
|
|
import libp2p/peerid
|
2026-06-08 13:37:53 +02:00
|
|
|
import tools/confutils/cli_args
|
2026-01-30 01:06:00 +01:00
|
|
|
import ./[api_conf, types]
|
2025-10-01 16:31:34 +10:00
|
|
|
|
2026-03-03 19:17:54 +01:00
|
|
|
export cli_args
|
|
|
|
|
|
2026-01-30 01:06:00 +01:00
|
|
|
logScope:
|
|
|
|
|
topics = "api"
|
2025-10-01 16:31:34 +10:00
|
|
|
|
2026-03-03 19:17:54 +01:00
|
|
|
proc createNode*(conf: WakuNodeConf): Future[Result[Waku, string]] {.async.} =
|
|
|
|
|
let wakuConf = conf.toWakuConf().valueOr:
|
2025-10-01 16:31:34 +10:00
|
|
|
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)
|
2026-01-30 01:06:00 +01:00
|
|
|
|
2026-06-19 08:06:11 +02:00
|
|
|
# TODO workaround for legacy use. It will be removed as soon all usage goes through LogosDelivery.
|
|
|
|
|
proc mountMessagingClient*(w: Waku): Result[void, string] =
|
|
|
|
|
## Construct and attach a `MessagingClient` to the node, wiring its brokers
|
|
|
|
|
## under the node's own `brokerCtx` so emitted events (MessageSent/Error/
|
|
|
|
|
## Propagated) reach listeners registered on that same context.
|
|
|
|
|
if w.isNil() or w.node.isNil():
|
|
|
|
|
return err("Waku node is not initialized")
|
|
|
|
|
if not w.messagingClient.isNil():
|
|
|
|
|
return ok()
|
|
|
|
|
|
2026-06-19 12:07:28 +02:00
|
|
|
w.messagingClient =
|
|
|
|
|
MessagingClient.createUnderContext(w.brokerCtx, w.conf.p2pReliability, w.node)
|
2026-06-19 08:06:11 +02:00
|
|
|
return ok()
|
|
|
|
|
|
|
|
|
|
# TODO workaround for legacy use. It will be removed as soon all usage goes through LogosDelivery.
|
|
|
|
|
proc mountReliableChannelManager*(w: Waku): Result[void, string] =
|
|
|
|
|
## Construct and attach a `ReliableChannelManager` to the node, wiring its
|
|
|
|
|
## brokers under the node's own `brokerCtx` (matching `mountMessagingClient`)
|
|
|
|
|
## so channel events reach listeners on that same context. Requires the
|
|
|
|
|
## messaging client to be mounted first.
|
|
|
|
|
if w.isNil() or w.node.isNil():
|
|
|
|
|
return err("Waku node is not initialized")
|
|
|
|
|
if w.messagingClient.isNil():
|
|
|
|
|
return err("messaging client must be mounted before reliable channel manager")
|
|
|
|
|
if not w.reliableChannelManager.isNil():
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
|
|
w.reliableChannelManager = ReliableChannelManager.createUnderContext(
|
|
|
|
|
w.brokerCtx, MessagingClientInterface(w.messagingClient)
|
|
|
|
|
)
|
|
|
|
|
return ok()
|
|
|
|
|
|
2026-01-30 01:06:00 +01:00
|
|
|
proc checkApiAvailability(w: Waku): Result[void, string] =
|
|
|
|
|
if w.isNil():
|
|
|
|
|
return err("Waku node is not initialized")
|
|
|
|
|
|
2026-02-12 14:52:39 -03:00
|
|
|
# TODO: Conciliate request-bouncing health checks here with unit testing.
|
|
|
|
|
# (For now, better to just allow all sends and rely on retries.)
|
2026-01-30 01:06:00 +01:00
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
|
|
proc subscribe*(
|
|
|
|
|
w: Waku, contentTopic: ContentTopic
|
|
|
|
|
): Future[Result[void, string]] {.async.} =
|
|
|
|
|
?checkApiAvailability(w)
|
|
|
|
|
|
2026-06-04 15:53:27 -03:00
|
|
|
return w.node.subscriptionManager.subscribe(contentTopic)
|
2026-01-30 01:06:00 +01:00
|
|
|
|
|
|
|
|
proc unsubscribe*(w: Waku, contentTopic: ContentTopic): Result[void, string] =
|
|
|
|
|
?checkApiAvailability(w)
|
|
|
|
|
|
2026-06-04 15:53:27 -03:00
|
|
|
return w.node.subscriptionManager.unsubscribe(contentTopic)
|
2026-01-30 01:06:00 +01:00
|
|
|
|
|
|
|
|
proc send*(
|
|
|
|
|
w: Waku, envelope: MessageEnvelope
|
|
|
|
|
): Future[Result[RequestId, string]] {.async.} =
|
|
|
|
|
?checkApiAvailability(w)
|
2026-06-04 15:53:27 -03:00
|
|
|
return await w.messagingClient.send(envelope)
|