7878  CodexNodeRef *  =  ref  CodexNode 
7979
8080  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+   .}
8284
8385func  switch * (self: CodexNodeRef ): Switch  = 
8486  return  self.switch
@@ -109,7 +111,9 @@ proc storeManifest*(
109111
110112  success  blk
111113
112- proc  fetchManifest * (self: CodexNodeRef , cid: Cid ): Future [?! Manifest ] {.async .} = 
114+ proc  fetchManifest * (
115+     self: CodexNodeRef , cid: Cid 
116+ ): Future [?! Manifest ] {.async : (raises: [CancelledError ]).} = 
113117  # # Fetch and decode a manifest block
114118  # #
115119
@@ -144,7 +148,7 @@ proc connect*(
144148
145149proc  updateExpiry * (
146150    self: CodexNodeRef , manifestCid: Cid , expiry: SecondsSince1970 
147- ): Future [?! void ] {.async .} = 
151+ ): Future [?! void ] {.async : (raises: [ CancelledError ]) .} = 
148152  without  manifest =?  await  self.fetchManifest (manifestCid), error:
149153    trace  " Unable to fetch manifest for cid" 
150154    return  failure (error)
@@ -154,7 +158,7 @@ proc updateExpiry*(
154158        self.networkStore.localStore.ensureExpiry (manifest.treeCid, it, expiry)
155159      )
156160
157-     let  res =  await  allFinishedFailed (ensuringFutures)
161+     let  res =  await  allFinishedFailed [ ?! void ] (ensuringFutures)
158162    if  res.failure.len >  0 :
159163      trace  " Some blocks failed to update expiry" =  res.failure.len
160164      return  failure (" Some blocks failed to update expiry (" &  $ res.failure.len &  "  )" 
@@ -172,7 +176,7 @@ proc fetchBatched*(
172176    batchSize =  DefaultFetchBatch ,
173177    onBatch: BatchProc  =  nil ,
174178    fetchLocal =  true ,
175- ): Future [?! void ] {.async , gcsafe .} = 
179+ ): Future [?! void ] {.async : (raises: [ CancelledError ]) , gcsafe .} = 
176180  # # Fetch blocks in batches of `batchSize`
177181  # #
178182
@@ -190,7 +194,10 @@ proc fetchBatched*(
190194          if  not  (await  address in  self.networkStore) or  fetchLocal:
191195            self.networkStore.getBlock (address)
192196
193-     without  blockResults =?  await  allFinishedValues (blockFutures), err:
197+     if  blockFutures.len ==  0 :
198+       continue 
199+ 
200+     without  blockResults =?  await  allFinishedValues [?! bt.Block ](blockFutures), err:
194201      trace  " Some blocks failed to fetch" =  err.msg
195202      return  failure (err)
196203
@@ -215,7 +222,7 @@ proc fetchBatched*(
215222    batchSize =  DefaultFetchBatch ,
216223    onBatch: BatchProc  =  nil ,
217224    fetchLocal =  true ,
218- ): Future [?! void ] = 
225+ ): Future [?! void ] {. async : (raw:  true , raises: [ CancelledError ]).}  = 
219226  # # Fetch manifest in batches of `batchSize`
220227  # #
221228
@@ -240,16 +247,16 @@ proc fetchDatasetAsync*(
240247      error  " Unable to fetch blocks" =  err.msg
241248  except  CancelledError  as  exc:
242249    trace  " Cancelled fetching blocks" =  exc.msg
243-   except  CatchableError  as  exc:
244-     error  " Error fetching blocks" =  exc.msg
245250
246251proc  fetchDatasetAsyncTask * (self: CodexNodeRef , manifest: Manifest ) = 
247252  # # Start fetching a dataset in the background.
248253  # # The task will be tracked and cleaned up on node shutdown.
249254  # #
250255  self.trackedFutures.track (self.fetchDatasetAsync (manifest, fetchLocal =  false ))
251256
252- proc  streamSingleBlock (self: CodexNodeRef , cid: Cid ): Future [?! LPStream ] {.async .} = 
257+ proc  streamSingleBlock (
258+     self: CodexNodeRef , cid: Cid 
259+ ): Future [?! LPStream ] {.async : (raises: [CancelledError ]).} = 
253260  # # Streams the contents of a single block.
254261  # #
255262  trace  " Streaming single block" =  cid
@@ -264,15 +271,17 @@ proc streamSingleBlock(self: CodexNodeRef, cid: Cid): Future[?!LPStream] {.async
264271      defer :
265272        await  stream.pushEof ()
266273      await  stream.pushData (blk.data)
267-     except  CatchableError  as  exc:
274+     except  CancelledError  as  exc:
275+       trace  " Streaming block cancelled" =  exc.msg
276+     except  LPStreamError  as  exc:
268277      trace  " Unable to send block" =  exc.msg
269278
270279  self.trackedFutures.track (streamOneBlock ())
271280  LPStream 
272281
273282proc  streamEntireDataset (
274283    self: CodexNodeRef , manifest: Manifest , manifestCid: Cid 
275- ): Future [?! LPStream ] {.async .} = 
284+ ): Future [?! LPStream ] {.async : (raises: [ CancelledError ]) .} = 
276285  # # Streams the contents of the entire dataset described by the manifest.
277286  # #
278287  trace  " Retrieving blocks from manifest" 
@@ -294,14 +303,14 @@ proc streamEntireDataset(
294303
295304    jobs.add (erasureJob ())
296305
297-   jobs.add (self.fetchDatasetAsync (manifest))
306+   jobs.add (self.fetchDatasetAsync (manifest, fetchLocal  =   false ))
298307
299308  #  Monitor stream completion and cancel background jobs when done
300309  proc  monitorStream () {.async : (raises: []).} = 
301310    try :
302311      await  stream.join ()
303-     except  CatchableError  as  exc:
304-       warn  " Stream failed " =  exc.msg
312+     except  CancelledError  as  exc:
313+       warn  " Stream cancelled " =  exc.msg
305314    finally :
306315      await  noCancel  allFutures (jobs.mapIt (it.cancelAndWait))
307316
@@ -314,7 +323,7 @@ proc streamEntireDataset(
314323
315324proc  retrieve * (
316325    self: CodexNodeRef , cid: Cid , local: bool  =  true 
317- ): Future [?! LPStream ] {.async .} = 
326+ ): Future [?! LPStream ] {.async : (raises: [ CancelledError ]) .} = 
318327  # # Retrieve by Cid a single block or an entire dataset described by manifest
319328  # #
320329
@@ -470,11 +479,11 @@ proc store*(
470479  return  manifestBlk.cid.success
471480
472481proc  iterateManifests * (self: CodexNodeRef , onManifest: OnManifest ) {.async .} = 
473-   without  cids  =?  await  self.networkStore.listBlocks (BlockType .Manifest ):
482+   without  cidsIter  =?  await  self.networkStore.listBlocks (BlockType .Manifest ):
474483    warn  " Failed to listBlocks" 
475484    return 
476485
477-   for  c in  cids :
486+   for  c in  cidsIter :
478487    if  cid =?  await  c:
479488      without  blk =?  await  self.networkStore.getBlock (cid):
480489        warn  " Failed to get manifest block by cid" 
@@ -617,7 +626,7 @@ proc onStore(
617626    slotIdx: uint64 ,
618627    blocksCb: BlocksCb ,
619628    isRepairing: bool  =  false ,
620- ): Future [?! void ] {.async .} = 
629+ ): Future [?! void ] {.async : (raises: [ CancelledError ]) .} = 
621630  # # store data in local storage
622631  # #
623632
@@ -648,13 +657,15 @@ proc onStore(
648657    trace  " Slot index not in manifest" 
649658    return  failure (newException (CodexError , " Slot index not in manifest" 
650659
651-   proc  updateExpiry (blocks: seq [bt.Block ]): Future [?! void ] {.async .} = 
660+   proc  updateExpiry (
661+       blocks: seq [bt.Block ]
662+   ): Future [?! void ] {.async : (raises: [CancelledError ]).} = 
652663    trace  " Updating expiry for blocks" =  blocks.len
653664
654665    let  ensureExpiryFutures = 
655666      blocks.mapIt (self.networkStore.ensureExpiry (it.cid, expiry.toSecondsSince1970))
656667
657-     let  res =  await  allFinishedFailed (ensureExpiryFutures)
668+     let  res =  await  allFinishedFailed [ ?! void ] (ensureExpiryFutures)
658669    if  res.failure.len >  0 :
659670      trace  " Some blocks failed to update expiry" =  res.failure.len
660671      return  failure (" Some blocks failed to update expiry (" &  $ res.failure.len &  "  )" 
@@ -702,7 +713,7 @@ proc onStore(
702713
703714proc  onProve (
704715    self: CodexNodeRef , slot: Slot , challenge: ProofChallenge 
705- ): Future [?! Groth16Proof ] {.async .} = 
716+ ): Future [?! Groth16Proof ] {.async : (raises: [ CancelledError ]) .} = 
706717  # # Generats a proof for a given slot and challenge
707718  # #
708719
@@ -758,7 +769,7 @@ proc onProve(
758769
759770proc  onExpiryUpdate (
760771    self: CodexNodeRef , rootCid: Cid , expiry: SecondsSince1970 
761- ): Future [?! void ] {.async .} = 
772+ ): Future [?! void ] {.async : (raises: [ CancelledError ]) .} = 
762773  return  await  self.updateExpiry (rootCid, expiry)
763774
764775proc  onClear (self: CodexNodeRef , request: StorageRequest , slotIndex: uint64 ) = 
@@ -781,12 +792,12 @@ proc start*(self: CodexNodeRef) {.async.} =
781792        slot: uint64 ,
782793        onBatch: BatchProc ,
783794        isRepairing: bool  =  false ,
784-     ): Future [?! void ] = 
795+     ): Future [?! void ] {. async : (raw:  true , raises: [ CancelledError ]).}  = 
785796      self.onStore (request, slot, onBatch, isRepairing)
786797
787798    hostContracts.sales.onExpiryUpdate =  proc (
788799        rootCid: Cid , expiry: SecondsSince1970 
789-     ): Future [?! void ] = 
800+     ): Future [?! void ] {. async : (raw:  true , raises: [ CancelledError ]).}  = 
790801      self.onExpiryUpdate (rootCid, expiry)
791802
792803    hostContracts.sales.onClear =  proc (request: StorageRequest , slotIndex: uint64 ) = 
@@ -795,7 +806,7 @@ proc start*(self: CodexNodeRef) {.async.} =
795806
796807    hostContracts.sales.onProve =  proc (
797808        slot: Slot , challenge: ProofChallenge 
798-     ): Future [?! Groth16Proof ] = 
809+     ): Future [?! Groth16Proof ] {. async : (raw:  true , raises: [ CancelledError ]).}  = 
799810      #  TODO : generate proof
800811      self.onProve (slot, challenge)
801812
0 commit comments