2024-01-08 22:52:46 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/math
|
|
|
|
import std/importutils
|
|
|
|
import std/sugar
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/rng
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/chunker
|
|
|
|
import pkg/codex/merkletree
|
2024-01-11 16:45:23 +00:00
|
|
|
import pkg/codex/manifest {.all.}
|
2024-01-08 22:52:46 +00:00
|
|
|
import pkg/codex/utils
|
|
|
|
import pkg/codex/utils/digest
|
|
|
|
import pkg/datastore
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/poseidon2/io
|
|
|
|
import constantine/math/io/io_fields
|
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
import ../../asynctest
|
2024-01-08 22:52:46 +00:00
|
|
|
import ../helpers
|
|
|
|
import ../examples
|
|
|
|
import ../merkletree/helpers
|
|
|
|
|
|
|
|
import pkg/codex/indexingstrategy {.all.}
|
2024-01-11 16:45:23 +00:00
|
|
|
import pkg/codex/slots {.all.}
|
|
|
|
|
|
|
|
privateAccess(SlotsBuilder) # enable access to private fields
|
|
|
|
privateAccess(Manifest) # enable access to private fields
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
suite "Slot builder":
|
|
|
|
let
|
2024-01-17 19:24:34 +00:00
|
|
|
blockSize = NBytes 1024
|
|
|
|
cellSize = NBytes 64
|
2024-01-08 22:52:46 +00:00
|
|
|
ecK = 3
|
|
|
|
ecM = 2
|
|
|
|
|
|
|
|
numSlots = ecK + ecM
|
|
|
|
numDatasetBlocks = 100
|
2024-01-17 19:24:34 +00:00
|
|
|
numBlockCells = (blockSize div cellSize).int
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
numTotalBlocks = calcEcBlocksCount(numDatasetBlocks, ecK, ecM) # total number of blocks in the dataset after
|
|
|
|
# EC (should will match number of slots)
|
2024-01-17 19:24:34 +00:00
|
|
|
originalDatasetSize = numDatasetBlocks * blockSize.int # size of the dataset before EC
|
|
|
|
totalDatasetSize = numTotalBlocks * blockSize.int # size of the dataset after EC
|
2024-01-08 22:52:46 +00:00
|
|
|
numTotalSlotBlocks = nextPowerOfTwo(numTotalBlocks div numSlots)
|
|
|
|
|
|
|
|
blockPadBytes =
|
2024-01-17 19:24:34 +00:00
|
|
|
newSeq[byte](numBlockCells.nextPowerOfTwoPad * cellSize.int) # power of two padding for blocks
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
slotsPadLeafs =
|
|
|
|
newSeqWith((numTotalBlocks div numSlots).nextPowerOfTwoPad, Poseidon2Zero) # power of two padding for block roots
|
|
|
|
|
|
|
|
rootsPadLeafs =
|
|
|
|
newSeqWith(numSlots.nextPowerOfTwoPad, Poseidon2Zero)
|
|
|
|
|
|
|
|
var
|
|
|
|
datasetBlocks: seq[bt.Block]
|
|
|
|
localStore: BlockStore
|
|
|
|
manifest: Manifest
|
|
|
|
protectedManifest: Manifest
|
|
|
|
expectedEmptyCid: Cid
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder: SlotsBuilder
|
2024-01-08 22:52:46 +00:00
|
|
|
chunker: Chunker
|
|
|
|
|
|
|
|
proc createBlocks(): Future[void] {.async.} =
|
|
|
|
while true:
|
|
|
|
let chunk = await chunker.getBytes()
|
|
|
|
if chunk.len <= 0:
|
|
|
|
break
|
|
|
|
let blk = bt.Block.new(chunk).tryGet()
|
|
|
|
datasetBlocks.add(blk)
|
|
|
|
discard await localStore.putBlock(blk)
|
|
|
|
|
|
|
|
proc createProtectedManifest(): Future[void] {.async.} =
|
|
|
|
let
|
|
|
|
cids = datasetBlocks.mapIt(it.cid)
|
|
|
|
datasetTree = CodexTree.init(cids[0..<numDatasetBlocks]).tryGet()
|
|
|
|
datasetTreeCid = datasetTree.rootCid().tryGet()
|
|
|
|
|
|
|
|
protectedTree = CodexTree.init(cids).tryGet()
|
|
|
|
protectedTreeCid = protectedTree.rootCid().tryGet()
|
|
|
|
|
|
|
|
for index, cid in cids[0..<numDatasetBlocks]:
|
|
|
|
let proof = datasetTree.getProof(index).tryget()
|
|
|
|
(await localStore.putCidAndProof(datasetTreeCid, index, cid, proof)).tryGet
|
|
|
|
|
|
|
|
for index, cid in cids:
|
|
|
|
let proof = protectedTree.getProof(index).tryget()
|
|
|
|
(await localStore.putCidAndProof(protectedTreeCid, index, cid, proof)).tryGet
|
|
|
|
|
|
|
|
manifest = Manifest.new(
|
|
|
|
treeCid = datasetTreeCid,
|
|
|
|
blockSize = blockSize.NBytes,
|
|
|
|
datasetSize = originalDatasetSize.NBytes)
|
|
|
|
|
|
|
|
protectedManifest = Manifest.new(
|
|
|
|
manifest = manifest,
|
|
|
|
treeCid = protectedTreeCid,
|
|
|
|
datasetSize = totalDatasetSize.NBytes,
|
|
|
|
ecK = ecK,
|
|
|
|
ecM = ecM)
|
|
|
|
|
|
|
|
let
|
|
|
|
manifestBlock = bt.Block.new(
|
|
|
|
manifest.encode().tryGet(),
|
|
|
|
codec = ManifestCodec).tryGet()
|
|
|
|
|
|
|
|
protectedManifestBlock = bt.Block.new(
|
|
|
|
protectedManifest.encode().tryGet(),
|
|
|
|
codec = ManifestCodec).tryGet()
|
|
|
|
|
|
|
|
(await localStore.putBlock(manifestBlock)).tryGet()
|
|
|
|
(await localStore.putBlock(protectedManifestBlock)).tryGet()
|
|
|
|
|
|
|
|
expectedEmptyCid = emptyCid(
|
|
|
|
protectedManifest.version,
|
|
|
|
protectedManifest.hcodec,
|
|
|
|
protectedManifest.codec).tryGet()
|
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
|
|
|
repoDs = SQLiteDatastore.new(Memory).tryGet()
|
|
|
|
metaDs = SQLiteDatastore.new(Memory).tryGet()
|
|
|
|
localStore = RepoStore.new(repoDs, metaDs)
|
|
|
|
|
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = totalDatasetSize, chunkSize = blockSize)
|
|
|
|
await createBlocks()
|
|
|
|
await createProtectedManifest()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
await localStore.close()
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
# TODO: THIS IS A BUG IN asynctest, because it doesn't release the
|
|
|
|
# objects after the test is done, so we need to do it manually
|
|
|
|
#
|
2024-01-08 22:52:46 +00:00
|
|
|
# Need to reset all objects because otherwise they get
|
|
|
|
# captured by the test runner closures, not good!
|
|
|
|
reset(datasetBlocks)
|
|
|
|
reset(localStore)
|
|
|
|
reset(manifest)
|
|
|
|
reset(protectedManifest)
|
|
|
|
reset(expectedEmptyCid)
|
|
|
|
reset(slotBuilder)
|
|
|
|
reset(chunker)
|
|
|
|
|
|
|
|
test "Can only create slotBuilder with protected manifest":
|
|
|
|
let
|
|
|
|
unprotectedManifest = Manifest.new(
|
|
|
|
treeCid = Cid.example,
|
|
|
|
blockSize = blockSize.NBytes,
|
|
|
|
datasetSize = originalDatasetSize.NBytes)
|
|
|
|
|
|
|
|
check:
|
2024-01-11 16:45:23 +00:00
|
|
|
SlotsBuilder.new(localStore, unprotectedManifest, cellSize = cellSize)
|
|
|
|
.error.msg == "Can only create SlotsBuilder using protected manifests."
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
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 = originalDatasetSize.NBytes),
|
|
|
|
treeCid = Cid.example,
|
|
|
|
datasetSize = totalDatasetSize.NBytes,
|
|
|
|
ecK = ecK - 1,
|
|
|
|
ecM = ecM)
|
|
|
|
|
|
|
|
check:
|
2024-01-11 16:45:23 +00:00
|
|
|
SlotsBuilder.new(localStore, mismatchManifest, cellSize = cellSize)
|
2024-01-08 22:52:46 +00:00
|
|
|
.error.msg == "Number of blocks must be divisable by number of slots."
|
|
|
|
|
|
|
|
test "Block size must be divisable by cell size":
|
|
|
|
let
|
|
|
|
mismatchManifest = Manifest.new(
|
|
|
|
manifest = Manifest.new(
|
|
|
|
treeCid = Cid.example,
|
|
|
|
blockSize = (blockSize + 1).NBytes,
|
|
|
|
datasetSize = (originalDatasetSize - 1).NBytes),
|
|
|
|
treeCid = Cid.example,
|
|
|
|
datasetSize = (totalDatasetSize - 1).NBytes,
|
|
|
|
ecK = ecK,
|
|
|
|
ecM = ecM)
|
|
|
|
|
|
|
|
check:
|
2024-01-11 16:45:23 +00:00
|
|
|
SlotsBuilder.new(localStore, mismatchManifest, cellSize = cellSize)
|
2024-01-08 22:52:46 +00:00
|
|
|
.error.msg == "Block size must be divisable by cell size."
|
|
|
|
|
|
|
|
test "Should build correct slot builder":
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
slotBuilder.numBlockPadBytes == blockPadBytes.len
|
|
|
|
slotBuilder.numSlotsPadLeafs == slotsPadLeafs.len
|
|
|
|
slotBuilder.numRootsPadLeafs == rootsPadLeafs.len
|
|
|
|
|
|
|
|
test "Should build slot hashes for all slots":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
steppedStrategy = SteppedStrategy.init(0, numTotalBlocks - 1, numSlots)
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
for i in 0 ..< numSlots:
|
|
|
|
let
|
|
|
|
expectedBlock = steppedStrategy
|
|
|
|
.getIndicies(i)
|
|
|
|
.mapIt( datasetBlocks[it] )
|
|
|
|
|
|
|
|
expectedHashes: seq[Poseidon2Hash] = collect(newSeq):
|
|
|
|
for blk in expectedBlock:
|
2024-01-17 19:24:34 +00:00
|
|
|
SpongeMerkle.digest(blk.data & blockPadBytes, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-25 23:34:38 +00:00
|
|
|
cellHashes = (await slotBuilder.getBlockHashes(i)).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
expectedHashes == cellHashes
|
|
|
|
|
|
|
|
test "Should build slot trees for all slots":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
steppedStrategy = SteppedStrategy.init(0, numTotalBlocks - 1, numSlots)
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
for i in 0 ..< numSlots:
|
|
|
|
let
|
|
|
|
expectedBlock = steppedStrategy
|
|
|
|
.getIndicies(i)
|
|
|
|
.mapIt( datasetBlocks[it] )
|
|
|
|
|
|
|
|
expectedHashes: seq[Poseidon2Hash] = collect(newSeq):
|
|
|
|
for blk in expectedBlock:
|
2024-01-17 19:24:34 +00:00
|
|
|
SpongeMerkle.digest(blk.data & blockPadBytes, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
expectedRoot = Merkle.digest(expectedHashes & slotsPadLeafs)
|
|
|
|
|
|
|
|
slotTree = (await slotBuilder.buildSlotTree(i)).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
expectedRoot == slotTree.root().tryGet()
|
|
|
|
|
|
|
|
test "Should persist trees for all slots":
|
|
|
|
let
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
for i in 0 ..< numSlots:
|
|
|
|
let
|
|
|
|
slotTree = (await slotBuilder.buildSlotTree(i)).tryGet()
|
|
|
|
slotRoot = (await slotBuilder.buildSlot(i)).tryGet()
|
|
|
|
slotCid = slotRoot.toSlotCid().tryGet()
|
|
|
|
|
|
|
|
for cellIndex in 0..<numTotalSlotBlocks:
|
|
|
|
let
|
|
|
|
(cellCid, proof) = (await localStore.getCidAndProof(slotCid, cellIndex)).tryGet()
|
|
|
|
verifiableProof = proof.toVerifiableProof().tryGet()
|
|
|
|
posProof = slotTree.getProof(cellIndex).tryGet
|
|
|
|
|
|
|
|
check:
|
|
|
|
verifiableProof.index == posProof.index
|
|
|
|
verifiableProof.nleaves == posProof.nleaves
|
|
|
|
verifiableProof.path == posProof.path
|
|
|
|
|
|
|
|
test "Should build correct verification root":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
steppedStrategy = SteppedStrategy.init(0, numTotalBlocks - 1, numSlots)
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
(await slotBuilder.buildSlots()).tryGet
|
|
|
|
let
|
2024-01-08 22:52:46 +00:00
|
|
|
slotsHashes = collect(newSeq):
|
|
|
|
for i in 0 ..< numSlots:
|
|
|
|
let
|
|
|
|
expectedBlocks = steppedStrategy
|
|
|
|
.getIndicies(i)
|
|
|
|
.mapIt( datasetBlocks[it] )
|
|
|
|
|
|
|
|
slotHashes: seq[Poseidon2Hash] = collect(newSeq):
|
|
|
|
for blk in expectedBlocks:
|
2024-01-17 19:24:34 +00:00
|
|
|
SpongeMerkle.digest(blk.data & blockPadBytes, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
Merkle.digest(slotHashes & slotsPadLeafs)
|
|
|
|
|
|
|
|
expectedRoot = Merkle.digest(slotsHashes & rootsPadLeafs)
|
2024-01-17 19:24:34 +00:00
|
|
|
rootHash = slotBuilder.buildVerifyTree(slotBuilder.slotRoots).tryGet().root.tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
expectedRoot == rootHash
|
|
|
|
|
|
|
|
test "Should build correct verification root manifest":
|
|
|
|
let
|
2024-02-07 19:13:27 +00:00
|
|
|
steppedStrategy = SteppedStrategy.init(0, numTotalBlocks - 1, numSlots)
|
2024-01-11 16:45:23 +00:00
|
|
|
slotBuilder = SlotsBuilder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
slotsHashes = collect(newSeq):
|
|
|
|
for i in 0 ..< numSlots:
|
|
|
|
let
|
|
|
|
expectedBlocks = steppedStrategy
|
|
|
|
.getIndicies(i)
|
|
|
|
.mapIt( datasetBlocks[it] )
|
|
|
|
|
|
|
|
slotHashes: seq[Poseidon2Hash] = collect(newSeq):
|
|
|
|
for blk in expectedBlocks:
|
2024-01-17 19:24:34 +00:00
|
|
|
SpongeMerkle.digest(blk.data & blockPadBytes, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
Merkle.digest(slotHashes & slotsPadLeafs)
|
|
|
|
|
|
|
|
expectedRoot = Merkle.digest(slotsHashes & rootsPadLeafs)
|
|
|
|
manifest = (await slotBuilder.buildManifest()).tryGet()
|
2024-01-11 16:45:23 +00:00
|
|
|
mhash = manifest.verifyRoot.mhash.tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
mhashBytes = mhash.digestBytes
|
2024-01-11 16:45:23 +00:00
|
|
|
rootHash = Poseidon2Hash.fromBytes(mhashBytes.toArray32).get
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
expectedRoot == rootHash
|
2024-01-11 16:45:23 +00:00
|
|
|
|
|
|
|
test "Should not build from verifiable manifest with 0 slots":
|
|
|
|
var
|
|
|
|
slotBuilder = SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
verifyManifest = (await slotBuilder.buildManifest()).tryGet()
|
|
|
|
|
|
|
|
verifyManifest.slotRoots = @[]
|
|
|
|
check SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).isErr
|
|
|
|
|
|
|
|
test "Should not build from verifiable manifest with incorrect number of slots":
|
|
|
|
var
|
|
|
|
slotBuilder = SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
verifyManifest = (await slotBuilder.buildManifest()).tryGet()
|
|
|
|
|
|
|
|
verifyManifest.slotRoots.del(
|
|
|
|
verifyManifest.slotRoots.len - 1
|
|
|
|
)
|
|
|
|
|
|
|
|
check SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).isErr
|
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
test "Should not build from verifiable manifest with invalid verify root":
|
2024-01-11 16:45:23 +00:00
|
|
|
let
|
|
|
|
slotBuilder = SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
verifyManifest = (await slotBuilder.buildManifest()).tryGet()
|
|
|
|
offset = verifyManifest.verifyRoot.data.buffer.len div 2
|
|
|
|
|
|
|
|
rng.shuffle(
|
|
|
|
Rng.instance,
|
|
|
|
verifyManifest.verifyRoot.data.buffer)
|
|
|
|
|
|
|
|
check SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).isErr
|
|
|
|
|
|
|
|
test "Should build from verifiable manifest":
|
|
|
|
let
|
|
|
|
slotBuilder = SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
verifyManifest = (await slotBuilder.buildManifest()).tryGet()
|
|
|
|
|
|
|
|
verificationBuilder = SlotsBuilder.new(
|
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
check:
|
|
|
|
slotBuilder.slotRoots == verificationBuilder.slotRoots
|
|
|
|
slotBuilder.verifyRoot == verificationBuilder.verifyRoot
|