2026-03-03 19:17:54 +01:00
|
|
|
import chronicles, chronos, results
|
2025-10-01 16:31:34 +10:00
|
|
|
|
|
|
|
|
import waku/factory/waku
|
2026-02-12 14:52:39 -03:00
|
|
|
import waku/[requests/health_requests, waku_core, waku_node]
|
2026-01-30 01:06:00 +01:00
|
|
|
import waku/node/delivery_service/send_service
|
2026-03-02 14:52:36 -03:00
|
|
|
import waku/node/delivery_service/subscription_manager
|
2026-02-17 10:38:35 +01:00
|
|
|
import libp2p/peerid
|
2026-03-03 19:17:54 +01: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
|
|
|
|
|
|
|
|
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-03-02 14:52:36 -03:00
|
|
|
return w.deliveryService.subscriptionManager.subscribe(contentTopic)
|
2026-01-30 01:06:00 +01:00
|
|
|
|
|
|
|
|
proc unsubscribe*(w: Waku, contentTopic: ContentTopic): Result[void, string] =
|
|
|
|
|
?checkApiAvailability(w)
|
|
|
|
|
|
2026-03-02 14:52:36 -03:00
|
|
|
return w.deliveryService.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-03-02 14:52:36 -03:00
|
|
|
let isSubbed = w.deliveryService.subscriptionManager
|
|
|
|
|
.isSubscribed(envelope.contentTopic)
|
|
|
|
|
.valueOr(false)
|
|
|
|
|
if not isSubbed:
|
|
|
|
|
info "Auto-subscribing to topic on send", contentTopic = envelope.contentTopic
|
|
|
|
|
w.deliveryService.subscriptionManager.subscribe(envelope.contentTopic).isOkOr:
|
|
|
|
|
warn "Failed to auto-subscribe", error = error
|
|
|
|
|
return err("Failed to auto-subscribe before sending: " & error)
|
|
|
|
|
|
2026-01-30 01:06:00 +01:00
|
|
|
let requestId = RequestId.new(w.rng)
|
|
|
|
|
|
|
|
|
|
let deliveryTask = DeliveryTask.new(requestId, envelope, w.brokerCtx).valueOr:
|
|
|
|
|
return err("API send: Failed to create delivery task: " & error)
|
|
|
|
|
|
|
|
|
|
info "API send: scheduling delivery task",
|
|
|
|
|
requestId = $requestId,
|
|
|
|
|
pubsubTopic = deliveryTask.pubsubTopic,
|
|
|
|
|
contentTopic = deliveryTask.msg.contentTopic,
|
|
|
|
|
msgHash = deliveryTask.msgHash.to0xHex(),
|
|
|
|
|
myPeerId = w.node.peerId()
|
|
|
|
|
|
|
|
|
|
asyncSpawn w.deliveryService.sendService.send(deliveryTask)
|
|
|
|
|
|
|
|
|
|
return ok(requestId)
|