From fd156e9cf85eb445f88b0cddd421f85cfcb2026c Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 6 Dec 2023 08:45:32 +0100 Subject: [PATCH] Adds check that block size is a multiple of cell size --- codex/slotbuilder/slotbuilder.nim | 5 ++++- tests/codex/slotbuilder/testslotbuilder.nim | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/codex/slotbuilder/slotbuilder.nim b/codex/slotbuilder/slotbuilder.nim index cc912b1b..12be8267 100644 --- a/codex/slotbuilder/slotbuilder.nim +++ b/codex/slotbuilder/slotbuilder.nim @@ -30,7 +30,10 @@ proc new*( 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.") + return failure("Number of blocks must be divisable by number of slots.") + + if (manifest.blockSize.int mod CellSize) != 0: + return failure("Block size must be divisable by cell size.") let numberOfSlotBlocks = manifest.blocksCount div manifest.ecK success(SlotBuilder( diff --git a/tests/codex/slotbuilder/testslotbuilder.nim b/tests/codex/slotbuilder/testslotbuilder.nim index 45018a84..8319517f 100644 --- a/tests/codex/slotbuilder/testslotbuilder.nim +++ b/tests/codex/slotbuilder/testslotbuilder.nim @@ -96,6 +96,21 @@ asyncchecksuite "Slot builder": check: SlotBuilder.new(localStore, mismatchManifest).isErr + test "Block size must be divisable by cell size": + let mismatchManifest = Manifest.new( + manifest = Manifest.new( + treeCid = Cid.example, + blockSize = (blockSize - 1).NBytes, + datasetSize = (datasetSize - numberOfDatasetBlocks).NBytes), + treeCid = Cid.example, + datasetSize = (datasetSize - numberOfDatasetBlocks).NBytes, + ecK = numberOfSlots, + ecM = 0 + ) + + check: + SlotBuilder.new(localStore, mismatchManifest).isErr + proc getNextPowerOfTwo(i: int): int = if i < 1: return 1 @@ -178,5 +193,3 @@ asyncchecksuite "Slot builder": # 1 pad block slotTree.getLeafCid(numberOfSlotBlocks).tryget() == expectedEmptyCid - -