mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-05-12 05:19:33 +00:00
* Add createNode(preset, mode, overrides, additions) nim api * Set p2pTcp/discv5Udp/websocket ports to 0 (auto-bind) in new createNode() * Soft-deprecate --cluster-id=N triggering the associated preset selection * Rewrite applyNetworkConf to apply user-set fields over preset fields * Generate WakuNodeConfOverlay (all Option fields) from WakuNodeConf * New parser for configJson handles new messaging shape and full conf shape * Change all confbuilder defaults from literal values to DefaultXXX consts * Change int/bool WakuNodeConf fields to Option to get user intent w/o sentinels * Make Option CLI default-value help mention defaults now owned by confbuilder * Misc refactors, fixes * Add tests
103 lines
3.4 KiB
Nim
103 lines
3.4 KiB
Nim
import std/[net, options]
|
|
|
|
import chronicles, chronos, libp2p/peerid, results
|
|
|
|
import waku/factory/waku
|
|
import waku/[requests/health_requests, waku_core, waku_node]
|
|
import waku/node/delivery_service/send_service
|
|
import waku/node/delivery_service/subscription_manager
|
|
import ../../tools/confutils/cli_args
|
|
import ../../tools/confutils/messaging_conf
|
|
import ./[api_conf, types]
|
|
|
|
export cli_args, messaging_conf
|
|
|
|
logScope:
|
|
topics = "api"
|
|
|
|
proc createNode*(conf: WakuNodeConf): Future[Result[Waku, string]] {.async.} =
|
|
let wakuConf = conf.toWakuConf().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)
|
|
|
|
proc seedDeveloperProfile(conf: var WakuNodeConf) =
|
|
# TODO: Remember to add QUIC port here as well when that is added.
|
|
var devPorts = WakuNodeConfOverlay.init()
|
|
devPorts.tcpPort = some(Port(0))
|
|
devPorts.discv5UdpPort = some(Port(0))
|
|
devPorts.websocketPort = some(Port(0))
|
|
applyAsOverride(conf, devPorts)
|
|
|
|
proc createNode*(
|
|
preset = "",
|
|
mode = cli_args.WakuMode.Core,
|
|
overrides = WakuNodeConfOverlay.init(),
|
|
additions = WakuNodeConfOverlay.init(),
|
|
): Future[Result[Waku, string]] {.async.} =
|
|
## Create a Waku node from messaging-API parameters.
|
|
var conf = defaultWakuNodeConf().valueOr:
|
|
return err("Failed creating default conf: " & error)
|
|
conf.mode = mode
|
|
conf.preset = preset
|
|
seedDeveloperProfile(conf)
|
|
applyAsOverride(conf, overrides)
|
|
applyAsAddition(conf, additions)
|
|
return await createNode(conf)
|
|
|
|
proc checkApiAvailability(w: Waku): Result[void, string] =
|
|
if w.isNil():
|
|
return err("Waku node is not initialized")
|
|
|
|
# TODO: Conciliate request-bouncing health checks here with unit testing.
|
|
# (For now, better to just allow all sends and rely on retries.)
|
|
|
|
return ok()
|
|
|
|
proc subscribe*(
|
|
w: Waku, contentTopic: ContentTopic
|
|
): Future[Result[void, string]] {.async.} =
|
|
?checkApiAvailability(w)
|
|
|
|
return w.deliveryService.subscriptionManager.subscribe(contentTopic)
|
|
|
|
proc unsubscribe*(w: Waku, contentTopic: ContentTopic): Result[void, string] =
|
|
?checkApiAvailability(w)
|
|
|
|
return w.deliveryService.subscriptionManager.unsubscribe(contentTopic)
|
|
|
|
proc send*(
|
|
w: Waku, envelope: MessageEnvelope
|
|
): Future[Result[RequestId, string]] {.async.} =
|
|
?checkApiAvailability(w)
|
|
|
|
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)
|
|
|
|
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)
|