Add the peerId to the transport.dial (#842)

This commit is contained in:
Ludovic Chenut 2023-01-31 12:46:10 +01:00 committed by GitHub
parent 4ace70d53b
commit 0e28d3b828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 31 additions and 37 deletions

View File

@ -57,8 +57,7 @@ proc main() {.async.} =
let let
# Create a relay address to swDst using swRel as the relay # Create a relay address to swDst using swRel as the relay
addrs = MultiAddress.init($swRel.peerInfo.addrs[0] & "/p2p/" & addrs = MultiAddress.init($swRel.peerInfo.addrs[0] & "/p2p/" &
$swRel.peerInfo.peerId & "/p2p-circuit/p2p/" & $swRel.peerInfo.peerId & "/p2p-circuit").get()
$swDst.peerInfo.peerId).get()
# Connect Dst to the relay # Connect Dst to the relay
await swDst.connect(swRel.peerInfo.peerId, swRel.peerInfo.addrs) await swDst.connect(swRel.peerInfo.peerId, swRel.peerInfo.addrs)

View File

@ -60,7 +60,7 @@ proc dialAndUpgrade(
let dialed = let dialed =
try: try:
libp2p_total_dial_attempts.inc() libp2p_total_dial_attempts.inc()
await transport.dial(hostname, address) await transport.dial(hostname, address, peerId)
except CancelledError as exc: except CancelledError as exc:
debug "Dialing canceled", msg = exc.msg, peerId debug "Dialing canceled", msg = exc.msg, peerId
raise exc raise exc

View File

@ -92,15 +92,15 @@ proc dial*(self: RelayTransport, ma: MultiAddress): Future[Connection] {.async,
method dial*( method dial*(
self: RelayTransport, self: RelayTransport,
hostname: string, hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} = ma: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
let address = MultiAddress.init($ma & "/p2p/" & $peerId.get()).tryGet()
result = await self.dial(address) result = await self.dial(address)
method handles*(self: RelayTransport, ma: MultiAddress): bool {.gcsafe} = method handles*(self: RelayTransport, ma: MultiAddress): bool {.gcsafe} =
if ma.protocols.isOk(): if ma.protocols.isOk():
let sma = toSeq(ma.items()) let sma = toSeq(ma.items())
if sma.len >= 3: result = sma.len >= 2 and CircuitRelay.match(sma[^1].get())
result = CircuitRelay.match(sma[^2].get()) and
P2PPattern.match(sma[^1].get())
trace "Handles return", ma, result trace "Handles return", ma, result
proc new*(T: typedesc[RelayTransport], cl: RelayClient, upgrader: Upgrade): T = proc new*(T: typedesc[RelayTransport], cl: RelayClient, upgrader: Upgrade): T =

View File

@ -39,7 +39,7 @@ proc reserveAndUpdate(self: AutoRelayService, relayPid: PeerId, selfPid: PeerId)
let let
rsvp = await self.client.reserve(relayPid).wait(chronos.seconds(5)) rsvp = await self.client.reserve(relayPid).wait(chronos.seconds(5))
relayedAddr = rsvp.addrs.mapIt( relayedAddr = rsvp.addrs.mapIt(
MultiAddress.init($it & "/p2p-circuit/p2p/" & $selfPid).tryGet()) MultiAddress.init($it & "/p2p-circuit").tryGet())
ttl = rsvp.expire.int64 - times.now().utc.toTime.toUnix ttl = rsvp.expire.int64 - times.now().utc.toTime.toUnix
if ttl <= 60: if ttl <= 60:
# A reservation under a minute is basically useless # A reservation under a minute is basically useless

View File

@ -246,7 +246,8 @@ method accept*(self: TcpTransport): Future[Connection] {.async, gcsafe.} =
method dial*( method dial*(
self: TcpTransport, self: TcpTransport,
hostname: string, hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} = address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer ## dial a peer
## ##

View File

@ -188,7 +188,8 @@ proc dialPeer(
method dial*( method dial*(
self: TorTransport, self: TorTransport,
hostname: string, hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} = address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer ## dial a peer
## ##
if not handlesDial(address): if not handlesDial(address):

View File

@ -65,7 +65,8 @@ method accept*(self: Transport): Future[Connection]
method dial*( method dial*(
self: Transport, self: Transport,
hostname: string, hostname: string,
address: MultiAddress): Future[Connection] {.base, gcsafe.} = address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.base, gcsafe.} =
## dial a peer ## dial a peer
## ##
@ -73,7 +74,8 @@ method dial*(
proc dial*( proc dial*(
self: Transport, self: Transport,
address: MultiAddress): Future[Connection] {.gcsafe.} = address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.gcsafe.} =
self.dial("", address) self.dial("", address)
method upgradeIncoming*( method upgradeIncoming*(

View File

@ -292,7 +292,8 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
method dial*( method dial*(
self: WsTransport, self: WsTransport,
hostname: string, hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} = address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer ## dial a peer
## ##

View File

@ -499,7 +499,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await rel.start() await rel.start()
let daemonNode = await newDaemonApi() let daemonNode = await newDaemonApi()
let daemonPeer = await daemonNode.identity() let daemonPeer = await daemonNode.identity()
let maStr = $rel.peerInfo.addrs[0] & "/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $daemonPeer.peer let maStr = $rel.peerInfo.addrs[0] & "/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit"
let maddr = MultiAddress.init(maStr).tryGet() let maddr = MultiAddress.init(maStr).tryGet()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel.connect(daemonPeer.peer, daemonPeer.addresses) await rel.connect(daemonPeer.peer, daemonPeer.addresses)
@ -542,7 +542,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await dst.start() await dst.start()
let daemonNode = await newDaemonApi() let daemonNode = await newDaemonApi()
let daemonPeer = await daemonNode.identity() let daemonPeer = await daemonNode.identity()
let maStr = $rel.peerInfo.addrs[0] & "/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $dst.peerInfo.peerId let maStr = $rel.peerInfo.addrs[0] & "/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit"
let maddr = MultiAddress.init(maStr).tryGet() let maddr = MultiAddress.init(maStr).tryGet()
await daemonNode.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await daemonNode.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel.connect(dst.peerInfo.peerId, dst.peerInfo.addrs) await rel.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
@ -581,7 +581,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await dst.start() await dst.start()
let daemonNode = await newDaemonApi({RelayHop}) let daemonNode = await newDaemonApi({RelayHop})
let daemonPeer = await daemonNode.identity() let daemonPeer = await daemonNode.identity()
let maStr = $daemonPeer.addresses[0] & "/p2p/" & $daemonPeer.peer & "/p2p-circuit/p2p/" & $dst.peerInfo.peerId let maStr = $daemonPeer.addresses[0] & "/p2p/" & $daemonPeer.peer & "/p2p-circuit"
let maddr = MultiAddress.init(maStr).tryGet() let maddr = MultiAddress.init(maStr).tryGet()
await src.connect(daemonPeer.peer, daemonPeer.addresses) await src.connect(daemonPeer.peer, daemonPeer.addresses)
await daemonNode.connect(dst.peerInfo.peerId, dst.peerInfo.addrs) await daemonNode.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)

View File

@ -26,8 +26,7 @@ proc createSwitch(r: Relay, autorelay: Service = nil): Switch =
proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress = proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress =
MultiAddress.init($switchRelay.peerInfo.addrs[0] & "/p2p/" & MultiAddress.init($switchRelay.peerInfo.addrs[0] & "/p2p/" &
$switchRelay.peerInfo.peerId & "/p2p-circuit/p2p/" & $switchRelay.peerInfo.peerId & "/p2p-circuit").get()
$switchClient.peerInfo.peerId).get()
suite "Autorelay": suite "Autorelay":
asyncTeardown: asyncTeardown:

View File

@ -257,7 +257,7 @@ suite "Circuit Relay":
await allFutures(dst2.stop()) await allFutures(dst2.stop())
asyncTest "Dial Peer": asyncTest "Dial Peer":
let maStr = $srelay.peerInfo.addrs[0] & "/p2p/" & $srelay.peerInfo.peerId & "/p2p-circuit/p2p/" & $dst.peerInfo.peerId let maStr = $srelay.peerInfo.addrs[0] & "/p2p/" & $srelay.peerInfo.peerId & "/p2p-circuit"
let maddr = MultiAddress.init(maStr).tryGet() let maddr = MultiAddress.init(maStr).tryGet()
await src.connect(srelay.peerInfo.peerId, srelay.peerInfo.addrs) await src.connect(srelay.peerInfo.peerId, srelay.peerInfo.addrs)
await srelay.connect(dst.peerInfo.peerId, dst.peerInfo.addrs) await srelay.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)

View File

@ -98,8 +98,7 @@ suite "Circuit Relay V2":
let let
rv2add = Relay.new() rv2add = Relay.new()
addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit").get() ]
$src2.peerInfo.peerId).get() ]
rv2add.setup(src2) rv2add.setup(src2)
await rv2add.start() await rv2add.start()
src2.mount(rv2add) src2.mount(rv2add)
@ -164,8 +163,7 @@ suite "Circuit Relay V2":
await dst.start() await dst.start()
let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit").get()
$dst.peerInfo.peerId).get()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
@ -201,8 +199,7 @@ suite "Circuit Relay V2":
await dst.start() await dst.start()
let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit").get()
$dst.peerInfo.peerId).get()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
@ -250,8 +247,7 @@ take to the ship.""")
await dst.start() await dst.start()
let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit").get()
$dst.peerInfo.peerId).get()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
@ -286,8 +282,7 @@ take to the ship.""")
await dst.start() await dst.start()
let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit").get()
$dst.peerInfo.peerId).get()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
@ -332,8 +327,7 @@ take to the ship.""")
addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" & addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$rel2.peerInfo.peerId & "/p2p/" & $rel2.peerInfo.peerId & "/p2p/" &
$rel2.peerInfo.peerId & "/p2p-circuit/p2p/" & $rel2.peerInfo.peerId & "/p2p-circuit").get() ]
$dst.peerInfo.peerId).get() ]
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel2.connect(rel.peerInfo.peerId, rel.peerInfo.addrs) await rel2.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
@ -392,14 +386,11 @@ take to the ship.""")
let let
addrsABC = MultiAddress.init($switchB.peerInfo.addrs[0] & "/p2p/" & addrsABC = MultiAddress.init($switchB.peerInfo.addrs[0] & "/p2p/" &
$switchB.peerInfo.peerId & "/p2p-circuit/p2p/" & $switchB.peerInfo.peerId & "/p2p-circuit").get()
$switchC.peerInfo.peerId).get()
addrsBCA = MultiAddress.init($switchC.peerInfo.addrs[0] & "/p2p/" & addrsBCA = MultiAddress.init($switchC.peerInfo.addrs[0] & "/p2p/" &
$switchC.peerInfo.peerId & "/p2p-circuit/p2p/" & $switchC.peerInfo.peerId & "/p2p-circuit").get()
$switchA.peerInfo.peerId).get()
addrsCAB = MultiAddress.init($switchA.peerInfo.addrs[0] & "/p2p/" & addrsCAB = MultiAddress.init($switchA.peerInfo.addrs[0] & "/p2p/" &
$switchA.peerInfo.peerId & "/p2p-circuit/p2p/" & $switchA.peerInfo.peerId & "/p2p-circuit").get()
$switchB.peerInfo.peerId).get()
await switchA.connect(switchB.peerInfo.peerId, switchB.peerInfo.addrs) await switchA.connect(switchB.peerInfo.peerId, switchB.peerInfo.addrs)
await switchB.connect(switchC.peerInfo.peerId, switchC.peerInfo.addrs) await switchB.connect(switchC.peerInfo.peerId, switchC.peerInfo.addrs)