Implements getting treeCID from slot

This commit is contained in:
benbierens 2023-11-22 12:43:49 +01:00 committed by Dmitriy Ryajov
parent 7a47fb6e5d
commit f767d2b4d0
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 45 additions and 2 deletions

View File

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

View File

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