diff --git a/libp2p/dial.nim b/libp2p/dial.nim index 9d5012e321..45146ada48 100644 --- a/libp2p/dial.nim +++ b/libp2p/dial.nim @@ -15,7 +15,9 @@ import peerid, stream/connection, transports/transport export results -type Dial* = ref object of RootObj +type + Dial* = ref object of RootObj + DialFailedError* = object of LPError method connect*( self: Dial, @@ -24,7 +26,7 @@ method connect*( forceDial = false, reuseConnection = true, dir = Direction.Out, -) {.async, base.} = +) {.base, async: (raises: [DialFailedError, CancelledError]).} = ## connect remote peer without negotiating ## a protocol ## @@ -33,14 +35,14 @@ method connect*( method connect*( self: Dial, address: MultiAddress, allowUnknownPeerId = false -): Future[PeerId] {.async, base.} = +): Future[PeerId] {.base, async: (raises: [DialFailedError, CancelledError]).} = ## Connects to a peer and retrieve its PeerId doAssert(false, "Not implemented!") method dial*( self: Dial, peerId: PeerId, protos: seq[string] -): Future[Connection] {.async, base.} = +): Future[Connection] {.base, async: (raises: [DialFailedError, CancelledError]).} = ## create a protocol stream over an ## existing connection ## @@ -53,7 +55,7 @@ method dial*( addrs: seq[MultiAddress], protos: seq[string], forceDial = false, -): Future[Connection] {.async, base.} = +): Future[Connection] {.base, async: (raises: [DialFailedError, CancelledError]).} = ## create a protocol stream and establish ## a connection if one doesn't exist already ## @@ -65,5 +67,7 @@ method addTransport*(self: Dial, transport: Transport) {.base.} = method tryDial*( self: Dial, peerId: PeerId, addrs: seq[MultiAddress] -): Future[Opt[MultiAddress]] {.async, base.} = +): Future[Opt[MultiAddress]] {. + base, async: (raises: [DialFailedError, CancelledError]) +.} = doAssert(false, "Not implemented!") diff --git a/libp2p/dialer.nim b/libp2p/dialer.nim index 81a7c83fd0..b43ccbdd6c 100644 --- a/libp2p/dialer.nim +++ b/libp2p/dialer.nim @@ -36,16 +36,13 @@ declareCounter(libp2p_total_dial_attempts, "total attempted dials") declareCounter(libp2p_successful_dials, "dialed successful peers") declareCounter(libp2p_failed_dials, "failed dials") -type - DialFailedError* = object of LPError - - Dialer* = ref object of Dial - localPeerId*: PeerId - connManager: ConnManager - dialLock: Table[PeerId, AsyncLock] - transports: seq[Transport] - peerStore: PeerStore - nameResolver: NameResolver +type Dialer* = ref object of Dial + localPeerId*: PeerId + connManager: ConnManager + dialLock: Table[PeerId, AsyncLock] + transports: seq[Transport] + peerStore: PeerStore + nameResolver: NameResolver proc dialAndUpgrade( self: Dialer, @@ -53,7 +50,7 @@ proc dialAndUpgrade( hostname: string, address: MultiAddress, dir = Direction.Out, -): Future[Muxer] {.async.} = +): Future[Muxer] {.async: (raises: [CancelledError]).} = for transport in self.transports: # for each transport if transport.handles(address): # check if it can dial it trace "Dialing address", address, peerId = peerId.get(default(PeerId)), hostname @@ -105,7 +102,9 @@ proc dialAndUpgrade( proc expandDnsAddr( self: Dialer, peerId: Opt[PeerId], address: MultiAddress -): Future[seq[(MultiAddress, Opt[PeerId])]] {.async.} = +): Future[seq[(MultiAddress, Opt[PeerId])]] {. + async: (raises: [CancelledError, MaError, TransportAddressError, LPError]) +.} = if not DNSADDR.matchPartial(address): return @[(address, peerId)] if isNil(self.nameResolver): @@ -115,7 +114,10 @@ proc expandDnsAddr( let toResolve = if peerId.isSome: - address & MultiAddress.init(multiCodec("p2p"), peerId.tryGet()).tryGet() + try: + address & MultiAddress.init(multiCodec("p2p"), peerId.tryGet()).tryGet() + except ResultError[void]: + raiseAssert "checked with if" else: address resolved = await self.nameResolver.resolveDnsAddr(toResolve) @@ -132,7 +134,9 @@ proc expandDnsAddr( proc dialAndUpgrade( self: Dialer, peerId: Opt[PeerId], addrs: seq[MultiAddress], dir = Direction.Out -): Future[Muxer] {.async.} = +): Future[Muxer] {. + async: (raises: [CancelledError, MaError, TransportAddressError, LPError]) +.} = debug "Dialing peer", peerId = peerId.get(default(PeerId)), addrs for rawAddress in addrs: @@ -169,47 +173,59 @@ proc internalConnect( forceDial: bool, reuseConnection = true, dir = Direction.Out, -): Future[Muxer] {.async.} = +): Future[Muxer] {.async: (raises: [DialFailedError, CancelledError]).} = if Opt.some(self.localPeerId) == peerId: - raise newException(CatchableError, "can't dial self!") + raise newException(DialFailedError, "can't dial self!") # Ensure there's only one in-flight attempt per peer let lock = self.dialLock.mgetOrPut(peerId.get(default(PeerId)), newAsyncLock()) - try: - await lock.acquire() - - if reuseConnection: - peerId.withValue(peerId): - self.tryReusingConnection(peerId).withValue(mux): - return mux - - let slot = self.connManager.getOutgoingSlot(forceDial) - let muxed = - try: - await self.dialAndUpgrade(peerId, addrs, dir) - except CatchableError as exc: - slot.release() - raise exc - slot.trackMuxer(muxed) - if isNil(muxed): # None of the addresses connected - raise newException(DialFailedError, "Unable to establish outgoing link") + await lock.acquire() + defer: + try: + lock.release() + except AsyncLockError: + raiseAssert "lock must have been acquired in line above" + + if reuseConnection: + peerId.withValue(peerId): + self.tryReusingConnection(peerId).withValue(mux): + return mux + let slot = try: - self.connManager.storeMuxer(muxed) - await self.peerStore.identify(muxed) - await self.connManager.triggerPeerEvents( - muxed.connection.peerId, - PeerEvent(kind: PeerEventKind.Identified, initiator: true), - ) - except CatchableError as exc: - trace "Failed to finish outgoung upgrade", description = exc.msg - await muxed.close() + self.connManager.getOutgoingSlot(forceDial) + except TooManyConnectionsError as exc: + raise newException(DialFailedError, exc.msg) + + let muxed = + try: + await self.dialAndUpgrade(peerId, addrs, dir) + except CancelledError as exc: + slot.release() raise exc + except CatchableError as exc: + slot.release() + raise newException(DialFailedError, exc.msg) + slot.trackMuxer(muxed) + if isNil(muxed): # None of the addresses connected + raise newException(DialFailedError, "Unable to establish outgoing link") + + try: + self.connManager.storeMuxer(muxed) + await self.peerStore.identify(muxed) + await self.connManager.triggerPeerEvents( + muxed.connection.peerId, + PeerEvent(kind: PeerEventKind.Identified, initiator: true), + ) return muxed - finally: - if lock.locked(): - lock.release() + except CancelledError as exc: + await muxed.close() + raise exc + except CatchableError as exc: + trace "Failed to finish outgoing upgrade", description = exc.msg + await muxed.close() + raise newException(DialFailedError, "Failed to finish outgoing upgrade") method connect*( self: Dialer, @@ -218,7 +234,7 @@ method connect*( forceDial = false, reuseConnection = true, dir = Direction.Out, -) {.async.} = +) {.async: (raises: [DialFailedError, CancelledError]).} = ## connect remote peer without negotiating ## a protocol ## @@ -231,7 +247,7 @@ method connect*( method connect*( self: Dialer, address: MultiAddress, allowUnknownPeerId = false -): Future[PeerId] {.async.} = +): Future[PeerId] {.async: (raises: [DialFailedError, CancelledError]).} = ## Connects to a peer and retrieve its PeerId parseFullAddress(address).toOpt().withValue(fullAddress): @@ -249,7 +265,7 @@ method connect*( proc negotiateStream( self: Dialer, conn: Connection, protos: seq[string] -): Future[Connection] {.async.} = +): Future[Connection] {.async: (raises: [CatchableError]).} = trace "Negotiating stream", conn, protos let selected = await MultistreamSelect.select(conn, protos) if not protos.contains(selected): @@ -260,7 +276,7 @@ proc negotiateStream( method tryDial*( self: Dialer, peerId: PeerId, addrs: seq[MultiAddress] -): Future[Opt[MultiAddress]] {.async.} = +): Future[Opt[MultiAddress]] {.async: (raises: [DialFailedError, CancelledError]).} = ## Create a protocol stream in order to check ## if a connection is possible. ## Doesn't use the Connection Manager to save it. @@ -280,17 +296,24 @@ method tryDial*( method dial*( self: Dialer, peerId: PeerId, protos: seq[string] -): Future[Connection] {.async.} = +): Future[Connection] {.async: (raises: [DialFailedError, CancelledError]).} = ## create a protocol stream over an ## existing connection ## trace "Dialing (existing)", peerId, protos - let stream = await self.connManager.getStream(peerId) - if stream.isNil: - raise newException(DialFailedError, "Couldn't get muxed stream") - return await self.negotiateStream(stream, protos) + try: + let stream = await self.connManager.getStream(peerId) + if stream.isNil: + raise newException(DialFailedError, "Couldn't get muxed stream") + return await self.negotiateStream(stream, protos) + except CancelledError as exc: + trace "Dial canceled" + raise exc + except CatchableError as exc: + trace "Error dialing", description = exc.msg + raise newException(DialFailedError, exc.msg) method dial*( self: Dialer, @@ -298,7 +321,7 @@ method dial*( addrs: seq[MultiAddress], protos: seq[string], forceDial = false, -): Future[Connection] {.async.} = +): Future[Connection] {.async: (raises: [DialFailedError, CancelledError]).} = ## create a protocol stream and establish ## a connection if one doesn't exist already ## @@ -307,7 +330,7 @@ method dial*( conn: Muxer stream: Connection - proc cleanup() {.async.} = + proc cleanup() {.async: (raises: []).} = if not (isNil(stream)): await stream.closeWithEOF() @@ -331,7 +354,7 @@ method dial*( except CatchableError as exc: debug "Error dialing", conn, description = exc.msg await cleanup() - raise exc + raise newException(DialFailedError, exc.msg) method addTransport*(self: Dialer, t: Transport) = self.transports &= t diff --git a/libp2p/protocols/connectivity/autonat/client.nim b/libp2p/protocols/connectivity/autonat/client.nim index c20b724109..0347c6c8ea 100644 --- a/libp2p/protocols/connectivity/autonat/client.nim +++ b/libp2p/protocols/connectivity/autonat/client.nim @@ -47,7 +47,7 @@ method dialMe*( await switch.dial(pid, @[AutonatCodec]) else: await switch.dial(pid, addrs, AutonatCodec) - except CatchableError as err: + except DialFailedError as err: raise newException(AutonatError, "Unexpected error when dialling: " & err.msg, err) diff --git a/libp2p/protocols/connectivity/autonat/server.nim b/libp2p/protocols/connectivity/autonat/server.nim index 1b35ed0dfe..f71ec6e2bd 100644 --- a/libp2p/protocols/connectivity/autonat/server.nim +++ b/libp2p/protocols/connectivity/autonat/server.nim @@ -40,7 +40,7 @@ proc sendDial(conn: Connection, pid: PeerId, addrs: seq[MultiAddress]) {.async.} proc sendResponseError( conn: Connection, status: ResponseStatus, text: string = "" -) {.async.} = +) {.async: (raises: [CancelledError]).} = let pb = AutonatDialResponse( status: status, text: @@ -50,17 +50,27 @@ proc sendResponseError( Opt.some(text), ma: Opt.none(MultiAddress), ).encode() - await conn.writeLp(pb.buffer) + try: + await conn.writeLp(pb.buffer) + except LPStreamError as exc: + trace "autonat failed to send response error", description = exc.msg, conn -proc sendResponseOk(conn: Connection, ma: MultiAddress) {.async.} = +proc sendResponseOk( + conn: Connection, ma: MultiAddress +) {.async: (raises: [CancelledError]).} = let pb = AutonatDialResponse( status: ResponseStatus.Ok, text: Opt.some("Ok"), ma: Opt.some(ma) ).encode() - await conn.writeLp(pb.buffer) + try: + await conn.writeLp(pb.buffer) + except LPStreamError as exc: + trace "autonat failed to send response ok", description = exc.msg, conn -proc tryDial(autonat: Autonat, conn: Connection, addrs: seq[MultiAddress]) {.async.} = +proc tryDial( + autonat: Autonat, conn: Connection, addrs: seq[MultiAddress] +) {.async: (raises: [DialFailedError, CancelledError]).} = await autonat.sem.acquire() - var futs: seq[Future[Opt[MultiAddress]]] + var futs: seq[Future[Opt[MultiAddress]].Raising([DialFailedError, CancelledError])] try: # This is to bypass the per peer max connections limit let outgoingConnection = @@ -71,7 +81,8 @@ proc tryDial(autonat: Autonat, conn: Connection, addrs: seq[MultiAddress]) {.asy return # Safer to always try to cancel cause we aren't sure if the connection was established defer: - outgoingConnection.cancel() + outgoingConnection.cancelSoon() + # tryDial is to bypass the global max connections limit futs = addrs.mapIt(autonat.switch.dialer.tryDial(conn.peerId, @[it])) let fut = await anyCompleted(futs).wait(autonat.dialTimeout) @@ -88,9 +99,6 @@ proc tryDial(autonat: Autonat, conn: Connection, addrs: seq[MultiAddress]) {.asy except AsyncTimeoutError as exc: debug "Dial timeout", addrs, description = exc.msg await conn.sendResponseError(DialError, "Dial timeout") - except CatchableError as exc: - debug "Unexpected error", addrs, description = exc.msg - await conn.sendResponseError(DialError, "Unexpected error") finally: autonat.sem.release() for f in futs: @@ -163,8 +171,6 @@ proc new*( await autonat.handleDial(conn, msg) except CancelledError as exc: raise exc - except CatchableError as exc: - debug "exception in autonat handler", description = exc.msg, conn finally: trace "exiting autonat handler", conn await conn.close() diff --git a/libp2p/protocols/connectivity/relay/relay.nim b/libp2p/protocols/connectivity/relay/relay.nim index fb5357b67c..dab355595a 100644 --- a/libp2p/protocols/connectivity/relay/relay.nim +++ b/libp2p/protocols/connectivity/relay/relay.nim @@ -166,7 +166,7 @@ proc handleConnect(r: Relay, connSrc: Connection, msg: HopMessage) {.async.} = await r.switch.dial(dst, RelayV2StopCodec) except CancelledError as exc: raise exc - except CatchableError as exc: + except DialFailedError as exc: trace "error opening relay stream", dst, description = exc.msg await sendHopStatus(connSrc, ConnectionFailed) return @@ -271,7 +271,7 @@ proc handleHop*(r: Relay, connSrc: Connection, msg: RelayMessage) {.async.} = await r.switch.dial(dst.peerId, RelayV1Codec) except CancelledError as exc: raise exc - except CatchableError as exc: + except DialFailedError as exc: trace "error opening relay stream", dst, description = exc.msg await sendStatus(connSrc, StatusV1.HopCantDialDst) return diff --git a/libp2p/protocols/pubsub/gossipsub.nim b/libp2p/protocols/pubsub/gossipsub.nim index af40cc48b8..dfed01c9b4 100644 --- a/libp2p/protocols/pubsub/gossipsub.nim +++ b/libp2p/protocols/pubsub/gossipsub.nim @@ -833,7 +833,7 @@ proc maintainDirectPeer( except CancelledError as exc: trace "Direct peer dial canceled" raise exc - except CatchableError as exc: + except DialFailedError as exc: debug "Direct peer error dialing", description = exc.msg proc addDirectPeer*( diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index e4a03b3c26..7cc99064ab 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -366,7 +366,7 @@ method getOrCreatePeer*( return await p.switch.dial(peerId, protosToDial) except CancelledError as exc: raise exc - except CatchableError as e: + except DialFailedError as e: raise (ref GetConnDialError)(parent: e) proc onEvent(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe.} = diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 84da1c2a28..f043763178 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -139,14 +139,16 @@ method connect*( forceDial = false, reuseConnection = true, dir = Direction.Out, -): Future[void] {.public.} = +): Future[void] {. + public, async: (raises: [DialFailedError, CancelledError], raw: true) +.} = ## Connects to a peer without opening a stream to it s.dialer.connect(peerId, addrs, forceDial, reuseConnection, dir) method connect*( s: Switch, address: MultiAddress, allowUnknownPeerId = false -): Future[PeerId] = +): Future[PeerId] {.async: (raises: [DialFailedError, CancelledError], raw: true).} = ## Connects to a peer and retrieve its PeerId ## ## If the P2P part is missing from the MA and `allowUnknownPeerId` is set @@ -157,12 +159,18 @@ method connect*( method dial*( s: Switch, peerId: PeerId, protos: seq[string] -): Future[Connection] {.public.} = +): Future[Connection] {. + public, async: (raises: [DialFailedError, CancelledError], raw: true) +.} = ## Open a stream to a connected peer with the specified `protos` s.dialer.dial(peerId, protos) -proc dial*(s: Switch, peerId: PeerId, proto: string): Future[Connection] {.public.} = +proc dial*( + s: Switch, peerId: PeerId, proto: string +): Future[Connection] {. + public, async: (raises: [DialFailedError, CancelledError], raw: true) +.} = ## Open a stream to a connected peer with the specified `proto` dial(s, peerId, @[proto]) @@ -173,7 +181,9 @@ method dial*( addrs: seq[MultiAddress], protos: seq[string], forceDial = false, -): Future[Connection] {.public.} = +): Future[Connection] {. + public, async: (raises: [DialFailedError, CancelledError], raw: true) +.} = ## Connected to a peer and open a stream ## with the specified `protos` @@ -181,7 +191,9 @@ method dial*( proc dial*( s: Switch, peerId: PeerId, addrs: seq[MultiAddress], proto: string -): Future[Connection] {.public.} = +): Future[Connection] {. + public, async: (raises: [DialFailedError, CancelledError], raw: true) +.} = ## Connected to a peer and open a stream ## with the specified `proto` diff --git a/libp2p/utils/future.nim b/libp2p/utils/future.nim index 6d079c5ce6..87831cd00d 100644 --- a/libp2p/utils/future.nim +++ b/libp2p/utils/future.nim @@ -14,24 +14,24 @@ import chronos type AllFuturesFailedError* = object of CatchableError proc anyCompleted*[T]( - futs: seq[Future[T]] -): Future[Future[T]] {.async: (raises: [AllFuturesFailedError, CancelledError]).} = + futs: seq[T] +): Future[T] {.async: (raises: [AllFuturesFailedError, CancelledError]).} = ## Returns a future that will complete with the first future that completes. ## If all futures fail or futs is empty, the returned future will fail with AllFuturesFailedError. var requests = futs while true: - var raceFut: Future[T] - try: - raceFut = await one(requests) + var raceFut = await one(requests) if raceFut.completed: return raceFut + requests.del(requests.find(raceFut)) except ValueError: raise newException( AllFuturesFailedError, "None of the futures completed successfully" ) - - let index = requests.find(raceFut) - requests.del(index) + except CancelledError as exc: + raise exc + except CatchableError: + continue diff --git a/tests/pubsub/utils.nim b/tests/pubsub/utils.nim index 95878418eb..c05b588d42 100644 --- a/tests/pubsub/utils.nim +++ b/tests/pubsub/utils.nim @@ -34,7 +34,7 @@ proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer = return await p.switch.dial(peerId, GossipSubCodec_12) except CancelledError as exc: raise exc - except CatchableError as e: + except DialFailedError as e: raise (ref GetConnDialError)(parent: e) let pubSubPeer = PubSubPeer.new(peerId, getConn, nil, GossipSubCodec_12, 1024 * 1024) diff --git a/tests/stubs/switchstub.nim b/tests/stubs/switchstub.nim index 8c6e829420..88ae8f7d15 100644 --- a/tests/stubs/switchstub.nim +++ b/tests/stubs/switchstub.nim @@ -26,7 +26,7 @@ type forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} method connect*( self: SwitchStub, @@ -35,7 +35,7 @@ method connect*( forceDial = false, reuseConnection = true, dir = Direction.Out, -) {.async.} = +) {.async: (raises: [DialFailedError, CancelledError]).} = if (self.connectStub != nil): await self.connectStub(self, peerId, addrs, forceDial, reuseConnection, dir) else: diff --git a/tests/testdcutr.nim b/tests/testdcutr.nim index 4ec29e58de..580aa5b676 100644 --- a/tests/testdcutr.nim +++ b/tests/testdcutr.nim @@ -114,7 +114,7 @@ suite "Dcutr": forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} = + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} = await sleepAsync(100.millis) let behindNATSwitch = SwitchStub.new(newStandardSwitch(), connectTimeoutProc) @@ -136,8 +136,8 @@ suite "Dcutr": forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} = - raise newException(CatchableError, "error") + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} = + raise newException(DialFailedError, "error") let behindNATSwitch = SwitchStub.new(newStandardSwitch(), connectErrorProc) let publicSwitch = newStandardSwitch() @@ -193,7 +193,7 @@ suite "Dcutr": forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} = + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} = await sleepAsync(100.millis) await ductrServerTest(connectProc) @@ -206,8 +206,8 @@ suite "Dcutr": forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} = - raise newException(CatchableError, "error") + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} = + raise newException(DialFailedError, "error") await ductrServerTest(connectProc) diff --git a/tests/testdialer.nim b/tests/testdialer.nim index 8716158946..8cfaa7e447 100644 --- a/tests/testdialer.nim +++ b/tests/testdialer.nim @@ -9,6 +9,7 @@ import std/options import chronos +import sequtils import unittest2 import ../libp2p/[builders, switch] import ./helpers @@ -34,3 +35,25 @@ suite "Dialer": check src.connManager.connCount(dst.peerInfo.peerId) == 2 await allFutures(src.stop(), dst.stop()) + + asyncTest "Max connections reached": + var switches: seq[Switch] + + let dst = newStandardSwitch(maxConnections = 2) + await dst.start() + switches.add(dst) + + for i in 1 ..< 3: + let src = newStandardSwitch() + switches.add(src) + await src.start() + await src.connect(dst.peerInfo.peerId, dst.peerInfo.addrs, true, false) + + let src = newStandardSwitch() + switches.add(src) + await src.start() + check not await src.connect(dst.peerInfo.peerId, dst.peerInfo.addrs).withTimeout( + 1000.millis + ) + + await allFuturesThrowing(allFutures(switches.mapIt(it.stop()))) diff --git a/tests/testfuture.nim b/tests/testfuture.nim index c9d4d37873..9f9a8cd4df 100644 --- a/tests/testfuture.nim +++ b/tests/testfuture.nim @@ -60,7 +60,9 @@ suite "Future": proc fut2() {.async.} = await sleepAsync(200.milliseconds) - proc fut3() {.async.} = + proc fut3() {.async: (raises: [ValueError]).} = + # fut3 intentionally specifies raised ValueError + # so that it's type is of InternalRaisesFuture raise newException(ValueError, "fut3") var f1 = fut1() diff --git a/tests/testhpservice.nim b/tests/testhpservice.nim index 27fb4711d7..bfe5d5eecf 100644 --- a/tests/testhpservice.nim +++ b/tests/testhpservice.nim @@ -293,8 +293,8 @@ suite "Hole Punching": forceDial = false, reuseConnection = true, dir = Direction.Out, - ): Future[void] {.async.} = + ): Future[void] {.async: (raises: [DialFailedError, CancelledError]).} = self.connectStub = nil # this stub should be called only once - raise newException(CatchableError, "error") + raise newException(DialFailedError, "error") await holePunchingTest(connectStub, nil, Reachable) diff --git a/tests/testswitch.nim b/tests/testswitch.nim index eb645b14a6..5c0459b70e 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -15,6 +15,7 @@ import stew/byteutils import ../libp2p/[ errors, + dial, switch, multistream, builders, @@ -739,7 +740,7 @@ suite "Switch": 1000.millis ) - expect TooManyConnectionsError: + expect DialFailedError: await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs) switches.add(srcSwitch) @@ -792,7 +793,7 @@ suite "Switch": 1000.millis ) - expect TooManyConnectionsError: + expect DialFailedError: await srcSwitch.connect(dstSwitch.peerInfo.peerId, dstSwitch.peerInfo.addrs) switches.add(srcSwitch)