diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 995b7fbb3..b112e1d38 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -206,30 +206,30 @@ proc dial*(s: Switch, Future[Connection] {.async.} = let id = peer.id trace "Dialing peer", peer = id - result = s.connections.getOrDefault(id) - if result.isNil or result.closed: + var conn = s.connections.getOrDefault(id) + if conn.isNil or conn.closed: for t in s.transports: # for each transport for a in peer.addrs: # for each address - if t.handles(a): # check if it can dial it + if t.handles(a): # check if it can dial it trace "Dialing address", address = $a - result = await t.dial(a) + conn = await t.dial(a) # make sure to assign the peer to the connection - result.peerInfo = peer - result = await s.upgradeOutgoing(result) - if isNil(result): + conn.peerInfo = peer + conn = await s.upgradeOutgoing(conn) + if isNil(conn): continue - result.closeEvent.wait() + conn.closeEvent.wait() .addCallback do (udata: pointer): - asyncCheck s.cleanupConn(result) + asyncCheck s.cleanupConn(conn) break else: trace "Reusing existing connection" - if isNil(result): - raise newException(CatchableError, "unable to establish outgoing link!") + if isNil(conn): + raise newException(CatchableError, "Unable to establish outgoing link") - if proto.len > 0 and not result.closed: + if proto.len > 0 and not conn.closed: let stream = await s.getMuxedStream(peer) if not isNil(stream): trace "Connection is muxed, return muxed stream" diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 156a2f570..5e5472d1f 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -34,28 +34,28 @@ method init(p: TestProto) {.gcsafe.} = p.codec = TestCodec p.handler = handle +proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) = + let seckey = PrivateKey.random(RSA) + var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA)) + peerInfo.addrs.add(ma) + let identify = newIdentify(peerInfo) + + proc createMplex(conn: Connection): Muxer = + result = newMplex(conn) + + let mplexProvider = newMuxerProvider(createMplex, MplexCodec) + let transports = @[Transport(newTransport(TcpTransport))] + let muxers = [(MplexCodec, mplexProvider)].toTable() + let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable() + let switch = newSwitch(peerInfo, + transports, + identify, + muxers, + secureManagers) + result = (switch, peerInfo) + suite "Switch": - test "e2e use switch": - proc createSwitch(ma: MultiAddress): (Switch, PeerInfo) {.gcsafe.}= - let seckey = PrivateKey.random(RSA) - var peerInfo: PeerInfo = PeerInfo.init(PrivateKey.random(RSA)) - peerInfo.addrs.add(ma) - let identify = newIdentify(peerInfo) - - proc createMplex(conn: Connection): Muxer = - result = newMplex(conn) - - let mplexProvider = newMuxerProvider(createMplex, MplexCodec) - let transports = @[Transport(newTransport(TcpTransport))] - let muxers = [(MplexCodec, mplexProvider)].toTable() - let secureManagers = [(SecioCodec, Secure(newSecio(peerInfo.privateKey)))].toTable() - let switch = newSwitch(peerInfo, - transports, - identify, - muxers, - secureManagers) - result = (switch, peerInfo) - + test "e2e use switch dial proto string": proc testSwitch(): Future[bool] {.async, gcsafe.} = let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") @@ -82,3 +82,24 @@ suite "Switch": check: waitFor(testSwitch()) == true + + test "e2e use switch no proto string": + proc testSwitch(): Future[bool] {.async, gcsafe.} = + let ma1: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") + let ma2: MultiAddress = Multiaddress.init("/ip4/0.0.0.0/tcp/0") + + var peerInfo1, peerInfo2: PeerInfo + var switch1, switch2: Switch + (switch1, peerInfo1) = createSwitch(ma1) + asyncCheck switch1.start() + + (switch2, peerInfo2) = createSwitch(ma2) + asyncCheck switch2.start() + var conn = await switch2.dial(switch1.peerInfo) + + check isNil(conn) + discard allFutures(switch1.stop(), switch2.stop()) + result = true + + check: + waitFor(testSwitch()) == true