logos-messaging-nim/waku/factory/waku_state_info.nim
Fabiana Cecin 71a369ffad
feat: allow a port value of zero for service ports (auto-assign port) (#3828)
* any port set to 0 on conf results in a random port bound
* Debug API MyBoundPorts reports actually bound ports for all services, reports 0 if disabled
* write back bound values to both WakuConf and WakuNode.ports
* setupDiscoveryV5 returns Result and errors out on port 0
* rename setupAndStartDiscv5WithAutoPort to setupAndStartDiscv5
* updateWaku ENR rebuild now runs after discv5 startup
* Add DefaultP2pTcpPort, DefaultDiscv5UdpPort, DefaultWebSocketPort, DefaultRestPort, DefaultMetricsHttpPort
* add tests
2026-05-11 15:22:22 -03:00

54 lines
1.7 KiB
Nim

## 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, net/bound_ports]
type
NodeInfoId* {.pure.} = enum
Version
Metrics
MyMultiaddresses
MyENR
MyPeerId
MyBoundPorts
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())
of NodeInfoId.MyBoundPorts:
return $self.node.ports
else:
return "unknown info item id"
proc init*(T: typedesc[WakuStateInfo], node: WakuNode): T =
return WakuStateInfo(node: node)