mirror of
https://github.com/codex-storage/nim-codex.git
synced 2025-02-08 19:13:26 +00:00
ensures blocks are devisable by number of slots
This commit is contained in:
parent
8d17fb40b4
commit
c871102d98
@ -1,4 +1,6 @@
|
||||
import pkg/libp2p
|
||||
import pkg/questionable/results
|
||||
import pkg/codex/blocktype as bt
|
||||
import ../blocktype
|
||||
import ../stores
|
||||
import ../manifest
|
||||
@ -7,16 +9,27 @@ type
|
||||
SlotBuilder* = object of RootObj
|
||||
blockStore: BlockStore
|
||||
manifest: Manifest
|
||||
numberOfSlotBlocks: int
|
||||
|
||||
proc new*(
|
||||
T: type SlotBuilder,
|
||||
blockStore: BlockStore,
|
||||
manifest: Manifest
|
||||
): SlotBuilder =
|
||||
SlotBuilder(
|
||||
blockStore: blockStore,
|
||||
manifest: manifest
|
||||
)
|
||||
): ?!SlotBuilder =
|
||||
|
||||
proc getSlotBlockCids*(self: SlotBuilder, datasetSlotIndex: uint64): seq[Cid] =
|
||||
if not manifest.protected:
|
||||
return failure("Can only create SlotBuilder using protected manifests.")
|
||||
|
||||
if (manifest.blocksCount mod manifest.ecK) != 0:
|
||||
return failure("Number of blocks must be devisable by number of slots.")
|
||||
|
||||
let numberOfSlotBlocks = manifest.blocksCount div manifest.ecK
|
||||
success(SlotBuilder(
|
||||
blockStore: blockStore,
|
||||
manifest: manifest,
|
||||
numberOfSlotBlocks: numberOfSlotBlocks
|
||||
))
|
||||
|
||||
proc getSlotBlocks*(self: SlotBuilder, datasetSlotIndex: uint64): seq[bt.Block] =
|
||||
raiseAssert("a")
|
||||
|
||||
|
@ -9,6 +9,7 @@ import pkg/codex/chunker
|
||||
import pkg/codex/merkletree
|
||||
|
||||
import ../helpers
|
||||
import ../examples
|
||||
|
||||
import codex/slotbuilder/slotbuilder
|
||||
|
||||
@ -62,7 +63,31 @@ asyncchecksuite "Slot builder":
|
||||
setup:
|
||||
await createBlocks()
|
||||
await createProtectedManifest()
|
||||
slotBuilder = SlotBuilder.new(localStore, protectedManifest)
|
||||
slotBuilder = SlotBuilder.new(localStore, protectedManifest).tryGet()
|
||||
|
||||
test "Can only create slotBuilder with protected manifest":
|
||||
let unprotectedManifest = Manifest.new(
|
||||
treeCid = Cid.example,
|
||||
blockSize = blockSize.NBytes,
|
||||
datasetSize = datasetSize.NBytes)
|
||||
|
||||
check:
|
||||
SlotBuilder.new(localStore, unprotectedManifest).isErr
|
||||
|
||||
test "Number of blocks must be devisable by number of slots":
|
||||
let mismatchManifest = Manifest.new(
|
||||
manifest = Manifest.new(
|
||||
treeCid = Cid.example,
|
||||
blockSize = blockSize.NBytes,
|
||||
datasetSize = datasetSize.NBytes),
|
||||
treeCid = Cid.example,
|
||||
datasetSize = datasetSize.NBytes,
|
||||
ecK = numberOfSlots - 1,
|
||||
ecM = 0
|
||||
)
|
||||
|
||||
check:
|
||||
SlotBuilder.new(localStore, mismatchManifest).isErr
|
||||
|
||||
for i in 0 ..< numberOfSlots:
|
||||
test "Can get the protected slot blocks given a slot index (" & $i & ")":
|
||||
@ -70,7 +95,7 @@ asyncchecksuite "Slot builder":
|
||||
selectStart = i * numberOfSlotBlocks
|
||||
selectEnd = selectStart + numberOfSlotBlocks
|
||||
expectedCids = datasetBlocks.mapIt(it.cid)[selectStart ..< selectEnd]
|
||||
cids = slotBuilder.getSlotBlockCids(i.uint64)
|
||||
blocks = slotBuilder.getSlotBlocks(i.uint64)
|
||||
|
||||
check:
|
||||
cids == expectedCids
|
||||
blocks.mapIt(it.cid) == expectedCids
|
||||
|
Loading…
x
Reference in New Issue
Block a user