mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-02-28 06:03:39 +00:00
first step to have debug api in liblogosdelivery
This commit is contained in:
parent
ba85873f03
commit
a8fc3cef9d
44
liblogosdelivery/logos_delivery_api/debug_api.nim
Normal file
44
liblogosdelivery/logos_delivery_api/debug_api.nim
Normal file
@ -0,0 +1,44 @@
|
||||
import std/json
|
||||
|
||||
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 = parseEnum[NodeInfoId]($nodeInfoId)
|
||||
if infoItemIdEnum.isNone():
|
||||
return err("Invalid node info id: " & $nodeInfoId)
|
||||
return ok(ctx.myLib[].stateInfo.getNodeInfoItem(infoItemIdEnum.get()))
|
||||
|
||||
proc logosdelivery_get_available_configs(
|
||||
ctx: ptr FFIContext[Waku],
|
||||
callback: FFICallBack,
|
||||
userData: pointer,
|
||||
nodeInfoId: cstring,
|
||||
) {.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))
|
||||
@ -46,7 +46,8 @@ import
|
||||
factory/internal_config,
|
||||
factory/app_callbacks,
|
||||
],
|
||||
./waku_conf
|
||||
./waku_conf,
|
||||
./waku_state_info
|
||||
|
||||
logScope:
|
||||
topics = "wakunode waku"
|
||||
@ -55,7 +56,7 @@ logScope:
|
||||
const git_version* {.strdefine.} = "n/a"
|
||||
|
||||
type Waku* = ref object
|
||||
version: string
|
||||
stateInfo: WakuStateInfo
|
||||
conf*: WakuConf
|
||||
rng*: ref HmacDrbgContext
|
||||
|
||||
@ -78,9 +79,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
|
||||
) =
|
||||
@ -215,7 +213,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,
|
||||
|
||||
50
waku/factory/waku_state_info.nim
Normal file
50
waku/factory/waku_state_info.nim
Normal file
@ -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
|
||||
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 $self.node.peerId()
|
||||
else:
|
||||
return "unknown info item id"
|
||||
|
||||
proc init*(T: typedesc[WakuStateInfo], node: WakuNode): T =
|
||||
return WakuStateInfo(node: node)
|
||||
Loading…
x
Reference in New Issue
Block a user