78
78
CodexNodeRef* = ref CodexNode
79
79
80
80
OnManifest* = proc (cid: Cid, manifest: Manifest): void {.gcsafe, raises: [].}
81
- BatchProc* = proc (blocks: seq [bt.Block]) : Future[?! void ] {.gcsafe, raises: [].}
81
+ BatchProc* = proc (blocks: seq [bt.Block]) : Future[?! void ] {.
82
+ gcsafe, async: (raises: [CancelledError])
83
+ .}
82
84
83
85
func switch* (self: CodexNodeRef): Switch =
84
86
return self.switch
@@ -109,7 +111,9 @@ proc storeManifest*(
109
111
110
112
success blk
111
113
112
- proc fetchManifest* (self: CodexNodeRef, cid: Cid): Future[?! Manifest] {.async.} =
114
+ proc fetchManifest* (
115
+ self: CodexNodeRef, cid: Cid
116
+ ): Future[?! Manifest] {.async: (raises: [CancelledError]) .} =
113
117
# # Fetch and decode a manifest block
114
118
# #
115
119
@@ -144,7 +148,7 @@ proc connect*(
144
148
145
149
proc updateExpiry* (
146
150
self: CodexNodeRef, manifestCid: Cid, expiry: SecondsSince1970
147
- ): Future[?! void ] {.async.} =
151
+ ): Future[?! void ] {.async: (raises: [CancelledError] ) .} =
148
152
without manifest =? await self.fetchManifest(manifestCid), error:
149
153
trace " Unable to fetch manifest for cid" , manifestCid
150
154
return failure(error)
@@ -154,7 +158,7 @@ proc updateExpiry*(
154
158
self.networkStore.localStore.ensureExpiry(manifest.treeCid, it, expiry)
155
159
)
156
160
157
- let res = await allFinishedFailed(ensuringFutures)
161
+ let res = await allFinishedFailed[ ?! void ] (ensuringFutures)
158
162
if res.failure.len > 0 :
159
163
trace " Some blocks failed to update expiry" , len = res.failure.len
160
164
return failure(" Some blocks failed to update expiry (" & $ res.failure.len & " )" )
@@ -172,7 +176,7 @@ proc fetchBatched*(
172
176
batchSize = DefaultFetchBatch,
173
177
onBatch: BatchProc = nil ,
174
178
fetchLocal = true ,
175
- ): Future[?! void ] {.async, gcsafe.} =
179
+ ): Future[?! void ] {.async: (raises: [CancelledError] ) , gcsafe.} =
176
180
# # Fetch blocks in batches of `batchSize`
177
181
# #
178
182
@@ -190,7 +194,10 @@ proc fetchBatched*(
190
194
if not (await address in self.networkStore) or fetchLocal:
191
195
self.networkStore.getBlock(address)
192
196
193
- without blockResults =? await allFinishedValues(blockFutures), err:
197
+ if blockFutures.len == 0 :
198
+ continue
199
+
200
+ without blockResults =? await allFinishedValues[?! bt.Block](blockFutures), err:
194
201
trace "Some blocks failed to fetch", err = err.msg
195
202
return failure(err)
196
203
@@ -215,7 +222,7 @@ proc fetchBatched*(
215
222
batchSize = DefaultFetchBatch,
216
223
onBatch: BatchProc = nil,
217
224
fetchLocal = true,
218
- ): Future[?! void ] =
225
+ ): Future[?!void ] {.async: (raw: true, raises: [CancelledError]).} =
219
226
## Fetch manifest in batches of `batchSize`
220
227
##
221
228
@@ -240,16 +247,16 @@ proc fetchDatasetAsync*(
240
247
error "Unable to fetch blocks", err = err.msg
241
248
except CancelledError as exc:
242
249
trace "Cancelled fetching blocks", exc = exc.msg
243
- except CatchableError as exc:
244
- error " Error fetching blocks" , exc = exc.msg
245
250
246
251
proc fetchDatasetAsyncTask*(self: CodexNodeRef, manifest: Manifest) =
247
252
## Start fetching a dataset in the background.
248
253
## The task will be tracked and cleaned up on node shutdown.
249
254
##
250
255
self.trackedFutures.track(self.fetchDatasetAsync(manifest, fetchLocal = false))
251
256
252
- proc streamSingleBlock(self: CodexNodeRef, cid: Cid): Future[?! LPStream] {.async.} =
257
+ proc streamSingleBlock(
258
+ self: CodexNodeRef, cid: Cid
259
+ ): Future[?!LPStream] {.async: (raises: [CancelledError]).} =
253
260
## Streams the contents of a single block.
254
261
##
255
262
trace "Streaming single block", cid = cid
@@ -264,15 +271,17 @@ proc streamSingleBlock(self: CodexNodeRef, cid: Cid): Future[?!LPStream] {.async
264
271
defer:
265
272
await stream.pushEof()
266
273
await stream.pushData(blk.data)
267
- except CatchableError as exc:
274
+ except CancelledError as exc:
275
+ trace "Streaming block cancelled", cid, exc = exc.msg
276
+ except LPStreamError as exc:
268
277
trace "Unable to send block", cid, exc = exc.msg
269
278
270
279
self.trackedFutures.track(streamOneBlock())
271
280
LPStream(stream).success
272
281
273
282
proc streamEntireDataset(
274
283
self: CodexNodeRef, manifest: Manifest, manifestCid: Cid
275
- ): Future[?! LPStream] {.async.} =
284
+ ): Future[?!LPStream] {.async: (raises: [CancelledError]) .} =
276
285
## Streams the contents of the entire dataset described by the manifest.
277
286
##
278
287
trace "Retrieving blocks from manifest", manifestCid
@@ -294,14 +303,14 @@ proc streamEntireDataset(
294
303
295
304
jobs.add(erasureJob())
296
305
297
- jobs.add(self.fetchDatasetAsync(manifest))
306
+ jobs.add(self.fetchDatasetAsync(manifest, fetchLocal = false ))
298
307
299
308
# Monitor stream completion and cancel background jobs when done
300
309
proc monitorStream() {.async: (raises: []).} =
301
310
try :
302
311
await stream.join()
303
- except CatchableError as exc:
304
- warn " Stream failed " , exc = exc.msg
312
+ except CancelledError as exc:
313
+ warn " Stream cancelled " , exc = exc.msg
305
314
finally :
306
315
await noCancel allFutures(jobs.mapIt(it.cancelAndWait))
307
316
@@ -314,7 +323,7 @@ proc streamEntireDataset(
314
323
315
324
proc retrieve* (
316
325
self: CodexNodeRef, cid: Cid, local: bool = true
317
- ): Future[?! LPStream] {.async.} =
326
+ ): Future[?! LPStream] {.async: (raises: [CancelledError] ) .} =
318
327
# # Retrieve by Cid a single block or an entire dataset described by manifest
319
328
# #
320
329
@@ -470,11 +479,11 @@ proc store*(
470
479
return manifestBlk.cid.success
471
480
472
481
proc iterateManifests* (self: CodexNodeRef, onManifest: OnManifest) {.async.} =
473
- without cids =? await self.networkStore.listBlocks(BlockType.Manifest):
482
+ without cidsIter =? await self.networkStore.listBlocks(BlockType.Manifest):
474
483
warn " Failed to listBlocks"
475
484
return
476
485
477
- for c in cids :
486
+ for c in cidsIter :
478
487
if cid =? await c:
479
488
without blk =? await self.networkStore.getBlock(cid):
480
489
warn " Failed to get manifest block by cid" , cid
@@ -617,7 +626,7 @@ proc onStore(
617
626
slotIdx: uint64 ,
618
627
blocksCb: BlocksCb,
619
628
isRepairing: bool = false ,
620
- ): Future[?! void ] {.async.} =
629
+ ): Future[?! void ] {.async: (raises: [CancelledError] ) .} =
621
630
# # store data in local storage
622
631
# #
623
632
@@ -648,13 +657,15 @@ proc onStore(
648
657
trace " Slot index not in manifest" , slotIdx
649
658
return failure(newException(CodexError, " Slot index not in manifest" ))
650
659
651
- proc updateExpiry(blocks: seq [bt.Block]) : Future[?! void ] {.async.} =
660
+ proc updateExpiry(
661
+ blocks: seq [bt.Block]
662
+ ) : Future[?! void ] {.async: (raises: [CancelledError]) .} =
652
663
trace " Updating expiry for blocks" , blocks = blocks.len
653
664
654
665
let ensureExpiryFutures =
655
666
blocks.mapIt(self.networkStore.ensureExpiry(it.cid, expiry.toSecondsSince1970))
656
667
657
- let res = await allFinishedFailed(ensureExpiryFutures)
668
+ let res = await allFinishedFailed[ ?! void ] (ensureExpiryFutures)
658
669
if res.failure.len > 0 :
659
670
trace " Some blocks failed to update expiry" , len = res.failure.len
660
671
return failure(" Some blocks failed to update expiry (" & $ res.failure.len & " )" )
@@ -702,7 +713,7 @@ proc onStore(
702
713
703
714
proc onProve(
704
715
self: CodexNodeRef, slot: Slot, challenge: ProofChallenge
705
- ): Future[?! Groth16Proof] {.async.} =
716
+ ): Future[?! Groth16Proof] {.async: (raises: [CancelledError] ) .} =
706
717
# # Generats a proof for a given slot and challenge
707
718
# #
708
719
@@ -758,7 +769,7 @@ proc onProve(
758
769
759
770
proc onExpiryUpdate(
760
771
self: CodexNodeRef, rootCid: Cid, expiry: SecondsSince1970
761
- ): Future[?! void ] {.async.} =
772
+ ): Future[?! void ] {.async: (raises: [CancelledError] ) .} =
762
773
return await self.updateExpiry(rootCid, expiry)
763
774
764
775
proc onClear(self: CodexNodeRef, request: StorageRequest, slotIndex: uint64 ) =
@@ -781,12 +792,12 @@ proc start*(self: CodexNodeRef) {.async.} =
781
792
slot: uint64 ,
782
793
onBatch: BatchProc,
783
794
isRepairing: bool = false ,
784
- ): Future[?! void ] =
795
+ ): Future[?! void ] {.async: (raw: true , raises: [CancelledError] ) . } =
785
796
self.onStore(request, slot, onBatch, isRepairing)
786
797
787
798
hostContracts.sales.onExpiryUpdate = proc (
788
799
rootCid: Cid, expiry: SecondsSince1970
789
- ): Future[?! void ] =
800
+ ): Future[?! void ] {.async: (raw: true , raises: [CancelledError] ) . } =
790
801
self.onExpiryUpdate(rootCid, expiry)
791
802
792
803
hostContracts.sales.onClear = proc (request: StorageRequest, slotIndex: uint64 ) =
@@ -795,7 +806,7 @@ proc start*(self: CodexNodeRef) {.async.} =
795
806
796
807
hostContracts.sales.onProve = proc (
797
808
slot: Slot, challenge: ProofChallenge
798
- ): Future[?! Groth16Proof] =
809
+ ): Future[?! Groth16Proof] {.async: (raw: true , raises: [CancelledError] ) . } =
799
810
# TODO : generate proof
800
811
self.onProve(slot, challenge)
801
812
0 commit comments