Initializes backend in prover constructor

This commit is contained in:
benbierens 2024-08-23 10:31:05 +02:00
parent c1ae6e24d8
commit 4a94df3663
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
4 changed files with 29 additions and 49 deletions

View File

@ -272,9 +272,7 @@ proc new*(
engine = BlockExcEngine.new(repoStore, wallet, network, blockDiscovery, peerStore, pendingBlocks)
store = NetworkStore.new(engine, repoStore)
prover = if config.prover:
some Prover.new(
store,
config.numProofSamples)
some Prover.new(store, config).expect("Unable to create prover.")
else:
none Prover

View File

@ -740,11 +740,6 @@ proc start*(self: CodexNodeRef) {.async.} =
error "Unable to start validator contract interactions: ", error=error.msg
self.contracts.validator = ValidatorInteractions.none
if prover =? s.codexNode.prover:
if err =? (await prover.start(s.config)).errorOption:
error "Failed to start prover", msg = err.msg
raise err
self.networkId = self.switch.peerInfo.peerId
notice "Started codex node", id = self.networkId, addrs = self.switch.peerInfo.addrs

View File

@ -5,6 +5,7 @@ import pkg/chronicles
import pkg/questionable
import pkg/confutils/defs
import pkg/stew/io2
import pkg/ethers
import ../../conf
import ./backends
@ -65,12 +66,12 @@ proc suggestDownloadTool(config: CodexConf) =
$address
]
error "Proving circuit files are not found. Please run the following to download them:" &
"'./" & tokens.join(" ") & "'"
echo "Proving circuit files are not found. Please run the following to download them:"
echo "'./" & tokens.join(" ") & "'"
proc initializeBackend*(
config: CodexConf,
utils: BackendUtils = BackendUtils()): Future[?!AnyBackend] {.async.} =
utils: BackendUtils = BackendUtils()): ?!AnyBackend =
without backend =? initializeFromConfig(config, utils), cliErr:
info "Could not initialize prover backend from CLI options...", msg = cliErr.msg

View File

@ -43,7 +43,7 @@ type
AnyProofInputs* = ProofInputs[Poseidon2Hash]
Prover* = ref object of RootObj
backend: ?AnyBackend
backend: AnyBackend
store: BlockStore
nSamples: int
@ -62,27 +62,24 @@ proc prove*(
trace "Received proof challenge"
if backend =? self.backend:
without builder =? AnyBuilder.new(self.store, manifest), err:
error "Unable to create slots builder", err = err.msg
return failure(err)
without builder =? AnyBuilder.new(self.store, manifest), err:
error "Unable to create slots builder", err = err.msg
return failure(err)
without sampler =? AnySampler.new(slotIdx, self.store, builder), err:
error "Unable to create data sampler", err = err.msg
return failure(err)
without sampler =? AnySampler.new(slotIdx, self.store, builder), err:
error "Unable to create data sampler", err = err.msg
return failure(err)
without proofInput =? await sampler.getProofInput(challenge, self.nSamples), err:
error "Unable to get proof input for slot", err = err.msg
return failure(err)
without proofInput =? await sampler.getProofInput(challenge, self.nSamples), err:
error "Unable to get proof input for slot", err = err.msg
return failure(err)
# prove slot
without proof =? backend.prove(proofInput), err:
error "Unable to prove slot", err = err.msg
return failure(err)
# prove slot
without proof =? self.backend.prove(proofInput), err:
error "Unable to prove slot", err = err.msg
return failure(err)
success (proofInput, proof)
else:
return failure("Prover was not started")
success (proofInput, proof)
proc verify*(
self: Prover,
@ -90,29 +87,18 @@ proc verify*(
inputs: AnyProofInputs): Future[?!bool] {.async.} =
## Prove a statement using backend.
## Returns a future that resolves to a proof.
if backend =? self.backend:
return backend.verify(proof, inputs)
else:
return failure("Prover was not started")
proc start*(
self: Prover,
config: CodexConf): Future[?!void] {.async.} =
without backend =? (await initializeBackend(config)), err:
error "Failed to initialize backend", msg = err.msg
return failure(err)
self.backend = some backend
return success()
self.backend.verify(proof, inputs)
proc new*(
_: type Prover,
store: BlockStore,
nSamples: int): Prover =
config: CodexConf): ?!Prover =
Prover(
without backend =? initializeBackend(config), err:
error "Failed to initialize backend", msg = err.msg
return failure(err)
success Prover(
store: store,
backend: none AnyBackend,
nSamples: nSamples)
backend: backend,
nSamples: config.numProofSamples)