Skip to content

Commit

Permalink
Handle debug halt as expected result
Browse files Browse the repository at this point in the history
  • Loading branch information
mverzilli committed Oct 12, 2023
1 parent 449d16e commit c3a3138
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
34 changes: 6 additions & 28 deletions tooling/debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use nargo::NargoError;

use nargo::ops::ForeignCallExecutor;

use thiserror::Error;

use easy_repl::{command, CommandStatus, Critical, Repl};
use std::cell::{Cell, RefCell};

Expand All @@ -19,17 +17,6 @@ enum SolveResult {
Ok,
}

#[derive(Debug, Error)]
enum DebuggingError {
/// ACIR circuit execution error
#[error(transparent)]
ExecutionError(#[from] nargo::errors::ExecutionError),

/// Oracle handling error
#[error(transparent)]
ForeignCallError(#[from] noirc_printable_type::ForeignCallError),
}

struct DebugContext<'backend, B: BlackBoxFunctionSolver> {
acvm: Option<ACVM<'backend, B>>,
debug_artifact: DebugArtifact,
Expand All @@ -39,7 +26,7 @@ struct DebugContext<'backend, B: BlackBoxFunctionSolver> {
}

impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
fn step_opcode(&mut self) -> Result<SolveResult, DebuggingError> {
fn step_opcode(&mut self) -> Result<SolveResult, NargoError> {
// Assert messages are not a map due to https://github.com/noir-lang/acvm/issues/522
let assert_messages = &self.circuit.assert_messages;
let get_assert_message = |opcode_location| {
Expand All @@ -65,7 +52,7 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
_ => None,
};

Err(DebuggingError::ExecutionError(match call_stack {
Err(NargoError::ExecutionError(match call_stack {
Some(call_stack) => {
if let Some(assert_message) = get_assert_message(
call_stack.last().expect("Call stacks should not be empty"),
Expand Down Expand Up @@ -116,7 +103,7 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
}
}

fn cont(&mut self) -> Result<SolveResult, DebuggingError> {
fn cont(&mut self) -> Result<SolveResult, NargoError> {
loop {
match self.step_opcode()? {
SolveResult::Done => break,
Expand All @@ -131,15 +118,6 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> {
}
}

impl From<nargo::errors::NargoError> for DebuggingError {
fn from(e: nargo::errors::NargoError) -> Self {
match e {
NargoError::ForeignCallError(e1) => DebuggingError::ForeignCallError(e1),
_ => DebuggingError::ExecutionError(ExecutionError::Halted),
}
}
}

fn map_command_status(result: SolveResult) -> CommandStatus {
match result {
SolveResult::Ok => CommandStatus::Done,
Expand All @@ -153,7 +131,7 @@ pub fn debug_circuit<B: BlackBoxFunctionSolver>(
debug_artifact: DebugArtifact,
initial_witness: WitnessMap,
show_output: bool,
) -> Result<WitnessMap, NargoError> {
) -> Result<Option<WitnessMap>, NargoError> {
let opcodes = circuit.opcodes.clone();

let context = RefCell::new(DebugContext {
Expand Down Expand Up @@ -208,8 +186,8 @@ pub fn debug_circuit<B: BlackBoxFunctionSolver>(

if solved.get() {
let solved_witness = context.borrow_mut().finalize();
Ok(solved_witness)
Ok(Some(solved_witness))
} else {
Err(NargoError::ExecutionError(ExecutionError::Halted))
Ok(None)
}
}
4 changes: 0 additions & 4 deletions tooling/nargo/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl NargoError {
OpcodeResolutionError::BrilligFunctionFailed { message, .. } => Some(message),
OpcodeResolutionError::BlackBoxFunctionFailed(_, reason) => Some(reason),
},
ExecutionError::Halted => None,
}
}
}
Expand All @@ -61,9 +60,6 @@ pub enum ExecutionError {

#[error(transparent)]
SolvingError(#[from] OpcodeResolutionError),

#[error("Execution halted")]
Halted,
}

/// Extracts the opcode locations from a nargo error.
Expand Down
34 changes: 23 additions & 11 deletions tooling/nargo_cli/src/cli/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ pub(crate) fn run(
let (return_value, solved_witness) =
debug_program_and_decode(compiled_program, package, &args.prover_name)?;

println!("[{}] Circuit witness successfully solved", package.name);
if let Some(return_value) = return_value {
println!("[{}] Circuit output: {return_value:?}", package.name);
}
if let Some(witness_name) = &args.witness_name {
let witness_path = save_witness_to_dir(solved_witness, witness_name, target_dir)?;
if let Some(solved_witness) = solved_witness {
println!("[{}] Circuit witness successfully solved", package.name);

if let Some(return_value) = return_value {
println!("[{}] Circuit output: {return_value:?}", package.name);
}

if let Some(witness_name) = &args.witness_name {
let witness_path =
save_witness_to_dir(solved_witness, witness_name, target_dir)?;

println!("[{}] Witness saved to {}", package.name, witness_path.display());
println!("[{}] Witness saved to {}", package.name, witness_path.display());
}
} else {
println!("Debugger execution halted.");
}

// Only debug the first binary package that matches the selection criteria
Expand All @@ -90,21 +97,26 @@ fn debug_program_and_decode(
program: CompiledProgram,
package: &Package,
prover_name: &str,
) -> Result<(Option<InputValue>, WitnessMap), CliError> {
) -> Result<(Option<InputValue>, Option<WitnessMap>), CliError> {
// Parse the initial witness values from Prover.toml
let (inputs_map, _) =
read_inputs_from_file(&package.root_dir, prover_name, Format::Toml, &program.abi)?;
let solved_witness = debug_program(&program, &inputs_map)?;
let public_abi = program.abi.public_abi();
let (_, return_value) = public_abi.decode(&solved_witness)?;

Ok((return_value, solved_witness))
match solved_witness {
Some(witness) => {
let (_, return_value) = public_abi.decode(&witness)?;
Ok((return_value, Some(witness)))
}
None => Ok((None, None)),
}
}

pub(crate) fn debug_program(
compiled_program: &CompiledProgram,
inputs_map: &InputMap,
) -> Result<WitnessMap, CliError> {
) -> Result<Option<WitnessMap>, CliError> {
#[allow(deprecated)]
let blackbox_solver = barretenberg_blackbox_solver::BarretenbergSolver::new();

Expand Down

0 comments on commit c3a3138

Please sign in to comment.