From 983da3c24f7f932a7006950c51adef2e7dacf42b Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 4 Mar 2024 09:43:35 +0100 Subject: [PATCH] functional implementation --- codex/slots/proofs/backendfactory.nim | 52 +++++++++++++++++++-------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/codex/slots/proofs/backendfactory.nim b/codex/slots/proofs/backendfactory.nim index e9f55093..8c02f781 100644 --- a/codex/slots/proofs/backendfactory.nim +++ b/codex/slots/proofs/backendfactory.nim @@ -2,6 +2,7 @@ import os import httpclient import zip/zipfiles import pkg/chronos +import pkg/chronicles import pkg/questionable import pkg/confutils/defs import pkg/stew/io2 @@ -30,19 +31,20 @@ proc initializeFromConfig( endsWith($config.circomZkey, ".zkey"): return failure("Circom zkey file not accessible") + trace "Initialized prover backend from cli config" success(initializeCircomBackend( $config.circomR1cs, $config.circomWasm, $config.circomZkey)) proc r1csFilePath(config: CodexConf): string = - config.dataDir / "circuit.r1cs" + config.dataDir / "proof_main.r1cs" proc wasmFilePath(config: CodexConf): string = - config.dataDir / "circuit.wasm" + config.dataDir / "proof_main.wasm" proc zkeyFilePath(config: CodexConf): string = - config.dataDir / "circuit.zkey" + config.dataDir / "proof_main.zkey" proc zipFilePath(config: CodexConf): string = config.dataDir / "circuit.zip" @@ -51,6 +53,7 @@ proc initializeFromCeremonyFiles(config: CodexConf): ?!AnyBackend = if fileExists(config.r1csFilePath) and fileExists(config.wasmFilePath) and fileExists(config.zkeyFilePath): + trace "Initialized prover backend from local files" return success(initializeCircomBackend( config.r1csFilePath, config.wasmFilePath, @@ -58,28 +61,47 @@ proc initializeFromCeremonyFiles(config: CodexConf): ?!AnyBackend = failure("Ceremony files not found") -proc downloadCeremonyUrl( +proc downloadCeremony( config: CodexConf, - proofCeremonyUrl: string -) = - var client = newHttpClient() - client.downloadFile( - "https://circuit.codex.storage/proving-key/" & proofCeremonyUrl, config.zipFilePath) + ceremonyHash: string +): ?!void = + # TODO: + # In the future, the zip file will be stored in the Codex network + # instead of a url + ceremonyHash, we'll get a CID from the marketplace contract. + + echo "OVERRIDE!" + let hash = "1f512a1c6a089eff7eb22b810438c34fc59d4b0e7935dbc77ec77255d39ec094" + + let url = "https://circuit.codex.storage/proving-key/" & hash # was ceremonyHash + trace "Downloading ceremony file", url, filepath = config.zipFilePath + try: + # Nim's default webclient does not support SSL on all platforms. + # Not without shipping additional binaries and cert-files... :( + # So we're using curl for now. + var rc = execShellCmd("curl -o " & config.zipFilePath & " " & url) + if not rc == 0: + return failure("Download failed with return code: " & $rc) + except Exception as exc: + return failure(exc.msg) + trace "Download completed." + success() proc unzipCeremonyFile( config: CodexConf): ?!void = + trace "Unzipping..." var z: ZipArchive if not z.open(config.zipFilePath): return failure("Unable to open zip file: " & config.zipFilePath) z.extractAll($config.dataDir) success() -proc initializeFromCeremonyUrl( +proc initializeFromCeremonyHash( config: CodexConf, - proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} = + ceremonyHash: ?string): Future[?!AnyBackend] {.async.} = - if url =? proofCeremonyUrl: - downloadCeremonyUrl(config, url) + if hash =? ceremonyHash: + if dlErr =? downloadCeremony(config, hash).errorOption: + return failure(dlErr) if err =? unzipCeremonyFile(config).errorOption: return failure(err) without backend =? initializeFromCeremonyFiles(config), err: @@ -90,13 +112,13 @@ proc initializeFromCeremonyUrl( proc initializeBackend*( config: CodexConf, - proofCeremonyUrl: ?string): Future[?!AnyBackend] {.async.} = + ceremonyHash: ?string): Future[?!AnyBackend] {.async.} = without backend =? initializeFromConfig(config), cliErr: info "Could not initialize prover backend from CLI options...", msg = cliErr.msg without backend =? initializeFromCeremonyFiles(config), localErr: info "Could not initialize prover backend from local files...", msg = localErr.msg - without backend =? (await initializeFromCeremonyUrl(config, proofCeremonyUrl)), urlErr: + without backend =? (await initializeFromCeremonyHash(config, ceremonyHash)), urlErr: warn "Could not initialize prover backend from ceremony url...", msg = urlErr.msg return failure(urlErr) return success(backend)