Skip to content

Commit

Permalink
Cleanup error handling. Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Jan 4, 2024
1 parent b3da3eb commit eb4dcb9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
48 changes: 28 additions & 20 deletions codex/proof/datasampler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ proc new*(
): Future[?!DataSampler] {.async.} =
# Create a DataSampler for a slot.
# A DataSampler can create the input required for the proving circuit.
without slotBlocks =? await SlotBlocks.new(slot, blockStore):
error "Failed to create SlotBlocks object for slot"
return failure("Failed to create SlotBlocks object for slot")
without slotBlocks =? (await SlotBlocks.new(slot, blockStore)), e:
error "Failed to create SlotBlocks object for slot", error = e.msg
return failure(e)

let
manifest = slotBlocks.manifest
Expand All @@ -70,17 +70,17 @@ proc new*(
if not manifest.verifiable:
return failure("Can only create DataSampler using verifiable manifests.")

without starter =? (await startDataSampler(blockStore, manifest, slot)), error:
error "Failed to start data sampler"
return failure(error)
without starter =? (await startDataSampler(blockStore, manifest, slot)), e:
error "Failed to start data sampler", error = e.msg
return failure(e)

without datasetRoot =? manifest.verificationRoot.fromProvingCid(), error:
error "Failed to convert manifest verification root to Poseidon2Hash"
return failure(error)
without datasetRoot =? manifest.verificationRoot.fromProvingCid(), e:
error "Failed to convert manifest verification root to Poseidon2Hash", error = e.msg
return failure(e)

without slotRootHash =? starter.slotPoseidonTree.root(), error:
error "Failed to get slot tree root"
return failure(error)
without slotRootHash =? starter.slotTreeCid.fromSlotCid(), e:
error "Failed to convert slot cid to hash", error = e.msg
return failure(e)

success(DataSampler(
slot: slot,
Expand Down Expand Up @@ -138,30 +138,38 @@ proc getCellFromBlock*(self: DataSampler, blk: bt.Block, slotCellIndex: uint64):
return blk.data[dataStart ..< dataEnd]

proc getBlockCellMiniTree*(self: DataSampler, blk: bt.Block): ?!Poseidon2Tree =
return digestTree(blk.data, DefaultCellSize.int)
return Poseidon2Tree.digestTree(blk.data, DefaultCellSize.int)

proc getSlotBlockProof(self: DataSampler, slotBlockIndex: uint64): Future[?!Poseidon2Proof] {.async.} =
await self.blockStore.getCidAndProof(self.slotTreeCid, slotBlockIndex.int)
without (cid, proof) =? (await self.blockStore.getCidAndProof(self.slotTreeCid, slotBlockIndex.int)), err:
error "Unable to load cid and proof", error = err.msg
return failure(err)

without poseidon2Proof =? proof.toVerifiableProof(), err:
error "Unable to convert proof", error = err.msg
return failure(err)

return success(poseidon2Proof)

proc createProofSample(self: DataSampler, slotCellIndex: uint64) : Future[?!ProofSample] {.async.} =
let
slotBlockIndex = self.getSlotBlockIndexForSlotCellIndex(slotCellIndex)
blockCellIndex = self.getBlockCellIndexForSlotCellIndex(slotCellIndex)

without blockProof =? self.getSlotBlockProof(slotBlockIndex), err:
error "Failed to get slot-to-block inclusion proof"
without blockProof =? (await self.getSlotBlockProof(slotBlockIndex)), err:
error "Failed to get slot-to-block inclusion proof", error = err.msg
return failure(err)

without blk =? await self.slotBlocks.getSlotBlock(slotBlockIndex), err:
error "Failed to get slot block"
error "Failed to get slot block", error = err.msg
return failure(err)

without miniTree =? self.getBlockCellMiniTree(blk), err:
error "Failed to calculate minitree for block"
error "Failed to calculate minitree for block", error = err.msg
return failure(err)

without cellProof =? miniTree.getProof(blockCellIndex.int), err:
error "Failed to get block-to-cell inclusion proof"
error "Failed to get block-to-cell inclusion proof", error = err.msg
return failure(err)

let cell = self.getCellFromBlock(blk, slotCellIndex)
Expand All @@ -181,7 +189,7 @@ proc getProofInput*(self: DataSampler, challenge: Poseidon2Hash, nSamples: int):
trace "Collecing input for proof", selectedSlotCellIndices = $slotCellIndices
for slotCellIndex in slotCellIndices:
without sample =? await self.createProofSample(slotCellIndex), err:
error "Failed to create proof sample"
error "Failed to create proof sample", error = err.msg
return failure(err)
samples.add(sample)

Expand Down
22 changes: 12 additions & 10 deletions codex/proof/datasamplerstarter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import pkg/chronicles
import pkg/chronos
import pkg/questionable
import pkg/questionable/results
import pkg/constantine/math/io/io_fields

import ../contracts/requests
import ../blocktype as bt
Expand All @@ -23,6 +24,7 @@ proc getNumberOfBlocksInSlot*(slot: Slot, manifest: Manifest): uint64 =
let blockSize = manifest.blockSize.uint64
(slot.request.ask.slotSize.truncate(uint64) div blockSize)

# This shouldn't be necessary... is it? Should it be in utils?
proc rollUp[T](input: seq[?!T]): ?!seq[T] =
var output = newSeq[T]()
for element in input:
Expand All @@ -37,33 +39,33 @@ proc convertSlotRootCidsToHashes(slotRoots: seq[Cid]): ?!seq[Poseidon2Hash] =

proc calculateDatasetSlotProof(manifest: Manifest, slotRoots: seq[Cid], slotIndex: uint64): ?!Poseidon2Proof =
without leafs =? convertSlotRootCidsToHashes(slotRoots), err:
error "Failed to convert leaf Cids"
error "Failed to convert leaf Cids", error = err.msg
return failure(err)

# Todo: Duplicate of SlotBuilder.nim:166 and 212
# -> Extract/unify top-tree creation.
let rootsPadLeafs = newSeqWith(manifest.numSlots.nextPowerOfTwoPad, Poseidon2Zero)

without tree =? Poseidon2Tree.init(leafs & self.rootsPadLeafs), err:
error "Failed to calculate Dataset-SlotRoot tree"
without tree =? Poseidon2Tree.init(leafs & rootsPadLeafs), err:
error "Failed to calculate Dataset-SlotRoot tree", error = err.msg
return failure(err)

tree.getProof(slotIndex.int)

proc recreateSlotTree(blockStore: BlockStore, manifest: Manifest, slotTreeCid: Cid, datasetSlotIndex: uint64): Future[?!void] {.async.} =
without expectedSlotRoot =? slotTreeCid.fromSlotCid(), err:
error "Failed to convert slotTreeCid to hash"
error "Failed to convert slotTreeCid to hash", error = err.msg
return failure(err)

without slotBuilder =? SlotBuilder.new(blockStore, manifest), err:
error "Failed to initialize SlotBuilder"
error "Failed to initialize SlotBuilder", error = err.msg
return failure(err)

without reconsturctedSlotRoot =? (await slotBuilder.buildSlot(datasetSlotIndex.int)), err:
error "Failed to reconstruct slot tree", error = err.msg
return failure(err)

if not reconsturctedSlotRoot == expectedSlotRoot:
if reconsturctedSlotRoot.toDecimal() != expectedSlotRoot.toDecimal():
error "Reconstructed slot root does not match manifest slot root."
return failure("Reconstructed slot root does not match manifest slot root.")

Expand Down Expand Up @@ -93,12 +95,12 @@ proc startDataSampler*(blockStore: BlockStore, manifest: Manifest, slot: Slot):

trace "Initializing data sampler", datasetSlotIndex

without datasetToSlotProof =? calculateDatasetSlotProof(slotRoots, datasetSlotIndex), err:
error "Failed to calculate dataset-slot inclusion proof"
without datasetToSlotProof =? calculateDatasetSlotProof(manifest, slotRoots, datasetSlotIndex), err:
error "Failed to calculate dataset-slot inclusion proof", error = err.msg
return failure(err)

without slotPoseidonTree =? await ensureSlotTree(blockStore, manifest, slot, datasetSlotIndex), err:
error "Failed to load or recreate slot tree"
if err =? (await ensureSlotTree(blockStore, manifest, slot, slotTreeCid, datasetSlotIndex)).errorOption:
error "Failed to load or recreate slot tree", error = err.msg
return failure(err)

success(DataSamplerStarter(
Expand Down

0 comments on commit eb4dcb9

Please sign in to comment.