don't pass entire blocks to list blocks calback

This commit is contained in:
Dmitriy Ryajov 2022-04-19 20:44:25 -06:00
parent 92662dea62
commit 104d5d095e
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
6 changed files with 9 additions and 12 deletions

View File

@ -105,9 +105,9 @@ proc discoveryLoopRunner(b: BlockExcEngine) {.async.} =
await sleepAsync(30.seconds) await sleepAsync(30.seconds)
proc advertiseLoopRunner*(b: BlockExcEngine) {.async.} = proc advertiseLoopRunner*(b: BlockExcEngine) {.async.} =
proc onBlock(blk: bt.Block) {.async.} = proc onBlock(cid: Cid) {.async.} =
try: try:
await b.advertiseQueue.put(blk.cid) await b.advertiseQueue.put(cid)
except CatchableError as exc: except CatchableError as exc:
trace "Exception listing blocks", exc = exc.msg trace "Exception listing blocks", exc = exc.msg

View File

@ -20,7 +20,7 @@ import ../blocktype
export blocktype, libp2p export blocktype, libp2p
type type
OnBlock* = proc(blk: Block): Future[void] {.upraises: [], gcsafe.} OnBlock* = proc(cid: Cid): Future[void] {.upraises: [], gcsafe.}
BlockStore* = ref object of RootObj BlockStore* = ref object of RootObj
method getBlock*( method getBlock*(

View File

@ -70,10 +70,7 @@ method hasBlock*(self: CacheStore, cid: Cid): bool =
method listBlocks*(s: CacheStore, onBlock: OnBlock) {.async.} = method listBlocks*(s: CacheStore, onBlock: OnBlock) {.async.} =
for cid in toSeq(s.cache.keys): for cid in toSeq(s.cache.keys):
without blk =? (await s.getBlock(cid)): await onBlock(cid)
trace "Couldn't get block", cid = $cid
await onBlock(blk)
func putBlockSync(self: CacheStore, blk: Block): bool = func putBlockSync(self: CacheStore, blk: Block): bool =

View File

@ -143,7 +143,7 @@ method listBlocks*(self: FSStore, onBlock: OnBlock) {.async.} =
# getting a weird `Error: unhandled exception: index 1 not in 0 .. 0 [IndexError]` # getting a weird `Error: unhandled exception: index 1 not in 0 .. 0 [IndexError]`
# compilation error if using different syntax/construct bellow # compilation error if using different syntax/construct bellow
try: try:
await onBlock((await self.getBlock(cid.tryGet())).tryGet()) await onBlock(cid.get())
except CatchableError as exc: except CatchableError as exc:
trace "Couldn't get block", cid = $(cid.get()) trace "Couldn't get block", cid = $(cid.get())

View File

@ -112,8 +112,8 @@ suite "Cache Store tests":
var listed = false var listed = false
await store.listBlocks( await store.listBlocks(
proc(blk: Block) {.gcsafe, async.} = proc(cid: Cid) {.gcsafe, async.} =
check blk.cid in store check cid in store
listed = true listed = true
) )

View File

@ -57,8 +57,8 @@ suite "FS Store":
writeFile(store.blockPath(newBlock.cid), newBlock.data) writeFile(store.blockPath(newBlock.cid), newBlock.data)
await store.listBlocks( await store.listBlocks(
proc(blk: Block) {.gcsafe, async.} = proc(cid: Cid) {.gcsafe, async.} =
check blk.cid == newBlock.cid) check cid == newBlock.cid)
test "fail hasBlock": test "fail hasBlock":
check not store.hasBlock(newBlock.cid) check not store.hasBlock(newBlock.cid)