From f767d2b4d07f03adb3ef76d94238ba09f2cbb0bb Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 22 Nov 2023 12:43:49 +0100 Subject: [PATCH] Implements getting treeCID from slot --- codex/proof/slotblocks.nim | 16 +++++++++++++- tests/codex/proof/testslotblocks.nim | 31 +++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/codex/proof/slotblocks.nim b/codex/proof/slotblocks.nim index 9f580671..e17c3b17 100644 --- a/codex/proof/slotblocks.nim +++ b/codex/proof/slotblocks.nim @@ -2,15 +2,29 @@ import std/bitops import std/sugar import pkg/chronos +import pkg/chronicles import pkg/libp2p import pkg/questionable import pkg/questionable/results import ../contracts/requests import ../stores/blockstore +import ../manifest proc getTreeCidForSlot*(slot: Slot, blockstore: BlockStore): Future[?!Cid] {.async.} = - raiseAssert("a") + without manifestBlockCid =? Cid.init(slot.request.content.cid).mapFailure, err: + error "Unable to init CID from slot.content.cid" + return failure err + + without manifestBlock =? await blockstore.getBlock(manifestBlockCid), err: + error "Failed to fetch manifest block", cid = manifestBlockCid + return failure err + + without manifest =? Manifest.decode(manifestBlock): + error "Unable to decode manifest" + return failure("Unable to decode manifest") + + return success(manifest.treeCid) proc getSlotBlock*(slot: Slot, blockstore: BlockStore, treeCid: Cid, slotBlockIndex: int): Future[?!Block] {.async.} = raiseAssert("a") diff --git a/tests/codex/proof/testslotblocks.nim b/tests/codex/proof/testslotblocks.nim index ce079f7e..4a170304 100644 --- a/tests/codex/proof/testslotblocks.nim +++ b/tests/codex/proof/testslotblocks.nim @@ -41,6 +41,8 @@ asyncchecksuite "Test slotblocks": treeCid = Cid.example, blockSize = 1.MiBs, datasetSize = 100.MiBs) + + var manifestBlock = bt.Block.new(manifest.encode().tryGet(), codec = DagPBCodec).tryGet() slot = Slot( request: StorageRequest( @@ -67,7 +69,8 @@ asyncchecksuite "Test slotblocks": # break # slotBlocks.add(bt.Block.new(chunk).tryGet()) - # setup: + setup: + discard await localStore.putBlock(manifestBlock) # await createSlotBlocks() test "Can get tree root for slot": @@ -75,3 +78,29 @@ asyncchecksuite "Test slotblocks": check: cid == manifest.treeCid + + test "Can fail to get tree root for invalid cid": + slot.request.content.cid = "invalid" + let cid = (await getTreeCidForSlot(slot, localStore)) + + check: + cid.isErr + + test "Can fail to get tree root when manifest block not found": + let + emptyStore = CacheStore.new() + cid = (await getTreeCidForSlot(slot, emptyStore)) + + check: + cid.isErr + + test "Can fail to get tree root when manifest fails to decode": + manifestBlock.data = @[] + + let cid = (await getTreeCidForSlot(slot, localStore)) + + check: + cid.isErr + + +