diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index e1278befa9..e54800083b 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -146,7 +146,7 @@ type PubsubTicket* = ref object topic*: string - handler*: P2PPubSubCallback + handler*: P2PPubSubCallback2 transp*: StreamTransport PubSubMessage* = object @@ -162,8 +162,10 @@ type .} P2PPubSubCallback* = proc( api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage - ): Future[bool] {.gcsafe, async: (raises: [CatchableError]).} - + ): Future[bool] {.gcsafe, raises: [CatchableError].} + P2PPubSubCallback2* = proc( + api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage + ): Future[bool] {.async: (raises: [CatchableError]).} DaemonError* = object of LPError DaemonRemoteError* = object of DaemonError DaemonLocalError* = object of DaemonError @@ -1480,7 +1482,7 @@ proc pubsubLoop( break proc pubsubSubscribe*( - api: DaemonAPI, topic: string, handler: P2PPubSubCallback + api: DaemonAPI, topic: string, handler: P2PPubSubCallback2 ): Future[PubsubTicket] {. async: ( raises: @@ -1508,6 +1510,18 @@ proc pubsubSubscribe*( await api.closeConnection(transp) raise exc +proc pubsubSubscribe*( + api: DaemonAPI, topic: string, handler: P2PPubSubCallback +): Future[PubsubTicket] {. + async: (raises: [CatchableError]), deprecated: "Use P2PPubSubCallback2 instead" +.} = + proc wrap( + api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage + ): Future[bool] {.async: (raises: [CatchableError]).} = + await handler(api, ticket, message) + + await pubsubSubscribe(api, topic, wrap) + proc shortLog*(pinfo: PeerInfo): string = ## Get string representation of ``PeerInfo`` object. result = newStringOfCap(128) diff --git a/libp2p/nameresolving/dnsresolver.nim b/libp2p/nameresolving/dnsresolver.nim index 699ff1f526..e9a72f4984 100644 --- a/libp2p/nameresolving/dnsresolver.nim +++ b/libp2p/nameresolving/dnsresolver.nim @@ -64,7 +64,7 @@ proc getDnsResponse( proc datagramDataReceived( transp: DatagramTransport, raddr: TransportAddress - ): Future[void] {.async: (raises: []), closure.} = + ): Future[void] {.async: (raises: []).} = receivedDataFuture.complete() let sock = diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index eff9b5c2e7..e4a03b3c26 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -359,9 +359,13 @@ method getOrCreatePeer*( peer[].codec = protoNegotiated return peer[] - proc getConn(): Future[Connection] {.async: (raises: [GetConnDialError]).} = + proc getConn(): Future[Connection] {. + async: (raises: [CancelledError, GetConnDialError]) + .} = try: return await p.switch.dial(peerId, protosToDial) + except CancelledError as exc: + raise exc except CatchableError as e: raise (ref GetConnDialError)(parent: e) diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index a05bd5838e..c3958542bb 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -81,7 +81,8 @@ type PubSubPeerEvent* = object kind*: PubSubPeerEventKind - GetConn* = proc(): Future[Connection] {.gcsafe, async: (raises: [GetConnDialError]).} + GetConn* = + proc(): Future[Connection] {.async: (raises: [CancelledError, GetConnDialError]).} DropConn* = proc(peer: PubSubPeer) {.gcsafe, raises: [].} # have to pass peer as it's unknown during init OnEvent* = proc(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe, raises: [].} @@ -123,9 +124,8 @@ type # The max number of elements allowed in the non-priority queue. disconnected: bool - RPCHandler* = proc(peer: PubSubPeer, data: seq[byte]): Future[void] {. - gcsafe, async: (raises: []) - .} + RPCHandler* = + proc(peer: PubSubPeer, data: seq[byte]): Future[void] {.async: (raises: []).} when defined(libp2p_agents_metrics): func shortAgent*(p: PubSubPeer): string = diff --git a/libp2p/transports/quictransport.nim b/libp2p/transports/quictransport.nim index f2515f82c0..6a07aa6dc3 100644 --- a/libp2p/transports/quictransport.nim +++ b/libp2p/transports/quictransport.nim @@ -197,7 +197,7 @@ method dial*( hostname: string, address: MultiAddress, peerId: Opt[PeerId] = Opt.none(PeerId), -): Future[P2PConnection] {.async, gcsafe.} = +): Future[P2PConnection] {.async.} = let connection = await dial(initTAddress(address).tryGet) return transport.wrapConnection(connection) diff --git a/tests/pubsub/testgossipsub.nim b/tests/pubsub/testgossipsub.nim index 6613018c79..2d9bef79f5 100644 --- a/tests/pubsub/testgossipsub.nim +++ b/tests/pubsub/testgossipsub.nim @@ -724,7 +724,7 @@ suite "GossipSub": var handler: TopicHandler closureScope: var peerName = $dialer.peerInfo.peerId - handler = proc(topic: string, data: seq[byte]) {.async, closure.} = + handler = proc(topic: string, data: seq[byte]) {.async.} = seen.mgetOrPut(peerName, 0).inc() check topic == "foobar" if not seenFut.finished() and seen.len >= runs: @@ -772,7 +772,7 @@ suite "GossipSub": var handler: TopicHandler capture dialer, i: var peerName = $dialer.peerInfo.peerId - handler = proc(topic: string, data: seq[byte]) {.async, closure.} = + handler = proc(topic: string, data: seq[byte]) {.async.} = try: if peerName notin seen: seen[peerName] = 0 diff --git a/tests/pubsub/testgossipsub2.nim b/tests/pubsub/testgossipsub2.nim index 4878b90931..827c0a8c86 100644 --- a/tests/pubsub/testgossipsub2.nim +++ b/tests/pubsub/testgossipsub2.nim @@ -65,7 +65,7 @@ suite "GossipSub": var handler: TopicHandler closureScope: var peerName = $dialer.peerInfo.peerId - handler = proc(topic: string, data: seq[byte]) {.async, closure.} = + handler = proc(topic: string, data: seq[byte]) {.async.} = seen.mgetOrPut(peerName, 0).inc() info "seen up", count = seen.len check topic == "foobar" @@ -272,7 +272,7 @@ suite "GossipSub": var handler: TopicHandler closureScope: var peerName = $dialer.peerInfo.peerId - handler = proc(topic: string, data: seq[byte]) {.async, closure.} = + handler = proc(topic: string, data: seq[byte]) {.async.} = seen.mgetOrPut(peerName, 0).inc() check topic == "foobar" if not seenFut.finished() and seen.len >= runs: diff --git a/tests/pubsub/utils.nim b/tests/pubsub/utils.nim index be58ac064d..95878418eb 100644 --- a/tests/pubsub/utils.nim +++ b/tests/pubsub/utils.nim @@ -27,9 +27,13 @@ randomize() type TestGossipSub* = ref object of GossipSub proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer = - proc getConn(): Future[Connection] {.async: (raises: [GetConnDialError]).} = + proc getConn(): Future[Connection] {. + async: (raises: [CancelledError, GetConnDialError]) + .} = try: return await p.switch.dial(peerId, GossipSubCodec_12) + except CancelledError as exc: + raise exc except CatchableError as e: raise (ref GetConnDialError)(parent: e) diff --git a/tests/testping.nim b/tests/testping.nim index c086e264de..35c1f62ce8 100644 --- a/tests/testping.nim +++ b/tests/testping.nim @@ -45,7 +45,7 @@ suite "Ping": transport1 = TcpTransport.new(upgrade = Upgrade()) transport2 = TcpTransport.new(upgrade = Upgrade()) - proc handlePing(peer: PeerId) {.async, closure.} = + proc handlePing(peer: PeerId) {.async.} = inc pingReceivedCount pingProto1 = Ping.new() @@ -96,7 +96,7 @@ suite "Ping": asyncTest "bad ping data ack": type FakePing = ref object of LPProtocol let fakePingProto = FakePing() - proc fakeHandle(conn: Connection, proto: string) {.async, closure.} = + proc fakeHandle(conn: Connection, proto: string) {.async.} = var buf: array[32, byte] fakebuf: array[32, byte]