2024-01-08 22:52:46 +00:00
|
|
|
import std/sequtils
|
|
|
|
import std/math
|
|
|
|
import std/importutils
|
|
|
|
import std/sugar
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
import ../../asynctest
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
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
|
2024-02-08 02:27:11 +00:00
|
|
|
import pkg/codex/utils/poseidon2digest
|
2024-01-08 22:52:46 +00:00
|
|
|
import pkg/datastore
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/poseidon2/io
|
2024-02-08 02:27:11 +00:00
|
|
|
import pkg/constantine/math/io/io_fields
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
import ./helpers
|
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.}
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
privateAccess(Poseidon2Builder) # enable access to private fields
|
2024-01-11 16:45:23 +00:00
|
|
|
privateAccess(Manifest) # enable access to private fields
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
const
|
|
|
|
Strategy = SteppedStrategy
|
|
|
|
|
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
|
2024-02-19 02:19:59 +00:00
|
|
|
numDatasetBlocks = 8
|
2024-02-08 02:27:11 +00:00
|
|
|
numTotalBlocks = calcEcBlocksCount(numDatasetBlocks, ecK, ecM) # total number of blocks in the dataset after
|
|
|
|
# EC (should will match number of slots)
|
|
|
|
originalDatasetSize = numDatasetBlocks * blockSize.int
|
|
|
|
totalDatasetSize = numTotalBlocks * blockSize.int
|
|
|
|
|
2024-02-19 02:19:59 +00:00
|
|
|
numSlotBlocks = numTotalBlocks div numSlots
|
|
|
|
numBlockCells = (blockSize div cellSize).int # number of cells per block
|
|
|
|
numSlotCells = numSlotBlocks * numBlockCells # number of uncorrected slot cells
|
|
|
|
pow2SlotCells = nextPowerOfTwo(numSlotCells) # pow2 cells per slot
|
|
|
|
numPadSlotBlocks = (pow2SlotCells div numBlockCells) - numSlotBlocks # pow2 blocks per slot
|
|
|
|
|
|
|
|
numSlotBlocksTotal = # pad blocks per slot
|
|
|
|
if numPadSlotBlocks > 0:
|
|
|
|
numPadSlotBlocks + numSlotBlocks
|
|
|
|
else:
|
|
|
|
numSlotBlocks
|
|
|
|
|
|
|
|
numBlocksTotal = numSlotBlocksTotal * numSlots
|
2024-02-08 02:27:11 +00:00
|
|
|
|
|
|
|
# empty digest
|
|
|
|
emptyDigest = SpongeMerkle.digest(newSeq[byte](blockSize.int), cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
datasetBlocks: seq[bt.Block]
|
2024-02-08 02:27:11 +00:00
|
|
|
padBlocks: seq[bt.Block]
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore: BlockStore
|
|
|
|
manifest: Manifest
|
|
|
|
protectedManifest: Manifest
|
2024-02-08 02:27:11 +00:00
|
|
|
builder: Poseidon2Builder
|
2024-01-08 22:52:46 +00:00
|
|
|
chunker: Chunker
|
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
|
|
|
repoDs = SQLiteDatastore.new(Memory).tryGet()
|
|
|
|
metaDs = SQLiteDatastore.new(Memory).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
localStore = RepoStore.new(repoDs, metaDs)
|
2024-01-08 22:52:46 +00:00
|
|
|
chunker = RandomChunker.new(Rng.instance(), size = totalDatasetSize, chunkSize = blockSize)
|
2024-02-08 02:27:11 +00:00
|
|
|
datasetBlocks = await chunker.createBlocks(localStore)
|
|
|
|
|
|
|
|
(manifest, protectedManifest) =
|
|
|
|
await createProtectedManifest(
|
|
|
|
datasetBlocks,
|
|
|
|
localStore,
|
|
|
|
numDatasetBlocks,
|
|
|
|
ecK, ecM,
|
|
|
|
blockSize,
|
|
|
|
originalDatasetSize,
|
|
|
|
totalDatasetSize)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
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)
|
2024-02-08 02:27:11 +00:00
|
|
|
reset(builder)
|
2024-01-08 22:52:46 +00:00
|
|
|
reset(chunker)
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
test "Can only create builder with protected manifest":
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
|
|
|
unprotectedManifest = Manifest.new(
|
|
|
|
treeCid = Cid.example,
|
|
|
|
blockSize = blockSize.NBytes,
|
|
|
|
datasetSize = originalDatasetSize.NBytes)
|
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
Poseidon2Builder.new(localStore, unprotectedManifest, cellSize = cellSize)
|
|
|
|
.error.msg == "Manifest is not protected."
|
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,
|
2024-02-07 20:54:57 +00:00
|
|
|
ecM = ecM,
|
2024-02-08 02:27:11 +00:00
|
|
|
strategy = Strategy)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
Poseidon2Builder.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,
|
2024-02-07 20:54:57 +00:00
|
|
|
ecM = ecM,
|
2024-02-08 02:27:11 +00:00
|
|
|
strategy = Strategy)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
Poseidon2Builder.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-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
builder.cellSize == cellSize
|
|
|
|
builder.numSlots == numSlots
|
|
|
|
builder.numBlockCells == numBlockCells
|
2024-02-19 02:19:59 +00:00
|
|
|
builder.numSlotBlocks == numSlotBlocksTotal
|
2024-02-08 02:27:11 +00:00
|
|
|
builder.numSlotCells == pow2SlotCells
|
2024-02-19 02:19:59 +00:00
|
|
|
builder.numBlocks == numBlocksTotal
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "Should build slot hashes for all slots":
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
steppedStrategy = Strategy.init(
|
2024-02-19 02:19:59 +00:00
|
|
|
0, numBlocksTotal - 1, numSlots)
|
2024-02-08 02:27:11 +00:00
|
|
|
|
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-19 02:19:59 +00:00
|
|
|
for i in 0..<numSlots:
|
|
|
|
let
|
|
|
|
expectedHashes = collect(newSeq):
|
|
|
|
for j, idx in steppedStrategy.getIndicies(i):
|
|
|
|
if j > (protectedManifest.numSlotBlocks - 1):
|
|
|
|
emptyDigest
|
|
|
|
else:
|
|
|
|
SpongeMerkle.digest(datasetBlocks[idx].data, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-19 02:19:59 +00:00
|
|
|
cellHashes = (await builder.getCellHashes(i)).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-19 02:19:59 +00:00
|
|
|
check:
|
|
|
|
cellHashes.len == expectedHashes.len
|
|
|
|
cellHashes == expectedHashes
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "Should build slot trees for all slots":
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
steppedStrategy = Strategy.init(
|
2024-02-19 02:19:59 +00:00
|
|
|
0, numBlocksTotal - 1, numSlots)
|
2024-02-08 02:27:11 +00:00
|
|
|
|
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
for i in 0..<numSlots:
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
expectedHashes = collect(newSeq):
|
|
|
|
for j, idx in steppedStrategy.getIndicies(i):
|
|
|
|
if j > (protectedManifest.numSlotBlocks - 1):
|
|
|
|
emptyDigest
|
|
|
|
else:
|
|
|
|
SpongeMerkle.digest(datasetBlocks[idx].data, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
expectedRoot = Merkle.digest(expectedHashes)
|
|
|
|
slotTree = (await builder.buildSlotTree(i)).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
slotTree.root().tryGet() == expectedRoot
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
test "Should persist trees for all slots":
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
for i in 0..<numSlots:
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
slotTree = (await builder.buildSlotTree(i)).tryGet()
|
|
|
|
slotRoot = (await builder.buildSlot(i)).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
slotCid = slotRoot.toSlotCid().tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
for cellIndex in 0..<numPadSlotBlocks:
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
|
|
|
(cellCid, proof) = (await localStore.getCidAndProof(slotCid, cellIndex)).tryGet()
|
|
|
|
verifiableProof = proof.toVerifiableProof().tryGet()
|
2024-02-08 02:27:11 +00:00
|
|
|
posProof = slotTree.getProof(cellIndex).tryGet()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
verifiableProof.path == posProof.path
|
2024-01-08 22:52:46 +00:00
|
|
|
verifiableProof.index == posProof.index
|
|
|
|
verifiableProof.nleaves == posProof.nleaves
|
|
|
|
|
|
|
|
test "Should build correct verification root":
|
|
|
|
let
|
2024-02-19 02:19:59 +00:00
|
|
|
steppedStrategy = Strategy.init(0, numBlocksTotal - 1, numSlots)
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
(await builder.buildSlots()).tryGet
|
2024-01-11 16:45:23 +00:00
|
|
|
let
|
2024-01-08 22:52:46 +00:00
|
|
|
slotsHashes = collect(newSeq):
|
2024-02-08 02:27:11 +00:00
|
|
|
for i in 0..<numSlots:
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
slotHashes = collect(newSeq):
|
|
|
|
for j, idx in steppedStrategy.getIndicies(i):
|
|
|
|
if j > (protectedManifest.numSlotBlocks - 1):
|
|
|
|
emptyDigest
|
|
|
|
else:
|
|
|
|
SpongeMerkle.digest(datasetBlocks[idx].data, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
Merkle.digest(slotHashes)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
expectedRoot = Merkle.digest(slotsHashes)
|
|
|
|
rootHash = builder.buildVerifyTree(builder.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-19 02:19:59 +00:00
|
|
|
steppedStrategy = Strategy.init(0, numBlocksTotal - 1, numSlots)
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-08 22:52:46 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
slotsHashes = collect(newSeq):
|
2024-02-08 02:27:11 +00:00
|
|
|
for i in 0..<numSlots:
|
2024-01-08 22:52:46 +00:00
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
slotHashes = collect(newSeq):
|
|
|
|
for j, idx in steppedStrategy.getIndicies(i):
|
|
|
|
if j > (protectedManifest.numSlotBlocks - 1):
|
|
|
|
emptyDigest
|
|
|
|
else:
|
|
|
|
SpongeMerkle.digest(datasetBlocks[idx].data, cellSize.int)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
Merkle.digest(slotHashes)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
expectedRoot = Merkle.digest(slotsHashes)
|
|
|
|
manifest = (await builder.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
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
2024-02-08 02:27:11 +00:00
|
|
|
verifyManifest = (await builder.buildManifest()).tryGet()
|
2024-01-11 16:45:23 +00:00
|
|
|
|
|
|
|
verifyManifest.slotRoots = @[]
|
2024-02-08 02:27:11 +00:00
|
|
|
check Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).isErr
|
|
|
|
|
|
|
|
test "Should not build from verifiable manifest with incorrect number of slots":
|
|
|
|
var
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
verifyManifest = (await builder.buildManifest()).tryGet()
|
2024-01-11 16:45:23 +00:00
|
|
|
|
|
|
|
verifyManifest.slotRoots.del(
|
|
|
|
verifyManifest.slotRoots.len - 1
|
|
|
|
)
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
check Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
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
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-19 02:10:16 +00:00
|
|
|
var
|
2024-02-08 02:27:11 +00:00
|
|
|
verifyManifest = (await builder.buildManifest()).tryGet()
|
2024-01-11 16:45:23 +00:00
|
|
|
|
|
|
|
rng.shuffle(
|
|
|
|
Rng.instance,
|
|
|
|
verifyManifest.verifyRoot.data.buffer)
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
check Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).isErr
|
|
|
|
|
|
|
|
test "Should build from verifiable manifest":
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
builder = Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
protectedManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
verifyManifest = (await builder.buildManifest()).tryGet()
|
2024-01-11 16:45:23 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
verificationBuilder = Poseidon2Builder.new(
|
2024-01-11 16:45:23 +00:00
|
|
|
localStore,
|
|
|
|
verifyManifest,
|
|
|
|
cellSize = cellSize).tryGet()
|
|
|
|
|
|
|
|
check:
|
2024-02-08 02:27:11 +00:00
|
|
|
builder.slotRoots == verificationBuilder.slotRoots
|
|
|
|
builder.verifyRoot == verificationBuilder.verifyRoot
|