Skip to content

Commit

Permalink
Add the peerId to the transport.dial (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenut committed Jan 31, 2023
1 parent 4ace70d commit 0e28d3b
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 37 deletions.
3 changes: 1 addition & 2 deletions examples/circuitrelay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ proc main() {.async.} =
let
# Create a relay address to swDst using swRel as the relay
addrs = MultiAddress.init($swRel.peerInfo.addrs[0] & "/p2p/" &
$swRel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$swDst.peerInfo.peerId).get()
$swRel.peerInfo.peerId & "/p2p-circuit").get()

# Connect Dst to the relay
await swDst.connect(swRel.peerInfo.peerId, swRel.peerInfo.addrs)
Expand Down
2 changes: 1 addition & 1 deletion libp2p/dialer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ proc dialAndUpgrade(
let dialed =
try:
libp2p_total_dial_attempts.inc()
await transport.dial(hostname, address)
await transport.dial(hostname, address, peerId)
except CancelledError as exc:
debug "Dialing canceled", msg = exc.msg, peerId
raise exc
Expand Down
8 changes: 4 additions & 4 deletions libp2p/protocols/connectivity/relay/rtransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ proc dial*(self: RelayTransport, ma: MultiAddress): Future[Connection] {.async,
method dial*(
self: RelayTransport,
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)

method handles*(self: RelayTransport, ma: MultiAddress): bool {.gcsafe} =
if ma.protocols.isOk():
let sma = toSeq(ma.items())
if sma.len >= 3:
result = CircuitRelay.match(sma[^2].get()) and
P2PPattern.match(sma[^1].get())
result = sma.len >= 2 and CircuitRelay.match(sma[^1].get())
trace "Handles return", ma, result

proc new*(T: typedesc[RelayTransport], cl: RelayClient, upgrader: Upgrade): T =
Expand Down
2 changes: 1 addition & 1 deletion libp2p/services/autorelayservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ proc reserveAndUpdate(self: AutoRelayService, relayPid: PeerId, selfPid: PeerId)
let
rsvp = await self.client.reserve(relayPid).wait(chronos.seconds(5))
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
if ttl <= 60:
# A reservation under a minute is basically useless
Expand Down
3 changes: 2 additions & 1 deletion libp2p/transports/tcptransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ method accept*(self: TcpTransport): Future[Connection] {.async, gcsafe.} =
method dial*(
self: TcpTransport,
hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} =
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer
##

Expand Down
3 changes: 2 additions & 1 deletion libp2p/transports/tortransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ proc dialPeer(
method dial*(
self: TorTransport,
hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} =
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer
##
if not handlesDial(address):
Expand Down
6 changes: 4 additions & 2 deletions libp2p/transports/transport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ method accept*(self: Transport): Future[Connection]
method dial*(
self: Transport,
hostname: string,
address: MultiAddress): Future[Connection] {.base, gcsafe.} =
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.base, gcsafe.} =
## dial a peer
##

doAssert(false, "Not implemented!")

proc dial*(
self: Transport,
address: MultiAddress): Future[Connection] {.gcsafe.} =
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.gcsafe.} =
self.dial("", address)

method upgradeIncoming*(
Expand Down
3 changes: 2 additions & 1 deletion libp2p/transports/wstransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ method accept*(self: WsTransport): Future[Connection] {.async, gcsafe.} =
method dial*(
self: WsTransport,
hostname: string,
address: MultiAddress): Future[Connection] {.async, gcsafe.} =
address: MultiAddress,
peerId: Opt[PeerId] = Opt.none(PeerId)): Future[Connection] {.async, gcsafe.} =
## dial a peer
##

Expand Down
6 changes: 3 additions & 3 deletions tests/commoninterop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await rel.start()
let daemonNode = await newDaemonApi()
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()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel.connect(daemonPeer.peer, daemonPeer.addresses)
Expand Down Expand Up @@ -542,7 +542,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await dst.start()
let daemonNode = await newDaemonApi()
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()
await daemonNode.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
Expand Down Expand Up @@ -581,7 +581,7 @@ proc relayInteropTests*(name: string, relayCreator: SwitchCreator) =
await dst.start()
let daemonNode = await newDaemonApi({RelayHop})
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()
await src.connect(daemonPeer.peer, daemonPeer.addresses)
await daemonNode.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
Expand Down
3 changes: 1 addition & 2 deletions tests/testautorelay.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ proc createSwitch(r: Relay, autorelay: Service = nil): Switch =

proc buildRelayMA(switchRelay: Switch, switchClient: Switch): MultiAddress =
MultiAddress.init($switchRelay.peerInfo.addrs[0] & "/p2p/" &
$switchRelay.peerInfo.peerId & "/p2p-circuit/p2p/" &
$switchClient.peerInfo.peerId).get()
$switchRelay.peerInfo.peerId & "/p2p-circuit").get()

