mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-13 16:25:00 +00:00
chore: support ping with multiple multiaddresses and close stream (#3154)
This commit is contained in:
parent
b42f4c7a89
commit
3665991a65
@ -1,4 +1,4 @@
|
|||||||
import std/json
|
import std/[json, strutils]
|
||||||
import chronos, results
|
import chronos, results
|
||||||
import libp2p/[protocols/ping, switch, multiaddress, multicodec]
|
import libp2p/[protocols/ping, switch, multiaddress, multicodec]
|
||||||
import ../../../../waku/[factory/waku, waku_core/peers, node/waku_node], ../../../alloc
|
import ../../../../waku/[factory/waku, waku_core/peers, node/waku_node], ../../../alloc
|
||||||
@ -25,12 +25,15 @@ proc process*(
|
|||||||
defer:
|
defer:
|
||||||
destroyShared(self)
|
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)
|
return err("PingRequest failed to parse peer addr: " & $error)
|
||||||
|
|
||||||
proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} =
|
proc ping(): Future[Result[Duration, string]] {.async, gcsafe.} =
|
||||||
try:
|
try:
|
||||||
let conn = await waku.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
|
let conn = await waku.node.switch.dial(peerInfo.peerId, peerInfo.addrs, PingCodec)
|
||||||
|
defer:
|
||||||
|
await conn.close()
|
||||||
|
|
||||||
let pingRTT = await waku.node.libp2pPing.ping(conn)
|
let pingRTT = await waku.node.libp2pPing.ping(conn)
|
||||||
if pingRTT == 0.nanos:
|
if pingRTT == 0.nanos:
|
||||||
return err("could not ping peer: rtt-0")
|
return err("could not ping peer: rtt-0")
|
||||||
|
@ -199,26 +199,37 @@ proc parsePeerInfoFromRegularAddr(peer: MultiAddress): Result[RemotePeerInfo, st
|
|||||||
|
|
||||||
return ok(RemotePeerInfo.init(peerId, @[wireAddr]))
|
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
|
## Parses a fully qualified peer multiaddr into dialable RemotePeerInfo
|
||||||
|
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))
|
||||||
|
|
||||||
let peerAddrStr = $peer
|
proc parsePeerInfo*(maddrs: varargs[string]): Result[RemotePeerInfo, string] =
|
||||||
|
|
||||||
if "p2p-circuit" in peerAddrStr:
|
|
||||||
return parsePeerInfoFromCircuitRelayAddr(peerAddrStr)
|
|
||||||
|
|
||||||
return parsePeerInfoFromRegularAddr(peer)
|
|
||||||
|
|
||||||
proc parsePeerInfo*(peer: string): Result[RemotePeerInfo, string] =
|
|
||||||
## Parses a fully qualified peer multiaddr, in the
|
## Parses a fully qualified peer multiaddr, in the
|
||||||
## format `(ip4|ip6)/tcp/p2p`, into dialable PeerInfo
|
## format `(ip4|ip6)/tcp/p2p`, into dialable PeerInfo
|
||||||
let multiAddr =
|
var multiAddresses = newSeq[MultiAddress]()
|
||||||
?MultiAddress.init(peer).mapErr(
|
for maddr in maddrs:
|
||||||
proc(err: string): string =
|
let multiAddr =
|
||||||
"MultiAddress.init [" & err & "]"
|
?MultiAddress.init(maddr).mapErr(
|
||||||
)
|
proc(err: string): string =
|
||||||
|
"MultiAddress.init [" & err & "]"
|
||||||
|
)
|
||||||
|
multiAddresses.add(multiAddr)
|
||||||
|
|
||||||
parsePeerInfo(multiAddr)
|
parsePeerInfo(multiAddresses)
|
||||||
|
|
||||||
func getTransportProtocol(typedR: TypedRecord): Option[IpTransportProtocol] =
|
func getTransportProtocol(typedR: TypedRecord): Option[IpTransportProtocol] =
|
||||||
if typedR.tcp6.isSome() or typedR.tcp.isSome():
|
if typedR.tcp6.isSome() or typedR.tcp.isSome():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user