From d7653ea6bac0b094802815b778d1de0d8737c064 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 22 Dec 2023 17:13:38 -0600 Subject: [PATCH] expose getCidAndProof --- codex/blockexchange/engine/engine.nim | 7 ++++++- codex/stores/blockstore.nim | 15 ++++++++++++--- codex/stores/cachestore.nim | 15 +++++++++------ codex/stores/networkstore.nim | 4 ++-- codex/stores/repostore.nim | 10 +++++----- codex/stores/treehelper.nim | 2 +- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/codex/blockexchange/engine/engine.nim b/codex/blockexchange/engine/engine.nim index 23683718..649edec6 100644 --- a/codex/blockexchange/engine/engine.nim +++ b/codex/blockexchange/engine/engine.nim @@ -361,7 +361,12 @@ proc blocksDeliveryHandler*( without proof =? bd.proof: error "Proof expected for a leaf block delivery" continue - if err =? (await b.localStore.putBlockCidAndProof(bd.address.treeCid, bd.address.index, bd.blk.cid, proof)).errorOption: + if err =? (await b.localStore.putCidAndProof( + bd.address.treeCid, + bd.address.index, + bd.blk.cid, + proof)).errorOption: + error "Unable to store proof and cid for a block" continue diff --git a/codex/stores/blockstore.nim b/codex/stores/blockstore.nim index ac646d80..fbe36152 100644 --- a/codex/stores/blockstore.nim +++ b/codex/stores/blockstore.nim @@ -70,17 +70,26 @@ method putBlock*( raiseAssert("putBlock not implemented!") -method putBlockCidAndProof*( +method putCidAndProof*( self: BlockStore, treeCid: Cid, index: Natural, blockCid: Cid, proof: CodexProof ): Future[?!void] {.base.} = - ## Put a block to the blockstore + ## Put a block proof to the blockstore ## - raiseAssert("putBlockCidAndProof not implemented!") + raiseAssert("putCidAndProof not implemented!") + +method getCidAndProof*( + self: BlockStore, + treeCid: Cid, + index: Natural): Future[?!(Cid, CodexProof)] {.base.} = + ## Get a block proof from the blockstore + ## + + raiseAssert("putCidAndProof not implemented!") method ensureExpiry*( self: BlockStore, diff --git a/codex/stores/cachestore.nim b/codex/stores/cachestore.nim index 1d34c998..8fde35a1 100644 --- a/codex/stores/cachestore.nim +++ b/codex/stores/cachestore.nim @@ -65,20 +65,24 @@ method getBlock*(self: CacheStore, cid: Cid): Future[?!Block] {.async.} = trace "Error requesting block from cache", cid, error = exc.msg return failure exc -proc getCidAndProof(self: CacheStore, treeCid: Cid, index: Natural): ?!(Cid, CodexProof) = +method getCidAndProof*( + self: CacheStore, + treeCid: Cid, + index: Natural): Future[?!(Cid, CodexProof)] {.async.} = + if cidAndProof =? self.cidAndProofCache.getOption((treeCid, index)): success(cidAndProof) else: failure(newException(BlockNotFoundError, "Block not in cache: " & $BlockAddress.init(treeCid, index))) method getBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!Block] {.async.} = - without cidAndProof =? self.getCidAndProof(treeCid, index), err: + without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err: return failure(err) await self.getBlock(cidAndProof[0]) method getBlockAndProof*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!(Block, CodexProof)] {.async.} = - without cidAndProof =? self.getCidAndProof(treeCid, index), err: + without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err: return failure(err) let (cid, proof) = cidAndProof @@ -106,7 +110,7 @@ method hasBlock*(self: CacheStore, cid: Cid): Future[?!bool] {.async.} = return (cid in self.cache).success method hasBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!bool] {.async.} = - without cidAndProof =? self.getCidAndProof(treeCid, index), err: + without cidAndProof =? (await self.getCidAndProof(treeCid, index)), err: if err of BlockNotFoundError: return success(false) else: @@ -114,7 +118,6 @@ method hasBlock*(self: CacheStore, treeCid: Cid, index: Natural): Future[?!bool] await self.hasBlock(cidAndProof[0]) - func cids(self: CacheStore): (iterator: Cid {.gcsafe.}) = return iterator(): Cid = for cid in self.cache.keys: @@ -210,7 +213,7 @@ method putBlock*( discard self.putBlockSync(blk) return success() -method putBlockCidAndProof*( +method putCidAndProof*( self: CacheStore, treeCid: Cid, index: Natural, diff --git a/codex/stores/networkstore.nim b/codex/stores/networkstore.nim index af7de463..24f4249d 100644 --- a/codex/stores/networkstore.nim +++ b/codex/stores/networkstore.nim @@ -78,13 +78,13 @@ method putBlock*( await self.engine.resolveBlocks(@[blk]) return success() -method putBlockCidAndProof*( +method putCidAndProof*( self: NetworkStore, treeCid: Cid, index: Natural, blockCid: Cid, proof: CodexProof): Future[?!void] = - self.localStore.putBlockCidAndProof(treeCid, index, blockCid, proof) + self.localStore.putCidAndProof(treeCid, index, blockCid, proof) method ensureExpiry*( self: NetworkStore, diff --git a/codex/stores/repostore.nim b/codex/stores/repostore.nim index 5833dac4..5664da33 100644 --- a/codex/stores/repostore.nim +++ b/codex/stores/repostore.nim @@ -106,7 +106,7 @@ proc decodeCid(_: type (Cid, CodexProof), data: seq[byte]): ?!Cid = cid = ? Cid.init(data[sizeof(uint64)..