suite "Autorelay":
asyncTeardown:
Expand Down
2 changes: 1 addition & 1 deletion tests/testrelayv1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ suite "Circuit Relay":
await allFutures(dst2.stop())

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()
await src.connect(srelay.peerInfo.peerId, srelay.peerInfo.addrs)
await srelay.connect(dst.peerInfo.peerId, dst.peerInfo.addrs)
Expand Down
27 changes: 9 additions & 18 deletions tests/testrelayv2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ suite "Circuit Relay V2":
let
rv2add = Relay.new()
addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$src2.peerInfo.peerId).get() ]
$rel.peerInfo.peerId & "/p2p-circuit").get() ]
rv2add.setup(src2)
await rv2add.start()
src2.mount(rv2add)
Expand Down Expand Up @@ -164,8 +163,7 @@ suite "Circuit Relay V2":
await dst.start()

let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$dst.peerInfo.peerId).get()
$rel.peerInfo.peerId & "/p2p-circuit").get()

await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
Expand Down Expand Up @@ -201,8 +199,7 @@ suite "Circuit Relay V2":
await dst.start()

let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$dst.peerInfo.peerId).get()
$rel.peerInfo.peerId & "/p2p-circuit").get()

await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
Expand Down Expand Up @@ -250,8 +247,7 @@ take to the ship.""")
await dst.start()

let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$dst.peerInfo.peerId).get()
$rel.peerInfo.peerId & "/p2p-circuit").get()

await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
Expand Down Expand Up @@ -286,8 +282,7 @@ take to the ship.""")
await dst.start()

let addrs = MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$dst.peerInfo.peerId).get()
$rel.peerInfo.peerId & "/p2p-circuit").get()

await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await dst.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
Expand Down Expand Up @@ -332,8 +327,7 @@ take to the ship.""")
addrs = @[ MultiAddress.init($rel.peerInfo.addrs[0] & "/p2p/" &
$rel.peerInfo.peerId & "/p2p-circuit/p2p/" &
$rel2.peerInfo.peerId & "/p2p/" &
$rel2.peerInfo.peerId & "/p2p-circuit/p2p/" &
$dst.peerInfo.peerId).get() ]
$rel2.peerInfo.peerId & "/p2p-circuit").get() ]

await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
await rel2.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
Expand Down Expand Up @@ -392,14 +386,11 @@ take to the ship.""")

let
addrsABC = MultiAddress.init($switchB.peerInfo.addrs[0] & "/p2p/" &
$switchB.peerInfo.peerId & "/p2p-circuit/p2p/" &
$switchC.peerInfo.peerId).get()
$switchB.peerInfo.peerId & "/p2p-circuit").get()
addrsBCA = MultiAddress.init($switchC.peerInfo.addrs[0] & "/p2p/" &
$switchC.peerInfo.peerId & "/p2p-circuit/p2p/" &
$switchA.peerInfo.peerId).get()
$switchC.peerInfo.peerId & "/p2p-circuit").get()
addrsCAB = MultiAddress.init($switchA.peerInfo.addrs[0] & "/p2p/" &
$switchA.peerInfo.peerId & "/p2p-circuit/p2p/" &
$switchB.peerInfo.peerId).get()
$switchA.peerInfo.peerId & "/p2p-circuit").get()

await switchA.connect(switchB.peerInfo.peerId, switchB.peerInfo.addrs)
await switchB.connect(switchC.peerInfo.peerId, switchC.peerInfo.addrs)
Expand Down

0 comments on commit 0e28d3b

Please sign in to comment.