From 9f32717d1119edd1bb6dfd1c080553c63bc8b845 Mon Sep 17 00:00:00 2001 From: munna0908 Date: Sun, 2 Mar 2025 13:08:23 +0530 Subject: [PATCH] code cleanup --- codex/slots/proofs/backends/circomcompat.nim | 47 ++++++++------------ codex/slots/proofs/backends/converters.nim | 4 +- tests/codex/slots/testprover.nim | 2 +- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/codex/slots/proofs/backends/circomcompat.nim b/codex/slots/proofs/backends/circomcompat.nim index a38cf346..c321c581 100644 --- a/codex/slots/proofs/backends/circomcompat.nim +++ b/codex/slots/proofs/backends/circomcompat.nim @@ -215,18 +215,13 @@ proc asyncProve*[H]( task.circom.taskpool.spawn circomProveTask(taskPtr) let threadFut = threadPtr.wait() - try: - await threadFut.join() - except CatchableError as exc: - try: - await threadFut - except AsyncError as asyncExc: - return failure(asyncExc.msg) - finally: - if exc of CancelledError: - raise (ref CancelledError) exc - else: - return failure(exc.msg) + if joinErr =? catch(await threadFut.join()).errorOption: + if err=? catch(await noCancel threadFut).errorOption: + return failure(err) + if joinErr of CancelledError: + raise joinErr + else: + return failure(joinErr) if not task.success.load(): return failure("Failed to prove circuit") @@ -254,11 +249,11 @@ proc circomVerifyTask(task: ptr VerifyTask) {.gcsafe.} = let res = verify_circuit(task[].proof, task[].inputs, task[].vkp) if res == ERR_OK: - task[].success[].store(true) + task[].success[]=true elif res == ERR_FAILED_TO_VERIFY_PROOF: - task[].success[].store(false) + task[].success[]=false else: - task[].success[].store(false) + task[].success[]=false error "Failed to verify proof", errorCode = res proc asyncVerify*[H]( @@ -292,19 +287,15 @@ proc asyncVerify*[H]( self.taskpool.spawn circomVerifyTask(taskPtr) let threadFut = threadPtr.wait() + + if joinErr =? catch(await threadFut.join()).errorOption: + if err=? catch(await noCancel threadFut).errorOption: + return failure(err) + if joinErr of CancelledError: + raise joinErr + else: + return failure(joinErr) - try: - await threadFut.join() - except CatchableError as exc: - try: - await threadFut - except AsyncError as asyncExc: - return failure(asyncExc.msg) - finally: - if exc of CancelledError: - raise (ref CancelledError) exc - else: - return failure(exc.msg) success() proc verify*[H]( @@ -318,7 +309,7 @@ proc verify*[H]( try: if error =? (await self.asyncVerify(proof, inputs, res)).errorOption: return failure(error) - return success(res[].load()) + return success(res[]) except CancelledError as exc: raise exc diff --git a/codex/slots/proofs/backends/converters.nim b/codex/slots/proofs/backends/converters.nim index 88a02202..0e74b000 100644 --- a/codex/slots/proofs/backends/converters.nim +++ b/codex/slots/proofs/backends/converters.nim @@ -23,14 +23,14 @@ type CircomProof* = Proof CircomKey* = VerifyingKey CircomInputs* = Inputs - VerifyResult* = ptr Atomic[bool] + VerifyResult* = ptr bool ProofPtr* = ptr Proof proc new*(_: type ProofPtr): ProofPtr = cast[ptr Proof](allocShared0(sizeof(Proof))) proc new*(_: type VerifyResult): VerifyResult = - cast[ptr Atomic[bool]](allocShared0(sizeof(Atomic[bool]))) + cast[ptr bool](allocShared0(sizeof(bool))) proc toCircomInputs*(inputs: ProofInputs[Poseidon2Hash]): CircomInputs = var diff --git a/tests/codex/slots/testprover.nim b/tests/codex/slots/testprover.nim index 895be1ea..0727f0b2 100644 --- a/tests/codex/slots/testprover.nim +++ b/tests/codex/slots/testprover.nim @@ -166,4 +166,4 @@ suite "Test Prover": check exc of CancelledError finally: # validate the verifyResponse - check verifyRes[].load() == true + check verifyRes[] == true