Files
logos-messaging-nim/examples/api_example/api_example.nim
T
c59b91bce5 feat(conf): default --entry-layer to kernel (#4076)
The node binary is most often run as a transport-only service/fleet node, so
`logosdeliverynode` no longer mounts the messaging client and reliable channel
manager unless asked. Protocol flags are unchanged in the default case: the
individual CLI defaults already match what `applyMode(Core)` was setting.

Only the CLI default moves. `LogosDelivery.new(entryLayer = ...)` and
`parseLogosDeliveryConf` keep defaulting to `channels`, so the Nim and C library
entry points are unaffected. `MessagingClientConf.toWakuNodeConf` pins
`entryLayer = channels` for the same reason it already pins `mode`: a conf
derived from a messaging config is by construction not kernel-only.

`LogosDeliveryConf.init` now honours `entryLayer == kernel` instead of always
mounting the messaging layer, so the layer it records and the layers it mounts
agree. Doc comments follow the code: `mode`/`preset` shape the kernel conf for
every layer, `kernel` included, and `init(kernelConf)` stays the raw entry.

Co-authored-by: NagyZoltanPeter <113987313+NagyZoltanPeter@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-25 21:32:36 +01:00

95 lines
3.0 KiB
Nim

import chronos, results, confutils, confutils/defs
import logos_delivery
type CliArgs = object
ethRpcEndpoint* {.
defaultValue: "", desc: "ETH RPC Endpoint, if passed, RLN is enabled"
.}: string
proc periodicSender(logos: LogosDelivery): Future[void] {.async.} =
let sentListener = MessageSentEvent.listen(
proc(event: MessageSentEvent) {.async: (raises: []).} =
echo "Message sent with request ID: ",
event.requestId, " hash: ", event.messageHash
).valueOr:
echo "Failed to listen to message sent event: ", error
return
let errorListener = MessageErrorEvent.listen(
proc(event: MessageErrorEvent) {.async: (raises: []).} =
echo "Message failed to send with request ID: ",
event.requestId, " error: ", event.error
).valueOr:
echo "Failed to listen to message error event: ", error
return
let propagatedListener = MessagePropagatedEvent.listen(
proc(event: MessagePropagatedEvent) {.async: (raises: []).} =
echo "Message propagated with request ID: ",
event.requestId, " hash: ", event.messageHash
).valueOr:
echo "Failed to listen to message propagated event: ", error
return
defer:
await MessageSentEvent.dropListener(sentListener)
await MessageErrorEvent.dropListener(errorListener)
await MessagePropagatedEvent.dropListener(propagatedListener)
## Periodically sends a Waku message every 30 seconds
var counter = 0
while true:
let envelope = MessageEnvelope.init(
contentTopic = "example/content/topic",
payload = "Hello Waku! Message number: " & $counter,
)
let sendRequestId = (await logos.messagingClient.send(envelope)).valueOr:
echo "Failed to send message: ", error
quit(QuitFailure)
echo "Sending message with request ID: ", sendRequestId, " counter: ", counter
counter += 1
await sleepAsync(30.seconds)
when isMainModule:
let args = CliArgs.load()
echo "Starting Waku node..."
# Use WakuNodeConf (the CLI configuration type) for node setup
var conf = defaultWakuNodeConf().valueOr:
echo "Failed to create default config: ", error
quit(QuitFailure)
# The CLI default is kernel-only; this example drives the messaging API.
conf.entryLayer = EntryLayer.channels
if args.ethRpcEndpoint == "":
# Create a basic configuration for the Waku node
# No RLN as we don't have an ETH RPC Endpoint
conf.preset = "logos.dev"
else:
# Connect to TWN, use ETH RPC Endpoint for RLN
conf.preset = "twn"
conf.ethClientUrls = @[EthRpcUrl(args.ethRpcEndpoint)]
# Create the full Logos Messaging stack (Waku + messaging + channels)
let node = (waitFor LogosDelivery.new(conf)).valueOr:
echo "Failed to create node: ", error
quit(QuitFailure)
echo("Logos Messaging node created successfully!")
# Start the node
(waitFor node.start()).isOkOr:
echo "Failed to start node: ", error
quit(QuitFailure)
echo "Node started successfully!"
asyncSpawn periodicSender(node)
runForever()