From 15b86e370057baf222daa1e275b93fe5f2b37734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Fri, 24 Jan 2025 09:00:49 -0400 Subject: [PATCH 1/5] chore: remove `closure` annotation --- libp2p/nameresolving/dnsresolver.nim | 2 +- tests/pubsub/testgossipsub.nim | 4 ++-- tests/pubsub/testgossipsub2.nim | 4 ++-- tests/testping.nim | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) 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/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/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] From b5f657ef153a4925da34f919dc4bfcea46330e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Fri, 24 Jan 2025 09:02:53 -0400 Subject: [PATCH 2/5] chore: remove `gcsafe` annotation for async procs --- libp2p/daemon/daemonapi.nim | 2 +- libp2p/protocols/pubsub/pubsubpeer.nim | 4 ++-- libp2p/transports/quictransport.nim | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index e1278befa9..2212c9417c 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -162,7 +162,7 @@ type .} P2PPubSubCallback* = proc( api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage - ): Future[bool] {.gcsafe, async: (raises: [CatchableError]).} + ): Future[bool] {.async: (raises: [CatchableError]).} DaemonError* = object of LPError DaemonRemoteError* = object of DaemonError diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index a05bd5838e..52bfc0aae6 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -81,7 +81,7 @@ type PubSubPeerEvent* = object kind*: PubSubPeerEventKind - GetConn* = proc(): Future[Connection] {.gcsafe, async: (raises: [GetConnDialError]).} + GetConn* = proc(): Future[Connection] {.async: (raises: [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: [].} @@ -124,7 +124,7 @@ type disconnected: bool RPCHandler* = proc(peer: PubSubPeer, data: seq[byte]): Future[void] {. - gcsafe, async: (raises: []) + async: (raises: []) .} when defined(libp2p_agents_metrics): 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) From a978660bd9084d6a3eea02459a30a4910cd3424f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Fri, 24 Jan 2025 09:27:24 -0400 Subject: [PATCH 3/5] chore: callback compatibility for `daemonapi` --- libp2p/daemon/daemonapi.nim | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 2212c9417c..80171536fc 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] {.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,13 @@ 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) From 63c2533d52d3e2482165b326878b81e4cb1e0ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Fri, 24 Jan 2025 09:50:27 -0400 Subject: [PATCH 4/5] chore: add `Cancelled` exception to `GetConn` --- libp2p/protocols/pubsub/pubsub.nim | 4 +++- libp2p/protocols/pubsub/pubsubpeer.nim | 2 +- tests/pubsub/utils.nim | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index eff9b5c2e7..8c32e7f535 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -359,9 +359,11 @@ 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 52bfc0aae6..73b9e17591 100644 --- a/libp2p/protocols/pubsub/pubsubpeer.nim +++ b/libp2p/protocols/pubsub/pubsubpeer.nim @@ -81,7 +81,7 @@ type PubSubPeerEvent* = object kind*: PubSubPeerEventKind - GetConn* = proc(): Future[Connection] {.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: [].} diff --git a/tests/pubsub/utils.nim b/tests/pubsub/utils.nim index be58ac064d..b401e8c676 100644 --- a/tests/pubsub/utils.nim +++ b/tests/pubsub/utils.nim @@ -27,9 +27,11 @@ randomize() type TestGossipSub* = ref object of GossipSub proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer = - proc getConn(): Future[Connection] {.async: (raises: [GetConnDialError]).} = - try: + 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) From 1456bcfa3cd1bb633a116e170c2d0f8314cef861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Fri, 24 Jan 2025 17:58:39 -0400 Subject: [PATCH 5/5] fix: nph --- libp2p/daemon/daemonapi.nim | 11 ++++++++--- libp2p/protocols/pubsub/pubsub.nim | 4 +++- libp2p/protocols/pubsub/pubsubpeer.nim | 8 ++++---- tests/pubsub/utils.nim | 6 ++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 80171536fc..e54800083b 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -165,7 +165,7 @@ type ): Future[bool] {.gcsafe, raises: [CatchableError].} P2PPubSubCallback2* = proc( api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage - ): Future[bool] {.async:(raises: [CatchableError]).} + ): Future[bool] {.async: (raises: [CatchableError]).} DaemonError* = object of LPError DaemonRemoteError* = object of DaemonError DaemonLocalError* = object of DaemonError @@ -1512,9 +1512,14 @@ proc pubsubSubscribe*( 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]).} = +): 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 = diff --git a/libp2p/protocols/pubsub/pubsub.nim b/libp2p/protocols/pubsub/pubsub.nim index 8c32e7f535..e4a03b3c26 100644 --- a/libp2p/protocols/pubsub/pubsub.nim +++ b/libp2p/protocols/pubsub/pubsub.nim @@ -359,7 +359,9 @@ method getOrCreatePeer*( peer[].codec = protoNegotiated return peer[] - proc getConn(): Future[Connection] {.async: (raises: [CancelledError, GetConnDialError]).} = + proc getConn(): Future[Connection] {. + async: (raises: [CancelledError, GetConnDialError]) + .} = try: return await p.switch.dial(peerId, protosToDial) except CancelledError as exc: diff --git a/libp2p/protocols/pubsub/pubsubpeer.nim b/libp2p/protocols/pubsub/pubsubpeer.nim index 73b9e17591..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] {.async: (raises: [CancelledError, 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] {. - 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/tests/pubsub/utils.nim b/tests/pubsub/utils.nim index b401e8c676..95878418eb 100644 --- a/tests/pubsub/utils.nim +++ b/tests/pubsub/utils.nim @@ -27,8 +27,10 @@ randomize() type TestGossipSub* = ref object of GossipSub proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer = - proc getConn(): Future[Connection] {.async: (raises: [CancelledError, GetConnDialError]).} = - try: + proc getConn(): Future[Connection] {. + async: (raises: [CancelledError, GetConnDialError]) + .} = + try: return await p.switch.dial(peerId, GossipSubCodec_12) except CancelledError as exc: raise exc