From c804c14512bbb5eda3eae5c07d5cdb9930132ba5 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:04:42 +0100 Subject: [PATCH] chore: make `acvm` a non-optional field on `DebugContext` (#3149) Co-authored-by: Tom French --- tooling/debugger/src/lib.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tooling/debugger/src/lib.rs b/tooling/debugger/src/lib.rs index 6fde85e35f1..d1b0b350be3 100644 --- a/tooling/debugger/src/lib.rs +++ b/tooling/debugger/src/lib.rs @@ -18,7 +18,7 @@ enum SolveResult { } struct DebugContext<'backend, B: BlackBoxFunctionSolver> { - acvm: Option>, + acvm: ACVM<'backend, B>, debug_artifact: DebugArtifact, foreign_call_executor: ForeignCallExecutor, circuit: &'backend Circuit, @@ -27,7 +27,7 @@ struct DebugContext<'backend, B: BlackBoxFunctionSolver> { impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { fn step_opcode(&mut self) -> Result { - let solver_status = self.acvm.as_mut().unwrap().solve_opcode(); + let solver_status = self.acvm.solve_opcode(); match solver_status { ACVMStatus::Solved => Ok(SolveResult::Done), @@ -59,16 +59,15 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { ACVMStatus::RequiresForeignCall(foreign_call) => { let foreign_call_result = self.foreign_call_executor.execute(&foreign_call, self.show_output)?; - self.acvm.as_mut().unwrap().resolve_pending_foreign_call(foreign_call_result); + self.acvm.resolve_pending_foreign_call(foreign_call_result); Ok(SolveResult::Ok) } } } fn show_current_vm_status(&self) { - let acvm = self.acvm.as_ref().unwrap(); - let ip = acvm.instruction_pointer(); - let opcodes = acvm.opcodes(); + let ip = self.acvm.instruction_pointer(); + let opcodes = self.acvm.opcodes(); if ip >= opcodes.len() { println!("Finished execution"); } else { @@ -101,8 +100,8 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { Ok(SolveResult::Done) } - fn finalize(&mut self) -> WitnessMap { - self.acvm.take().unwrap().finalize() + fn finalize(self) -> WitnessMap { + self.acvm.finalize() } } @@ -120,10 +119,8 @@ pub fn debug_circuit( initial_witness: WitnessMap, show_output: bool, ) -> Result, NargoError> { - let opcodes = circuit.opcodes.clone(); - let context = RefCell::new(DebugContext { - acvm: Some(ACVM::new(blackbox_solver, &opcodes, initial_witness)), + acvm: ACVM::new(blackbox_solver, &circuit.opcodes, initial_witness), foreign_call_executor: ForeignCallExecutor::default(), circuit, debug_artifact, @@ -169,8 +166,12 @@ pub fn debug_circuit( repl.run().expect("Debugger error"); + // REPL execution has finished. + // Drop it so that we can move fields out from `context` again. + drop(repl); + if solved.get() { - let solved_witness = context.borrow_mut().finalize(); + let solved_witness = context.into_inner().finalize(); Ok(Some(solved_witness)) } else { Ok(None)