expose getCidAndProof

This commit is contained in:
Dmitriy Ryajov 2023-12-22 17:13:38 -06:00
parent 632b378b86
commit d7653ea6ba
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
6 changed files with 35 additions and 18 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -106,7 +106,7 @@ proc decodeCid(_: type (Cid, CodexProof), data: seq[byte]): ?!Cid =
cid = ? Cid.init(data[sizeof(uint64)..<sizeof(uint64) + n]).mapFailure
success(cid)
method putBlockCidAndProof*(
method putCidAndProof*(
self: RepoStore,
treeCid: Cid,
index: Natural,
@ -125,11 +125,10 @@ method putBlockCidAndProof*(
await self.metaDs.put(key, value)
proc getCidAndProof(
method getCidAndProof*(
self: RepoStore,
treeCid: Cid,
index: Natural
): Future[?!(Cid, CodexProof)] {.async.} =
index: Natural): Future[?!(Cid, CodexProof)] {.async.} =
without key =? createBlockCidAndProofMetadataKey(treeCid, index), err:
return failure(err)
@ -541,7 +540,8 @@ method close*(self: RepoStore): Future[void] {.async.} =
## For some implementations this may be a no-op
##
(await self.repoDs.close()).expect("Should close datastore")
(await self.metaDs.close()).expect("Should meta datastore")
(await self.repoDs.close()).expect("Should repo datastore")
proc reserve*(self: RepoStore, bytes: uint): Future[?!void] {.async.} =
## Reserve bytes

View File

@ -36,7 +36,7 @@ proc putSomeProofs*(store: BlockStore, tree: CodexTree, iter: Iter[int]): Future
without proof =? tree.getProof(i), err:
return failure(err)
let res = await store.putBlockCidAndProof(treeCid, i, blkCid, proof)
let res = await store.putCidAndProof(treeCid, i, blkCid, proof)
if err =? res.errorOption:
return failure(err)