Skip to content

Commit

Permalink
chore: support ping with multiple multiaddresses and close stream (#3154
Browse files Browse the repository at this point in the history
)
  • Loading branch information
richard-ramos authored Oct 28, 2024
1 parent b42f4c7 commit 3665991
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/json
import std/[json, strutils]
import chronos, results
import libp2p/[protocols/ping, switch, multiaddress, multicodec]
import ../../../../waku/[factory/waku, waku_core/peers, node/waku_node], ../../../alloc
Expand All @@ -25,12 +25,15 @@ proc process*(
defer:
destroyShared(self)

let peerInfo = peers.parsePeerInfo($self[].peerAddr).valueOr:
let peerInfo = peers.parsePeerInfo(($self[].peerAddr).split(",")).valueOr:
return err("PingRequest failed to parse peer addr: " & $error)

proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} =
try:
let conn = await waku.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
defer:
await conn.close()

let pingRTT = await waku.node.libp2pPing.ping(conn)
if pingRTT == 0.nanos:
return err("could not ping peer: rtt-0")
Expand Down
43 changes: 27 additions & 16 deletions waku/waku_core/peers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,37 @@ proc parsePeerInfoFromRegularAddr(peer: MultiAddress): Result[RemotePeerInfo, st

return ok(RemotePeerInfo.init(peerId, @[wireAddr]))

proc parsePeerInfo*(peer: MultiAddress): Result[RemotePeerInfo, string] =
proc parsePeerInfo*(maddrs: varargs[MultiAddress]): Result[RemotePeerInfo, string] =
## Parses a fully qualified peer multiaddr into dialable RemotePeerInfo

let peerAddrStr = $peer

if "p2p-circuit" in peerAddrStr:
return parsePeerInfoFromCircuitRelayAddr(peerAddrStr)

return parsePeerInfoFromRegularAddr(peer)

proc parsePeerInfo*(peer: string): Result[RemotePeerInfo, string] =
var peerID: PeerID
var addrs = newSeq[MultiAddress]()
for i in 0 ..< maddrs.len:
let peerAddrStr = $maddrs[i]
let peerInfo =
if "p2p-circuit" in peerAddrStr:
?parsePeerInfoFromCircuitRelayAddr(peerAddrStr)
else:
?parsePeerInfoFromRegularAddr(maddrs[i])
if i == 0:
peerID = peerInfo.peerID
elif peerID.cmp(peerInfo.peerID) != 0:
return err("Error in parsePeerInfo: multiple peerIds received")
addrs.add(peerInfo.addrs[0])
return ok(RemotePeerInfo.init(peerID, addrs))

proc parsePeerInfo*(maddrs: varargs[string]): Result[RemotePeerInfo, string] =
## Parses a fully qualified peer multiaddr, in the
## format `(ip4|ip6)/tcp/p2p`, into dialable PeerInfo
let multiAddr =
?MultiAddress.init(peer).mapErr(
proc(err: string): string =
"MultiAddress.init [" & err & "]"
)
var multiAddresses = newSeq[MultiAddress]()
for maddr in maddrs:
let multiAddr =
?MultiAddress.init(maddr).mapErr(
proc(err: string): string =
"MultiAddress.init [" & err & "]"
)
multiAddresses.add(multiAddr)

parsePeerInfo(multiAddr)
parsePeerInfo(multiAddresses)

func getTransportProtocol(typedR: TypedRecord): Option[IpTransportProtocol] =
if typedR.tcp6.isSome() or typedR.tcp.isSome():
Expand Down

0 comments on commit 3665991

Please sign in to comment.