Extracts backend initialization to backend-factory

This commit is contained in:
benbierens 2024-03-01 14:02:57 +01:00
parent 63ab4c5064
commit 83e1347038
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
3 changed files with 54 additions and 35 deletions

View File

@ -0,0 +1,44 @@
import pkg/chronos
import pkg/questionable
import pkg/confutils/defs
import ../../conf
import ./backends
proc initializeFromConfig(
config: CodexConf): ?!AnyBackend =
# check provided files exist
# initialize backend with files
# or failure
success(CircomCompat.init($config.circomR1cs, $config.circomWasm, $config.circomZkey))
proc initializeFromCeremonyFiles(): ?!AnyBackend =
# initialize from previously-downloaded files if they exist
echo "todo"
failure("todo")
proc initializeFromCeremonyUrl(
proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} =
# download the ceremony url
# unzip it
without backend =? initializeFromCeremonyFiles(), err:
return failure(err)
return success(backend)
proc initializeBackend*(
config: CodexConf,
proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} =
without backend =? initializeFromConfig(config), cliErr:
info "Could not initialize prover backend from CLI options...", msg = cliErr.msg
without backend =? initializeFromCeremonyFiles(), localErr:
info "Could not initialize prover backend from local files...", msg = localErr.msg
without backend =? (await initializeFromCeremonyUrl(proofCeremonyUrl)), urlErr:
warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg
return failure(urlErr)
return success(backend)

View File

@ -1,3 +1,6 @@
import ./backends/circomcompat import ./backends/circomcompat
export circomcompat export circomcompat
type
AnyBackend* = CircomCompat

View File

@ -13,7 +13,6 @@ import pkg/chronicles
import pkg/circomcompat import pkg/circomcompat
import pkg/poseidon2 import pkg/poseidon2
import pkg/questionable/results import pkg/questionable/results
import pkg/confutils/defs
import pkg/libp2p/cid import pkg/libp2p/cid
@ -28,6 +27,7 @@ import ../builder
import ../sampler import ../sampler
import ./backends import ./backends
import ./backendfactory
import ../types import ../types
export backends export backends
@ -36,7 +36,6 @@ logScope:
topics = "codex prover" topics = "codex prover"
type type
AnyBackend* = CircomCompat
AnyProof* = CircomProof AnyProof* = CircomProof
AnySampler* = Poseidon2Sampler AnySampler* = Poseidon2Sampler
@ -97,43 +96,16 @@ proc verify*(
else: else:
return failure("Prover was not started") return failure("Prover was not started")
proc initializeFromConfig(
self: Prover,
config: CodexConf): ?!void =
# check provided files exist
# initialize backend with files
# or failure
self.backend = some CircomCompat.init($config.circomR1cs, $config.circomWasm, $config.circomZkey)
success()
proc initializeFromCeremonyFiles(
self: Prover): ?!void =
# initialize from previously-downloaded files if they exist
echo "todo"
success()
proc initializeFromCeremonyUrl(
self: Prover,
proofCeremonyUrl: ?string): Future[?!void] {.async.} =
# download the ceremony url
# unzip it
return self.initializeFromCeremonyFiles()
proc start*( proc start*(
self: Prover, self: Prover,
config: CodexConf, config: CodexConf,
proofCeremonyUrl: ?string): Future[?!void] {.async.} = proofCeremonyUrl: ?string): Future[?!void] {.async.} =
if cliErr =? self.initializeFromConfig(config).errorOption:
info "Could not initialize prover backend from CLI options...", msg = cliErr.msg without backend =? (await initializeBackend(config, proofCeremonyUrl)), err:
if localErr =? self.initializeFromCeremonyFiles().errorOption: error "Failed to initialize backend", msg = err.msg
info "Could not initialize prover backend from local files...", msg = localErr.msg return failure(err)
if urlErr =? (await self.initializeFromCeremonyUrl(proofCeremonyUrl)).errorOption:
warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg self.backend = some backend
return failure(urlErr)
return success() return success()
proc new*( proc new*(