Ivan FB 89d1f87bfe
Rename kernel_api dir to waku_node and tidy node module layout
Why these changes hang together:

- Rename `waku/node/kernel_api/` to `waku/node/waku_node/`: the folder
  holds the node's protocol APIs, so it should carry the node's name
  rather than the legacy "kernel_api" label.

- Collapse the old `kernel_api.nim` aggregator into the top-level
  `waku/waku_node.nim` barrel, and drop `net_config`/`health_monitor`
  from it. Those aren't the node's concern; consumers that used them
  now import them directly (clearer, explicit deps).

- Move the `WakuNode` type from `node_types.nim` into `waku_node.nim`.
  `node_types.nim` only existed to dodge a `WakuNode`/`SubscriptionManager`
  import cycle that Nim actually handles fine, so the type now lives in
  one obvious home and the indirection module is deleted.

- Extract `ShardSubscription` and `EdgeFilterSubState` into their own
  small modules: they are standalone value types with no back-reference
  to the node. `SubscriptionManager` stays with `WakuNode` on purpose
  (it is the node's subscription subsystem; the relationship is real).

Verified: `wakunode2` and `libwaku` build; representative node tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 13:59:07 +02:00

88 lines
2.4 KiB
Nim

{.push raises: [].}
import
std/[options],
chronos,
chronicles,
metrics,
results,
libp2p/protocols/ping,
libp2p/builders,
libp2p/transports/tcptransport,
libp2p/utility
import ../waku_node, ../peer_manager
logScope:
topics = "waku node ping api"
proc mountLibp2pPing*(node: WakuNode) {.async: (raises: []).} =
info "mounting libp2p ping protocol"
try:
node.libp2pPing = Ping.new(rng = node.rng)
except Exception as e:
error "failed to create ping", error = getCurrentExceptionMsg()
if node.started:
# Node has started already. Let's start ping too.
try:
await node.libp2pPing.start()
except CatchableError:
error "failed to start libp2pPing", error = getCurrentExceptionMsg()
try:
node.switch.mount(node.libp2pPing)
except LPError:
error "failed to mount libp2pPing", error = getCurrentExceptionMsg()
proc pingPeer(node: WakuNode, peerId: PeerId): Future[Result[void, string]] {.async.} =
## Ping a single peer and return the result
try:
# Establish a stream
let stream = (await node.peerManager.dialPeer(peerId, PingCodec)).valueOr:
error "pingPeer: failed dialing peer", peerId = peerId
return err("pingPeer failed dialing peer peerId: " & $peerId)
defer:
# Always close the stream
try:
await stream.close()
except CatchableError as e:
info "Error closing ping connection", peerId = peerId, error = e.msg
# Perform ping
let pingDuration = await node.libp2pPing.ping(stream)
trace "Ping successful", peerId = peerId, duration = pingDuration
return ok()
except CatchableError as e:
error "pingPeer: exception raised pinging peer", peerId = peerId, error = e.msg
return err("pingPeer: exception raised pinging peer: " & e.msg)
# Returns the number of succesful pings performed
proc parallelPings*(node: WakuNode, peerIds: seq[PeerId]): Future[int] {.async.} =
if len(peerIds) == 0:
return 0
var pingFuts: seq[Future[Result[void, string]]]
# Create ping futures for each peer
for i, peerId in peerIds:
let fut = pingPeer(node, peerId)
pingFuts.add(fut)
# Wait for all pings to complete
discard await allFutures(pingFuts).withTimeout(5.seconds)
var successCount = 0
for fut in pingFuts:
if not fut.completed() or fut.failed():
continue
let res = fut.read()
if res.isOk():
successCount.inc()
return successCount