Skip to content

Commit ec43d0c

Browse files
chore: refactors to remove .closure., .gcsafe for .async. procs, and added callback compatibility to daemonapi (#1240)
1 parent 8469a75 commit ec43d0c

File tree

9 files changed

+40
-18
lines changed

9 files changed

+40
-18
lines changed

libp2p/daemon/daemonapi.nim

+18-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ type
146146

147147
PubsubTicket* = ref object
148148
topic*: string
149-
handler*: P2PPubSubCallback
149+
handler*: P2PPubSubCallback2
150150
transp*: StreamTransport
151151

152152
PubSubMessage* = object
@@ -162,8 +162,10 @@ type
162162
.}
163163
P2PPubSubCallback* = proc(
164164
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
165-
): Future[bool] {.gcsafe, async: (raises: [CatchableError]).}
166-
165+
): Future[bool] {.gcsafe, raises: [CatchableError].}
166+
P2PPubSubCallback2* = proc(
167+
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
168+
): Future[bool] {.async: (raises: [CatchableError]).}
167169
DaemonError* = object of LPError
168170
DaemonRemoteError* = object of DaemonError
169171
DaemonLocalError* = object of DaemonError
@@ -1480,7 +1482,7 @@ proc pubsubLoop(
14801482
break
14811483

14821484
proc pubsubSubscribe*(
1483-
api: DaemonAPI, topic: string, handler: P2PPubSubCallback
1485+
api: DaemonAPI, topic: string, handler: P2PPubSubCallback2
14841486
): Future[PubsubTicket] {.
14851487
async: (
14861488
raises:
@@ -1508,6 +1510,18 @@ proc pubsubSubscribe*(
15081510
await api.closeConnection(transp)
15091511
raise exc
15101512

1513+
proc pubsubSubscribe*(
1514+
api: DaemonAPI, topic: string, handler: P2PPubSubCallback
1515+
): Future[PubsubTicket] {.
1516+
async: (raises: [CatchableError]), deprecated: "Use P2PPubSubCallback2 instead"
1517+
.} =
1518+
proc wrap(
1519+
api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage
1520+
): Future[bool] {.async: (raises: [CatchableError]).} =
1521+
await handler(api, ticket, message)
1522+
1523+
await pubsubSubscribe(api, topic, wrap)
1524+
15111525
proc shortLog*(pinfo: PeerInfo): string =
15121526
## Get string representation of ``PeerInfo`` object.
15131527
result = newStringOfCap(128)

libp2p/nameresolving/dnsresolver.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ proc getDnsResponse(
6464

6565
proc datagramDataReceived(
6666
transp: DatagramTransport, raddr: TransportAddress
67-
): Future[void] {.async: (raises: []), closure.} =
67+
): Future[void] {.async: (raises: []).} =
6868
receivedDataFuture.complete()
6969

7070
let sock =

libp2p/protocols/pubsub/pubsub.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,13 @@ method getOrCreatePeer*(
359359
peer[].codec = protoNegotiated
360360
return peer[]
361361

362-
proc getConn(): Future[Connection] {.async: (raises: [GetConnDialError]).} =
362+
proc getConn(): Future[Connection] {.
363+
async: (raises: [CancelledError, GetConnDialError])
364+
.} =
363365
try:
364366
return await p.switch.dial(peerId, protosToDial)
367+
except CancelledError as exc:
368+
raise exc
365369
except CatchableError as e:
366370
raise (ref GetConnDialError)(parent: e)
367371

libp2p/protocols/pubsub/pubsubpeer.nim

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ type
8181
PubSubPeerEvent* = object
8282
kind*: PubSubPeerEventKind
8383

84-
GetConn* = proc(): Future[Connection] {.gcsafe, async: (raises: [GetConnDialError]).}
84+
GetConn* =
85+
proc(): Future[Connection] {.async: (raises: [CancelledError, GetConnDialError]).}
8586
DropConn* = proc(peer: PubSubPeer) {.gcsafe, raises: [].}
8687
# have to pass peer as it's unknown during init
8788
OnEvent* = proc(peer: PubSubPeer, event: PubSubPeerEvent) {.gcsafe, raises: [].}
@@ -123,9 +124,8 @@ type
123124
# The max number of elements allowed in the non-priority queue.
124125
disconnected: bool
125126

126-
RPCHandler* = proc(peer: PubSubPeer, data: seq[byte]): Future[void] {.
127-
gcsafe, async: (raises: [])
128-
.}
127+
RPCHandler* =
128+
proc(peer: PubSubPeer, data: seq[byte]): Future[void] {.async: (raises: []).}
129129

130130
when defined(libp2p_agents_metrics):
131131
func shortAgent*(p: PubSubPeer): string =

libp2p/transports/quictransport.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ method dial*(
197197
hostname: string,
198198
address: MultiAddress,
199199
peerId: Opt[PeerId] = Opt.none(PeerId),
200-
): Future[P2PConnection] {.async, gcsafe.} =
200+
): Future[P2PConnection] {.async.} =
201201
let connection = await dial(initTAddress(address).tryGet)
202202
return transport.wrapConnection(connection)
203203

tests/pubsub/testgossipsub.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ suite "GossipSub":
724724
var handler: TopicHandler
725725
closureScope:
726726
var peerName = $dialer.peerInfo.peerId
727-
handler = proc(topic: string, data: seq[byte]) {.async, closure.} =
727+
handler = proc(topic: string, data: seq[byte]) {.async.} =
728728
seen.mgetOrPut(peerName, 0).inc()
729729
check topic == "foobar"
730730
if not seenFut.finished() and seen.len >= runs:
@@ -772,7 +772,7 @@ suite "GossipSub":
772772
var handler: TopicHandler
773773
capture dialer, i:
774774
var peerName = $dialer.peerInfo.peerId
775-
handler = proc(topic: string, data: seq[byte]) {.async, closure.} =
775+
handler = proc(topic: string, data: seq[byte]) {.async.} =
776776
try:
777777
if peerName notin seen:
778778
seen[peerName] = 0

tests/pubsub/testgossipsub2.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ suite "GossipSub":
6565
var handler: TopicHandler
6666
closureScope:
6767
var peerName = $dialer.peerInfo.peerId
68-
handler = proc(topic: string, data: seq[byte]) {.async, closure.} =
68+
handler = proc(topic: string, data: seq[byte]) {.async.} =
6969
seen.mgetOrPut(peerName, 0).inc()
7070
info "seen up", count = seen.len
7171
check topic == "foobar"
@@ -272,7 +272,7 @@ suite "GossipSub":
272272
var handler: TopicHandler
273273
closureScope:
274274
var peerName = $dialer.peerInfo.peerId
275-
handler = proc(topic: string, data: seq[byte]) {.async, closure.} =
275+
handler = proc(topic: string, data: seq[byte]) {.async.} =
276276
seen.mgetOrPut(peerName, 0).inc()
277277
check topic == "foobar"
278278
if not seenFut.finished() and seen.len >= runs:

tests/pubsub/utils.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ randomize()
2727
type TestGossipSub* = ref object of GossipSub
2828

2929
proc getPubSubPeer*(p: TestGossipSub, peerId: PeerId): PubSubPeer =
30-
proc getConn(): Future[Connection] {.async: (raises: [GetConnDialError]).} =
30+
proc getConn(): Future[Connection] {.
31+
async: (raises: [CancelledError, GetConnDialError])
32+
.} =
3133
try:
3234
return await p.switch.dial(peerId, GossipSubCodec_12)
35+
except CancelledError as exc:
36+
raise exc
3337
except CatchableError as e:
3438
raise (ref GetConnDialError)(parent: e)
3539

tests/testping.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ suite "Ping":
4545
transport1 = TcpTransport.new(upgrade = Upgrade())
4646
transport2 = TcpTransport.new(upgrade = Upgrade())
4747

48-
proc handlePing(peer: PeerId) {.async, closure.} =
48+
proc handlePing(peer: PeerId) {.async.} =
4949
inc pingReceivedCount
5050

5151
pingProto1 = Ping.new()
@@ -96,7 +96,7 @@ suite "Ping":
9696
asyncTest "bad ping data ack":
9797
type FakePing = ref object of LPProtocol
9898
let fakePingProto = FakePing()
99-
proc fakeHandle(conn: Connection, proto: string) {.async, closure.} =
99+
proc fakeHandle(conn: Connection, proto: string) {.async.} =
100100
var
101101
buf: array[32, byte]
102102
fakebuf: array[32, byte]

0 commit comments

Comments
 (0)