Applies result-type to onProve callback.

This commit is contained in:
benbierens 2024-01-16 10:49:54 +01:00 committed by Dmitriy Ryajov
parent 2ba0977214
commit 4afec65d00
5 changed files with 29 additions and 17 deletions

View File

@ -548,7 +548,7 @@ proc onStore(
proc onProve(
self: CodexNodeRef,
slot: Slot,
challenge: ProofChallenge): Future[seq[byte]] {.async.} =
challenge: ProofChallenge): Future[?!seq[byte]] {.async.} =
## Generats a proof for a given slot and challenge
##
@ -564,24 +564,27 @@ proc onProve(
trace "Received proof challenge"
without cid =? Cid.init(cidStr).mapFailure, err:
trace "Unable to parse Cid", cid, err = err.msg
return
error "Unable to parse Cid", cid, err = err.msg
return failure(err)
without manifest =? await self.fetchManifest(cid), err:
trace "Unable to fetch manifest for cid", err = err.msg
return
error "Unable to fetch manifest for cid", err = err.msg
return failure(err)
without builder =? SlotsBuilder.new(self.blockStore, manifest), err:
trace "Unable to create slots builder", err = err.msg
return
error "Unable to create slots builder", err = err.msg
return failure(err)
without sampler =? DataSampler.new(slotIdx, self.blockStore, builder), err:
trace "Unable to create data sampler", err = err.msg
return
error "Unable to create data sampler", err = err.msg
return failure(err)
without proofInput =? await sampler.getProofInput(challenge, nSamples = 3), err:
trace "Unable to get proof input for slot", err = err.msg
return
error "Unable to get proof input for slot", err = err.msg
return failure(err)
# Todo: send proofInput to circuit. Get proof. (Profit, repeat.)
success(@[42'u8])
proc onExpiryUpdate(
self: CodexNodeRef,
@ -631,7 +634,7 @@ proc start*(self: CodexNodeRef) {.async.} =
self.onClear(request, slotIndex)
hostContracts.sales.onProve =
proc(slot: Slot, challenge: ProofChallenge): Future[seq[byte]] =
proc(slot: Slot, challenge: ProofChallenge): Future[?!seq[byte]] =
# TODO: generate proof
self.onProve(slot, challenge)

View File

@ -27,7 +27,7 @@ type
OnStore* = proc(request: StorageRequest,
slot: UInt256,
blocksCb: BlocksCb): Future[?!void] {.gcsafe, upraises: [].}
OnProve* = proc(slot: Slot, challenge: ProofChallenge): Future[seq[byte]] {.gcsafe, upraises: [].}
OnProve* = proc(slot: Slot, challenge: ProofChallenge): Future[?!seq[byte]] {.gcsafe, upraises: [].}
OnExpiryUpdate* = proc(rootCid: string, expiry: SecondsSince1970): Future[?!void] {.gcsafe, upraises: [].}
OnClear* = proc(request: StorageRequest,
slotIndex: UInt256) {.gcsafe, upraises: [].}

View File

@ -1,9 +1,11 @@
import pkg/chronicles
import pkg/questionable/results
import ../statemachine
import ../salesagent
import ./errorhandling
import ./filling
import ./cancelled
import ./errored
import ./failed
logScope:
@ -34,7 +36,10 @@ method run*(state: SaleInitialProving, machine: Machine): Future[?State] {.async
let
slot = Slot(request: request, slotIndex: data.slotIndex)
challenge = await context.market.getChallenge(slot.id)
proof = await onProve(slot, challenge)
without proof =? (await onProve(slot, challenge)), err:
error "Failed to generate initial proof", error = err.msg
return some State(SaleErrored(error: err))
debug "Finished proof calculation", requestId = $data.requestId
return some State(SaleFilling(proof: proof))

View File

@ -1,5 +1,6 @@
import std/options
import pkg/chronicles
import pkg/questionable/results
import ../../clock
import ../statemachine
import ../salesagent
@ -27,7 +28,10 @@ method prove*(
currentPeriod: Period
) {.base, async.} =
try:
let proof = await onProve(slot, challenge)
without proof =? (await onProve(slot, challenge)), err:
error "Failed to generate proof", error = err.msg
# In this state, there's nothing we can do except try again next time.
return
debug "Submitting proof", currentPeriod = currentPeriod, slotId = $slot.id
await market.submitProof(slot.id, proof)
except CatchableError as e:

View File

@ -69,10 +69,10 @@ proc new*(
blockStore: blockStore,
builder: builder)
proc getCell*(self: DataSampler, blkBytes: seq[byte], blkCellIdx: uint64): Cell =
proc getCell*(self: DataSampler, blkBytes: seq[byte], blkCellIdx: Natural): Cell =
let
cellSize = self.builder.cellSize.uint64
dataStart = (cellSize * blkCellIdx)
dataStart = cellSize * blkCellIdx.uint64
dataEnd = dataStart + cellSize
return blkBytes[dataStart ..< dataEnd]