Arseniy Klempner f3e6b51e9c
fix(option_shims): import wherever valueOr is called on std/Option
PR #3807 still uses std/Option.valueOr in ~30 places across logos_delivery
but does not consistently import waku/common/option_shims (the local
template that restores libp2p-1.15.2's valueOr/withValue for Option[T];
libp2p 1.15.3 dropped those overloads). Without the shim, nim picks up
only Result[T,E].valueOr from nim-results and fails with 'type mismatch'.

Surfaced when building liblogoschat which pulls a much wider slice of
nwaku than liblogosdelivery does. Each touched file now imports
option_shims either inline alongside other ../common/ entries or as a
standalone top-level import where the file uses absolute logos_delivery/
paths.

No semantic change — the shim restores the historical Option valueOr
behavior. Same files compiling against the older libp2p (where valueOr
on Option came from libp2p/utility) would behave identically.
2026-06-19 18:54:41 -04:00

93 lines
2.6 KiB
Nim

{.push raises: [].}
import logos_delivery/waku/common/option_shims
import
std/[options],
chronos,
chronicles,
metrics,
results,
libp2p/protocols/ping,
libp2p/builders,
libp2p/transports/tcptransport,
libp2p/utility
import ../waku_node, ../peer_manager
import libp2p/crypto/rng as libp2p_rng
logScope:
topics = "waku node ping api"
proc mountLibp2pPing*(node: WakuNode) {.async: (raises: []).} =
info "mounting libp2p ping protocol"
try:
# libp2p 1.15.3: Ping.new now expects libp2p's `Rng` (ref object
# wrapping a ref HmacDrbgContext). Wrap the node's BearSSL rng.
node.libp2pPing = Ping.new(rng = libp2p_rng.newBearSslRng(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