Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions liblogosdelivery/examples/logosdelivery_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,23 @@ int main() {
// Wait for subscription
sleep(1);

printf("\n5. Sending a message...\n");
printf("\n5. Retrieving all possibl node info ids...\n");
logosdelivery_get_available_node_info_ids(ctx, simple_callback, (void *)"get_available_node_info_ids");

printf("\nRetrieving node info for a specific invalid ID...\n");
logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "WrongNodeInfoId");

printf("\nRetrieving several node info for specific correct IDs...\n");
logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "Version");
// logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "Metrics");
logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "MyMultiaddresses");
logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "MyENR");
logosdelivery_get_node_info(ctx, simple_callback, (void *)"get_node_info", "MyPeerId");

printf("\nRetrieving available configs...\n");
logosdelivery_get_available_configs(ctx, simple_callback, (void *)"get_available_configs");

printf("\n6. Sending a message...\n");
printf("Watch for message events (sent, propagated, or error):\n");
// Create base64-encoded payload: "Hello, Logos Messaging!"
const char *message = "{"
Expand All @@ -175,17 +191,17 @@ int main() {
printf("Waiting for message delivery events...\n");
sleep(60);

printf("\n6. Unsubscribing from content topic...\n");
printf("\n7. Unsubscribing from content topic...\n");
logosdelivery_unsubscribe(ctx, simple_callback, (void *)"unsubscribe", contentTopic);

sleep(1);

printf("\n7. Stopping node...\n");
printf("\n8. Stopping node...\n");
logosdelivery_stop_node(ctx, simple_callback, (void *)"stop_node");

sleep(1);

printf("\n8. Destroying context...\n");
printf("\n9. Destroying context...\n");
logosdelivery_destroy(ctx, simple_callback, (void *)"destroy");

printf("\n=== Example completed ===\n");
Expand Down
16 changes: 16 additions & 0 deletions liblogosdelivery/liblogosdelivery.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ extern "C"
FFICallBack callback,
void *userData);

// Retrieves the list of available node info IDs.
int logosdelivery_get_available_node_info_ids(void *ctx,
FFICallBack callback,
void *userData);

// Given a node info ID, retrieves the corresponding info.
int logosdelivery_get_node_info(void *ctx,
FFICallBack callback,
void *userData,
const char *nodeInfoId);

// Retrieves the list of available configurations.
int logosdelivery_get_available_configs(void *ctx,
FFICallBack callback,
void *userData);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 5 additions & 1 deletion liblogosdelivery/liblogosdelivery.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import waku/factory/waku, waku/node/waku_node, ./declare_lib

################################################################################
## Include different APIs, i.e. all procs with {.ffi.} pragma
include ./logos_delivery_api/node_api, ./logos_delivery_api/messaging_api

include
./logos_delivery_api/node_api,
./logos_delivery_api/messaging_api,
./logos_delivery_api/debug_api
46 changes: 46 additions & 0 deletions liblogosdelivery/logos_delivery_api/debug_api.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import std/[json, strutils]
import waku/factory/waku_state_info

proc logosdelivery_get_available_node_info_ids(
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
) {.ffi.} =
## Returns the list of all available node info item ids that
## can be queried with `get_node_info_item`.
requireInitializedNode(ctx, "GetNodeInfoIds"):
return err(errMsg)

return ok($ctx.myLib[].stateInfo.getAllPossibleInfoItemIds())

proc logosdelivery_get_node_info(
ctx: ptr FFIContext[Waku],
callback: FFICallBack,
userData: pointer,
nodeInfoId: cstring,
) {.ffi.} =
## Returns the content of the node info item with the given id if it exists.
requireInitializedNode(ctx, "GetNodeInfoItem"):
return err(errMsg)

let infoItemIdEnum =
try:
parseEnum[NodeInfoId]($nodeInfoId)
except ValueError:
return err("Invalid node info id: " & $nodeInfoId)

return ok(ctx.myLib[].stateInfo.getNodeInfoItem(infoItemIdEnum))

proc logosdelivery_get_available_configs(
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
) {.ffi.} =
## Returns information about the accepted config items.
## For analogy with a CLI app, this is the info when typing --help for a command.
requireInitializedNode(ctx, "GetAvailableConfigs"):
return err(errMsg)

## TODO: we are now returning a simple default value for NodeConfig.
## The NodeConfig struct is too complex and we need to have a flattened simpler config.
## The expected returned value for this is a list of possible config items with their
## description, accepted values, default value, etc.

let defaultConfig = NodeConfig.init()
return ok($(%*defaultConfig))
2 changes: 1 addition & 1 deletion tests/wakunode2/test_app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ suite "Wakunode2 - Waku":
raiseAssert error

## When
let version = waku.version
let version = waku.stateInfo.getNodeInfoItem(NodeInfoId.Version)

## Then
check:
Expand Down
10 changes: 4 additions & 6 deletions waku/factory/waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import
factory/internal_config,
factory/app_callbacks,
],
./waku_conf
./waku_conf,
./waku_state_info

logScope:
topics = "wakunode waku"
Expand All @@ -56,7 +57,7 @@ logScope:
const git_version* {.strdefine.} = "n/a"

type Waku* = ref object
version: string
stateInfo*: WakuStateInfo
conf*: WakuConf
rng*: ref HmacDrbgContext

Expand All @@ -79,9 +80,6 @@ type Waku* = ref object

brokerCtx*: BrokerContext

func version*(waku: Waku): string =
waku.version

proc setupSwitchServices(
waku: Waku, conf: WakuConf, circuitRelay: Relay, rng: ref HmacDrbgContext
) =
Expand Down Expand Up @@ -216,7 +214,7 @@ proc new*(
return err("could not create delivery service: " & $error)

var waku = Waku(
version: git_version,
stateInfo: WakuStateInfo.init(node),
conf: wakuConf,
rng: rng,
key: wakuConf.nodeKey,
Expand Down
50 changes: 50 additions & 0 deletions waku/factory/waku_state_info.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## This module is aimed to collect and provide information about the state of the node,
## such as its version, metrics values, etc.
## It has been originally designed to be used by the debug API, which acts as a consumer of
## this information, but any other module can populate the information it needs to be
## accessible through the debug API.

import std/[tables, sequtils, strutils]
import metrics, eth/p2p/discoveryv5/enr, libp2p/peerid
import waku/waku_node

type
NodeInfoId* {.pure.} = enum
Version
Metrics
MyMultiaddresses
MyENR
MyPeerId

WakuStateInfo* {.requiresInit.} = object
node: WakuNode

proc getAllPossibleInfoItemIds*(self: WakuStateInfo): seq[NodeInfoId] =
## Returns all possible options that can be queried to learn about the node's information.
var ret = newSeq[NodeInfoId](0)
for item in NodeInfoId:
ret.add(item)
return ret

proc getMetrics(): string =
{.gcsafe.}:
return defaultRegistry.toText() ## defaultRegistry is {.global.} in metrics module

proc getNodeInfoItem*(self: WakuStateInfo, infoItemId: NodeInfoId): string =
## Returns the content of the info item with the given id if it exists.
case infoItemId
of NodeInfoId.Version:
return git_version
of NodeInfoId.Metrics:
return getMetrics()
of NodeInfoId.MyMultiaddresses:
return self.node.info().listenAddresses.join(",")
of NodeInfoId.MyENR:
return self.node.enr.toURI()
of NodeInfoId.MyPeerId:
return $PeerId(self.node.peerId())
else:
return "unknown info item id"

proc init*(T: typedesc[WakuStateInfo], node: WakuNode): T =
return WakuStateInfo(node: node)
Loading