From b842122a9eb7ca31e1e3c0fe21a538ebd08de0e3 Mon Sep 17 00:00:00 2001 From: Tomasz Bekas Date: Wed, 19 Jun 2024 17:03:30 +0200 Subject: [PATCH] Prover async backend tmp --- codex/slots/proofs/backends/asyncbackend.nim | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 codex/slots/proofs/backends/asyncbackend.nim diff --git a/codex/slots/proofs/backends/asyncbackend.nim b/codex/slots/proofs/backends/asyncbackend.nim new file mode 100644 index 00000000..57ff649d --- /dev/null +++ b/codex/slots/proofs/backends/asyncbackend.nim @@ -0,0 +1,74 @@ +import std/sequtils + +import pkg/taskpools +import pkg/taskpools/flowvars +import pkg/chronos +import pkg/chronos/threadsync +import pkg/questionable/results + +import ./circomcompat +import ../../errors +import ../../logutils + +logScope: + topics = "codex asyncprover" + +type + ProverArgs[H] = object + circom: CircomCompat + data: ProofInputs[H] + + VerifierArgs[H] = object + circom: CircomCompat + proof: CircomProof + inputs: ProofInputs[H] + +const + CompletitionTimeout = 1.seconds # Maximum await time for completition after receiving a signal + CompletitionRetryDelay = 10.millis + +proc awaitResult[T](signal: ThreadSignalPtr, handle: Flowvar[T]): Future[?!T] {.async.} = + await wait(signal) + + var + res: T + awaitTotal: Duration + while awaitTotal < CompletitionTimeout: + if handle.tryComplete(res): + return success(res) + else: + awaitTotal += CompletitionRetryDelay + await sleepAsync(CompletitionRetryDelay) + + return failure("Task signaled finish but didn't return any result within " & $CompletitionRetryDelay) + +proc asyncProve*[H]( + tp: Taskpool, + backend: CircomCompat, + input: ProofInputs[H] +): Future[?!CircomProof] {.async.} = + + without signal =? ThreadSignalPtr.new().mapFailure, err: + return failure(err) + + try: + echo "test" + finally: + if err =? signal.close().mapFailure.errorOption(): + error "Error closing signal", msg = $err.msg + + +proc asyncVerify*[H]( + tp: Taskpool, + self: CircomCompat, + proof: CircomProof, + inputs: ProofInputs[H] +): Future[?!bool] {.async.} = + without signal =? ThreadSignalPtr.new().mapFailure, err: + return failure(err) + + try: + echo "test" + finally: + if err =? signal.close().mapFailure.errorOption(): + error "Error closing signal", msg = $err.msg \ No newline at end of file