Skip to content

Commit

Permalink
chore: transfer ownership of last debug result from context to repl
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Oct 26, 2023
1 parent c9bbbe3 commit 6919ba3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
43 changes: 15 additions & 28 deletions tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub(super) struct DebugContext<'a, B: BlackBoxFunctionSolver> {
brillig_solver: Option<BrilligSolver<'a, B>>,
foreign_call_executor: ForeignCallExecutor,
show_output: bool,
last_result: DebugCommandResult,
}

impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
Expand All @@ -35,7 +34,6 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
brillig_solver: None,
foreign_call_executor: ForeignCallExecutor::default(),
show_output: true,
last_result: DebugCommandResult::Ok,
}
}

Expand All @@ -57,20 +55,15 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
}
}

pub(super) fn get_last_result(&self) -> &DebugCommandResult {
&self.last_result
}

fn step_brillig_opcode(&mut self) -> &DebugCommandResult {
fn step_brillig_opcode(&mut self) -> DebugCommandResult {
let Some(mut solver) = self.brillig_solver.take() else {
unreachable!("Missing Brillig solver");
};
match solver.step() {
Ok(status) => match status {
BrilligSolverStatus::InProgress => {
self.brillig_solver = Some(solver);
self.last_result = DebugCommandResult::Ok;
&self.last_result
DebugCommandResult::Ok
}
BrilligSolverStatus::Finished => {
let status = self.acvm.finish_brillig_with_solver(solver);
Expand All @@ -80,34 +73,30 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
self.handle_foreign_call(foreign_call)
}
},
Err(err) => {
self.last_result = DebugCommandResult::Error(NargoError::ExecutionError(
ExecutionError::SolvingError(err),
));
&self.last_result
}
Err(err) => DebugCommandResult::Error(NargoError::ExecutionError(
ExecutionError::SolvingError(err),
)),
}
}

fn handle_foreign_call(&mut self, foreign_call: ForeignCallWaitInfo) -> &DebugCommandResult {
fn handle_foreign_call(&mut self, foreign_call: ForeignCallWaitInfo) -> DebugCommandResult {
let foreign_call_result =
self.foreign_call_executor.execute(&foreign_call, self.show_output);
self.last_result = match foreign_call_result {
match foreign_call_result {
Ok(foreign_call_result) => {
self.acvm.resolve_pending_foreign_call(foreign_call_result);
// TODO: should we retry executing the opcode somehow in this case?
DebugCommandResult::Ok
}
Err(error) => DebugCommandResult::Error(error),
};
&self.last_result
}
}

fn handle_acvm_status(&mut self, status: ACVMStatus) -> &DebugCommandResult {
fn handle_acvm_status(&mut self, status: ACVMStatus) -> DebugCommandResult {
if let ACVMStatus::RequiresForeignCall(foreign_call) = status {
self.handle_foreign_call(foreign_call)
} else {
let result = match status {
match status {
ACVMStatus::Solved => DebugCommandResult::Done,
ACVMStatus::InProgress => DebugCommandResult::Ok,
ACVMStatus::Failure(error) => DebugCommandResult::Error(
Expand All @@ -116,13 +105,11 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
ACVMStatus::RequiresForeignCall(_) => {
unreachable!("Unexpected pending foreign call resolution");
}
};
self.last_result = result;
self.get_last_result()
}
}
}

pub(super) fn step_into_opcode(&mut self) -> &DebugCommandResult {
pub(super) fn step_into_opcode(&mut self) -> DebugCommandResult {
if matches!(self.brillig_solver, Some(_)) {
self.step_brillig_opcode()
} else {
Expand All @@ -136,7 +123,7 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
}
}

pub(super) fn step_acir_opcode(&mut self) -> &DebugCommandResult {
pub(super) fn step_acir_opcode(&mut self) -> DebugCommandResult {
let status = if let Some(solver) = self.brillig_solver.take() {
self.acvm.finish_brillig_with_solver(solver)
} else {
Expand All @@ -145,11 +132,11 @@ impl<'a, B: BlackBoxFunctionSolver> DebugContext<'a, B> {
self.handle_acvm_status(status)
}

pub(super) fn cont(&mut self) -> &DebugCommandResult {
pub(super) fn cont(&mut self) -> DebugCommandResult {
loop {
let result = self.step_into_opcode();
if !matches!(result, DebugCommandResult::Ok) {
return self.get_last_result();
return result;
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions tooling/debugger/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::ops::Range;
pub struct ReplDebugger<'a, B: BlackBoxFunctionSolver> {
context: DebugContext<'a, B>,
debug_artifact: &'a DebugArtifact,
last_result: DebugCommandResult,
}

impl<'a, B: BlackBoxFunctionSolver> ReplDebugger<'a, B> {
Expand All @@ -30,7 +31,7 @@ impl<'a, B: BlackBoxFunctionSolver> ReplDebugger<'a, B> {
initial_witness: WitnessMap,
) -> Self {
let context = DebugContext::new(blackbox_solver, circuit, initial_witness);
Self { context, debug_artifact }
Self { context, debug_artifact, last_result: DebugCommandResult::Ok }
}

pub fn show_current_vm_status(&self) {
Expand All @@ -54,7 +55,8 @@ impl<'a, B: BlackBoxFunctionSolver> ReplDebugger<'a, B> {
}
}

fn handle_debug_command_result(&self) {
fn handle_debug_command_result(&mut self, result: DebugCommandResult) {
self.last_result = result;
self.show_current_vm_status();
}

Expand Down Expand Up @@ -125,13 +127,13 @@ impl<'a, B: BlackBoxFunctionSolver> ReplDebugger<'a, B> {
}

fn validate_in_progress(&self) -> bool {
match self.context.get_last_result() {
match self.last_result {
DebugCommandResult::Ok => true,
DebugCommandResult::Done => {
println!("Execution finished");
false
}
DebugCommandResult::Error(error) => {
DebugCommandResult::Error(ref error) => {
println!("ERROR: {}", error);
self.show_current_vm_status();
false
Expand All @@ -141,23 +143,23 @@ impl<'a, B: BlackBoxFunctionSolver> ReplDebugger<'a, B> {

fn step_acir_opcode(&mut self) {
if self.validate_in_progress() {
_ = self.context.step_acir_opcode();
self.handle_debug_command_result();
let result = self.context.step_acir_opcode();
self.handle_debug_command_result(result);
}
}

fn step_into_opcode(&mut self) {
if self.validate_in_progress() {
_ = self.context.step_into_opcode();
self.handle_debug_command_result();
let result = self.context.step_into_opcode();
self.handle_debug_command_result(result);
}
}

fn cont(&mut self) {
if self.validate_in_progress() {
println!("(Continuing execution...)");
_ = self.context.cont();
self.handle_debug_command_result();
let result = self.context.cont();
self.handle_debug_command_result(result);
}
}

Expand Down

0 comments on commit 6919ba3

Please sign in to comment.