Skip to content

Commit 73591d5

Browse files
committed
gets rid of ugly casts
1 parent 5a5039b commit 73591d5

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

codex/blockexchange/engine/advertiser.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ proc advertiseLocalStoreLoop(b: Advertiser) {.async: (raises: []).} =
9999
if cidsIter =? await b.localStore.listBlocks(blockType = BlockType.Torrent):
100100
trace "Advertiser begins iterating torrent blocks..."
101101
for c in cidsIter:
102-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
102+
if cid =? await c:
103103
await b.advertiseBlock(cid)
104104
trace "Advertiser iterating torrent blocks finished."
105105
if cidsIter =? await b.localStore.listBlocks(blockType = BlockType.Manifest):
106106
trace "Advertiser begins iterating blocks..."
107107
for c in cidsIter:
108-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
108+
if cid =? await c:
109109
await b.advertiseBlock(cid)
110110
trace "Advertiser iterating blocks finished."
111111

codex/node.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ proc iterateManifests*(self: CodexNodeRef, onManifest: OnManifest) {.async.} =
728728
return
729729

730730
for c in cidsIter:
731-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
731+
if cid =? await c:
732732
without blk =? await self.networkStore.getBlock(cid):
733733
warn "Failed to get manifest block by cid", cid
734734
return

codex/rest/api.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ proc retrieveInfoHash(
204204
torrentDownloader.start()
205205

206206
for blockFut in torrentDownloader.getAsyncBlockIterator():
207-
let blockRes =
208-
await cast[Future[?!(int, seq[byte])].Raising([CancelledError])](blockFut)
207+
let blockRes = await blockFut
209208
without (blockIndex, data) =? (blockRes), err:
210209
error "Error streaming blocks", err = err.msg
211210
resp.status = Http500

codex/stores/maintenance.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ proc runBlockCheck(
8383

8484
var numberReceived = 0
8585
for beFut in iter:
86-
let beRes = await cast[Future[?!BlockExpiration].Raising([CancelledError])](beFut)
87-
without be =? beRes, err:
86+
without be =? (await beFut), err:
8887
trace "Unable to obtain blockExpiration from iterator"
8988
continue
9089
inc numberReceived

tests/codex/bittorrent/testtorrentdownloader.nim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import pkg/codex/bittorrent/manifest
1414
import pkg/codex/bittorrent/torrentdownloader
1515

1616
import pkg/codex/utils/iter
17+
import pkg/codex/utils/safeasynciter
1718
import pkg/codex/logutils
1819

1920
import ../../asynctest
@@ -208,6 +209,47 @@ asyncchecksuite "Torrent Downloader":
208209
check blockIter.finished
209210
await torrentDownloader.stop()
210211

212+
test "get downloaded blocks using async iter":
213+
torrentDownloader.start()
214+
215+
let blockIter = Iter.new(0 ..< codexManifest.blocksCount)
216+
217+
for dataFut in torrentDownloader.getAsyncBlockIterator():
218+
let status = await dataFut.withTimeout(1.seconds)
219+
assert status == true
220+
let (blockIndex, data) = (await dataFut).tryGet()
221+
trace "got data", blockIndex, len = data.len
222+
let expectedBlockIndex = blockIter.next()
223+
check blockIndex == expectedBlockIndex
224+
let treeCid = codexManifest.treeCid
225+
let address = BlockAddress.init(treeCid, expectedBlockIndex)
226+
let blk = (await localStore.getBlock(address)).tryGet()
227+
check blk.data == data
228+
229+
check blockIter.finished
230+
await torrentDownloader.stop()
231+
232+
test "get downloaded blocks using async pairs iter":
233+
torrentDownloader.start()
234+
235+
let blockIter = Iter.new(0 ..< codexManifest.blocksCount)
236+
237+
for i, dataFut in torrentDownloader.getAsyncBlockIterator():
238+
let status = await dataFut.withTimeout(1.seconds)
239+
assert status == true
240+
let (blockIndex, data) = (await dataFut).tryGet()
241+
trace "got data", blockIndex, len = data.len
242+
let expectedBlockIndex = blockIter.next()
243+
check i == expectedBlockIndex
244+
check blockIndex == expectedBlockIndex
245+
let treeCid = codexManifest.treeCid
246+
let address = BlockAddress.init(treeCid, expectedBlockIndex)
247+
let blk = (await localStore.getBlock(address)).tryGet()
248+
check blk.data == data
249+
250+
check blockIter.finished
251+
await torrentDownloader.stop()
252+
211253
test "canceling download":
212254
torrentDownloader.start()
213255

tests/codex/stores/commonstoretests.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ proc commonBlockStoreTests*(
110110

111111
var count = 0
112112
for c in cidsIter:
113-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
113+
if cid =? await c:
114114
check (await store.hasBlock(cid)).tryGet()
115115
count.inc
116116

@@ -134,7 +134,7 @@ proc commonBlockStoreTests*(
134134

135135
var count = 0
136136
for c in cidsIter:
137-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
137+
if cid =? await c:
138138
check manifestBlock.cid == cid
139139
check (await store.hasBlock(cid)).tryGet()
140140
count.inc
@@ -159,7 +159,7 @@ proc commonBlockStoreTests*(
159159

160160
var count = 0
161161
for c in cidsIter:
162-
if cid =? (await cast[Future[?!Cid].Raising([CancelledError])](c)):
162+
if cid =? await c:
163163
check (await store.hasBlock(cid)).tryGet()
164164
count.inc
165165

tests/codex/stores/testrepostore.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,13 @@ asyncchecksuite "RepoStore":
293293

294294
test "Should retrieve block expiration information":
295295
proc unpack(
296-
beIter: Future[?!SafeAsyncIter[BlockExpiration]]
296+
beIter: auto
297297
): Future[seq[BlockExpiration]] {.async: (raises: [CancelledError]).} =
298298
var expirations = newSeq[BlockExpiration](0)
299-
without iter =? (
300-
await cast[Future[?!SafeAsyncIter[BlockExpiration]].Raising([CancelledError])](beIter)
301-
), err:
299+
without iter =? (await beIter), err:
302300
return expirations
303301
for beFut in toSeq(iter):
304-
if value =?
305-
(await cast[Future[?!BlockExpiration].Raising([CancelledError])](beFut)):
302+
if value =? (await beFut):
306303
expirations.add(value)
307304
return expirations
308305

0 commit comments

Comments
 (0)