mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-06-28 12:30:09 +00:00
* bump nim-libp2p pin to v2.0.0 tag * bump json_rpc to v0.6.1, lsquic to v0.5.1, boringssl to v0.0.8 (latest tags) * add libp2p_mix dep; repoint libp2p/protocols/mix -> libp2p_mix * pin nimble.lock: websock / protobuf_serialization / npeg / jwt * Makefile: add -d:libp2p_quic_support * regenerate nix/deps.nix (adds libp2p_mix, refreshes pins) * migrate rng ref HmacDrbgContext -> libp2p Rng across prod/channels/tests (interface-only; same DRBG) * waku_switch: TransportConfig factory; unified 2.0.0 connection limits (withMaxInOut, withMaxConnections); local MaxConnections * waku_relay/rendezvous/discv5/kademlia: v2.0.0 API (rng, config, ServiceDiscovery rename) * call Service.setup() on post-build switch services (2.0.0 split setup/start) * drop libp2p/utils/semaphore -> chronos AsyncSemaphore * add logos_delivery/waku/compat/option_valueor shim (Option[T] valueOr/withValue, dropped upstream) * add std/options where a transitive re-export was removed * add newStandardSwitch shim (libp2p removed it in 2.0.0); mounts yamux+mplex to match prod muxer * PeerId.random(rng); common.rng()/crypto.newRng(); hoist shared rng (instantiation cleanup) * update expectations for 2.0.0 defaults: DEFAULT_PROTOCOLS += /ipfs/id/push/1.0.0; agent "nim-libp2p" * drop relay reboot/reconnect test (asserted a Switch restart capability that is simply not supported) * fix up a few tests that were flaking on MacOS (libp2p upgrade may have exposed these)
89 lines
2.4 KiB
Nim
89 lines
2.4 KiB
Nim
import logos_delivery/waku/compat/option_valueor
|
|
{.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
|