Ivan FB 0590b2bf68
messaging: route api send/subscription through the Waku kernel
The messaging api layer reached around the Waku kernel into the libp2p
node (`waku.node.subscriptionManager`, `waku.node.rng`,
`waku.node.brokerCtx`). That breaks the declared layering: messaging
depends on the Waku *kernel* abstraction, not the raw node.

Add a content-topic subscription api on the Waku layer
(`waku/api/subscriptions.nim`) and switch the messaging send/subscription
paths onto it, plus the kernel's own `rng`/`brokerCtx` fields, so the
layer no longer touches `node` internals.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 13:40:40 +02:00

35 lines
1.4 KiB
Nim

## Messaging layer API — send operation.
import results, chronos, chronicles
import logos_delivery/api/types
import logos_delivery/messaging/messaging_client
import logos_delivery/waku/waku
import logos_delivery/waku/api/subscriptions
import logos_delivery/messaging/delivery_service/send_service
import logos_delivery/messaging/delivery_service/send_service/delivery_task
proc send*(
self: MessagingClient, envelope: MessageEnvelope
): Future[Result[RequestId, string]] {.async.} =
## High-level messaging API send. Auto-subscribes to the content topic
## (so the local node sees its own gossipsub broadcast), builds a
## `DeliveryTask`, and hands it to the send service. Returns the request
## id the caller can correlate with `MessageSentEvent` / `MessageErrorEvent`.
?self.checkApiAvailability()
let isSubbed = self.waku.isSubscribed(envelope.contentTopic).valueOr(false)
if not isSubbed:
info "Auto-subscribing to topic on send", contentTopic = envelope.contentTopic
self.waku.subscribe(envelope.contentTopic).isOkOr:
warn "Failed to auto-subscribe", error = error
return err("Failed to auto-subscribe before sending: " & error)
let requestId = RequestId.new(self.waku.rng)
let deliveryTask = DeliveryTask.new(requestId, envelope, self.waku.brokerCtx).valueOr:
return err("MessagingClient.send: Failed to create delivery task: " & error)
asyncSpawn self.sendService.send(deliveryTask)
return ok(requestId)