Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions tooling/acvm_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use acir::circuit::Program;
use acir::native_types::{WitnessMap, WitnessStack};
use bn254_blackbox_solver::Bn254BlackBoxSolver;
use clap::Args;
use nargo::PrintOutput;

use nargo::foreign_calls::DefaultForeignCallBuilder;
use noir_artifact_cli::errors::CliError;
Expand Down Expand Up @@ -90,17 +89,17 @@ pub(crate) fn execute_program_from_witness(
let program: Program<FieldElement> =
Program::deserialize_program(bytecode).map_err(CliError::CircuitDeserializationError)?;

let mut foreign_call_executor = DefaultForeignCallBuilder::default()
.with_output(io::stdout())
.with_mocks(false)
.with_resolver_url(resolver_url)
.build();

nargo::ops::execute_program(
&program,
inputs_map,
&Bn254BlackBoxSolver(pedantic_solving),
&mut DefaultForeignCallBuilder {
output: PrintOutput::Stdout,
enable_mocks: false,
resolver_url,
..Default::default()
}
.build(),
&mut foreign_call_executor,
)
.map_err(CliError::CircuitExecutionError)
}
7 changes: 3 additions & 4 deletions tooling/artifact_cli/src/commands/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use crate::{
errors::CliError,
execution::{self, ExecutionResults},
};
use nargo::{
PrintOutput,
foreign_calls::{DefaultForeignCallBuilder, layers, transcript::ReplayForeignCallExecutor},
use nargo::foreign_calls::{
DefaultForeignCallBuilder, layers, transcript::ReplayForeignCallExecutor,
};
use noirc_driver::CompiledProgram;

Expand Down Expand Up @@ -122,7 +121,7 @@ fn execute(circuit: &CompiledProgram, args: &ExecuteCommand) -> Result<Execution
};

let mut foreign_call_executor = DefaultForeignCallBuilder {
output: PrintOutput::Stdout,
output: std::io::stdout(),
enable_mocks: false,
resolver_url: args.oracle_resolver.clone(),
root_path: None,
Expand Down
7 changes: 4 additions & 3 deletions tooling/ast_fuzzer/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use acir::{FieldElement, native_types::WitnessStack};
use arbitrary::Unstructured;
use bn254_blackbox_solver::Bn254BlackBoxSolver;
use color_eyre::eyre::{self, WrapErr, bail};
use nargo::{NargoError, PrintOutput, foreign_calls::DefaultForeignCallBuilder};
use nargo::{NargoError, foreign_calls::DefaultForeignCallBuilder};
use noirc_abi::{Abi, InputMap, input_parser::InputValue};
use noirc_evaluator::ssa::SsaProgramArtifact;
use noirc_frontend::monomorphization::ast::Program;
Expand Down Expand Up @@ -111,11 +111,11 @@ impl<P> CompareSsa<P> {
let initial_witness = self.abi.encode(&self.input_map, None).wrap_err("abi::encode")?;

let do_exec = |program| {
let mut print = String::new();
let mut print = Vec::new();

let mut foreign_call_executor = DefaultForeignCallBuilder::default()
.with_mocks(false)
.with_output(PrintOutput::String(&mut print))
.with_output(&mut print)
.build();

let res = nargo::ops::execute_program(
Expand All @@ -125,6 +125,7 @@ impl<P> CompareSsa<P> {
&mut foreign_call_executor,
);

let print = String::from_utf8(print).expect("should be valid utf8 string");
(res, print)
};

Expand Down
7 changes: 3 additions & 4 deletions tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,6 @@ mod tests {
BinaryFieldOp, HeapValueType, MemoryAddress, Opcode as BrilligOpcode, ValueOrArray,
},
};
use nargo::PrintOutput;

#[test]
fn test_resolve_foreign_calls_stepping_into_brillig() {
Expand Down Expand Up @@ -1034,7 +1033,7 @@ mod tests {
let initial_witness = BTreeMap::from([(Witness(1), fe_1)]).into();

let foreign_call_executor = Box::new(DefaultDebugForeignCallExecutor::from_artifact(
PrintOutput::Stdout,
std::io::stdout(),
debug_artifact,
));
let mut context = DebugContext::new(
Expand Down Expand Up @@ -1202,7 +1201,7 @@ mod tests {
let initial_witness = BTreeMap::from([(Witness(1), fe_1), (Witness(2), fe_1)]).into();

let foreign_call_executor = Box::new(DefaultDebugForeignCallExecutor::from_artifact(
PrintOutput::Stdout,
std::io::stdout(),
debug_artifact,
));
let brillig_funcs = &[brillig_bytecode];
Expand Down Expand Up @@ -1299,7 +1298,7 @@ mod tests {
&circuits,
&debug_artifact,
WitnessMap::new(),
Box::new(DefaultDebugForeignCallExecutor::new(PrintOutput::Stdout)),
Box::new(DefaultDebugForeignCallExecutor::new(std::io::stdout())),
brillig_funcs,
);

Expand Down
3 changes: 1 addition & 2 deletions tooling/debugger/src/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use acvm::acir::circuit::Circuit;
use acvm::acir::circuit::brillig::BrilligBytecode;
use acvm::acir::native_types::WitnessMap;
use acvm::{BlackBoxFunctionSolver, FieldElement};
use nargo::PrintOutput;

use crate::context::DebugContext;
use crate::context::{DebugCommandResult, DebugLocation};
Expand Down Expand Up @@ -73,7 +72,7 @@ impl<'a, R: Read, W: Write, B: BlackBoxFunctionSolver<FieldElement>> DapSession<
debug_artifact,
initial_witness,
Box::new(DefaultDebugForeignCallExecutor::from_artifact(
PrintOutput::Stdout,
std::io::stdout(),
debug_artifact,
)),
unconstrained_functions,
Expand Down
21 changes: 9 additions & 12 deletions tooling/debugger/src/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ use acvm::{
acir::brillig::{ForeignCallParam, ForeignCallResult},
pwg::ForeignCallWaitInfo,
};
use nargo::{
PrintOutput,
foreign_calls::{
DefaultForeignCallBuilder, ForeignCallError, ForeignCallExecutor, layers::Layer,
},
use nargo::foreign_calls::{
DefaultForeignCallBuilder, ForeignCallError, ForeignCallExecutor, layers::Layer,
};
use noirc_artifacts::debug::{DebugArtifact, DebugVars, StackFrame};
use noirc_errors::debug_info::{DebugFnId, DebugVarId};
Expand Down Expand Up @@ -51,22 +48,22 @@ pub struct DefaultDebugForeignCallExecutor {
}

impl DefaultDebugForeignCallExecutor {
fn make(
output: PrintOutput<'_>,
fn make<'a, W: 'a + std::io::Write>(
output: W,
ex: DefaultDebugForeignCallExecutor,
) -> impl DebugForeignCallExecutor + '_ {
) -> impl DebugForeignCallExecutor + 'a {
DefaultForeignCallBuilder::default().with_output(output).build().add_layer(ex)
}

#[allow(clippy::new_ret_no_self, dead_code)]
pub fn new(output: PrintOutput<'_>) -> impl DebugForeignCallExecutor + '_ {
pub fn new<'a, W: 'a + std::io::Write>(output: W) -> impl DebugForeignCallExecutor + 'a {
Self::make(output, Self::default())
}

pub fn from_artifact<'a>(
output: PrintOutput<'a>,
pub fn from_artifact<'a, W: 'a + std::io::Write>(
output: W,
artifact: &DebugArtifact,
) -> impl DebugForeignCallExecutor + use<'a> {
) -> impl DebugForeignCallExecutor + use<'a, W> {
let mut ex = Self::default();
ex.load_artifact(artifact);
Self::make(output, ex)
Expand Down
6 changes: 3 additions & 3 deletions tooling/debugger/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use acvm::acir::native_types::{Witness, WitnessMap, WitnessStack};
use acvm::brillig_vm::MemoryValue;
use acvm::brillig_vm::brillig::Opcode as BrilligOpcode;
use acvm::{BlackBoxFunctionSolver, FieldElement};
use nargo::{NargoError, PrintOutput};
use nargo::NargoError;
use noirc_driver::CompiledProgram;

use crate::foreign_calls::DefaultDebugForeignCallExecutor;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl<'a, B: BlackBoxFunctionSolver<FieldElement>> ReplDebugger<'a, B> {
raw_source_printing: bool,
) -> Self {
let foreign_call_executor = Box::new(DefaultDebugForeignCallExecutor::from_artifact(
PrintOutput::Stdout,
std::io::stdout(),
debug_artifact,
));
let context = DebugContext::new(
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a, B: BlackBoxFunctionSolver<FieldElement>> ReplDebugger<'a, B> {
fn restart_session(&mut self) {
let breakpoints: Vec<DebugLocation> = self.context.iterate_breakpoints().copied().collect();
let foreign_call_executor = Box::new(DefaultDebugForeignCallExecutor::from_artifact(
PrintOutput::Stdout,
std::io::stdout(),
self.debug_artifact,
));
self.context = DebugContext::new(
Expand Down
3 changes: 1 addition & 2 deletions tooling/lsp/src/requests/test_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::future::{self, Future};
use crate::insert_all_files_for_workspace_into_file_manager;
use async_lsp::{ErrorCode, ResponseError};
use nargo::{
PrintOutput,
foreign_calls::DefaultForeignCallBuilder,
ops::{TestStatus, run_test},
};
Expand Down Expand Up @@ -87,7 +86,7 @@ fn on_test_run_request_inner(
&state.solver,
&mut context,
&test_function,
PrintOutput::Stdout,
std::io::stdout(),
&CompileOptions::default(),
|output, base| {
DefaultForeignCallBuilder {
Expand Down
58 changes: 36 additions & 22 deletions tooling/nargo/src/foreign_calls/default.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use acvm::AcirField;
use serde::{Deserialize, Serialize};

use crate::PrintOutput;

use super::{
ForeignCallExecutor,
layers::{self, Either, Layer, Layering},
Expand All @@ -15,8 +13,8 @@ use super::rpc::RPCForeignCallExecutor;

/// A builder for [DefaultForeignCallLayers] where we can enable fields based on feature flags,
/// which is easier than providing different overrides for a `new` method.
pub struct DefaultForeignCallBuilder<'a> {
pub output: PrintOutput<'a>,
pub struct DefaultForeignCallBuilder<W> {
pub output: W,
pub enable_mocks: bool,

#[cfg(feature = "rpc")]
Expand All @@ -29,10 +27,10 @@ pub struct DefaultForeignCallBuilder<'a> {
pub package_name: Option<String>,
}

impl Default for DefaultForeignCallBuilder<'_> {
impl Default for DefaultForeignCallBuilder<std::io::Empty> {
fn default() -> Self {
Self {
output: PrintOutput::default(),
output: std::io::empty(),
enable_mocks: true,

#[cfg(feature = "rpc")]
Expand All @@ -47,11 +45,19 @@ impl Default for DefaultForeignCallBuilder<'_> {
}
}

impl<'a> DefaultForeignCallBuilder<'a> {
impl<W> DefaultForeignCallBuilder<W> {
/// Override the output.
pub fn with_output(mut self, output: PrintOutput<'a>) -> Self {
self.output = output;
self
pub fn with_output<T>(self, output: T) -> DefaultForeignCallBuilder<T> {
DefaultForeignCallBuilder {
output,
enable_mocks: self.enable_mocks,
#[cfg(feature = "rpc")]
resolver_url: self.resolver_url,
#[cfg(feature = "rpc")]
root_path: self.root_path,
#[cfg(feature = "rpc")]
package_name: self.package_name,
}
}

/// Enable or disable mocks.
Expand All @@ -60,19 +66,26 @@ impl<'a> DefaultForeignCallBuilder<'a> {
self
}

/// Set or unset resolver url.
#[cfg(feature = "rpc")]
pub fn with_resolver_url(mut self, resolver_url: Option<String>) -> Self {
self.resolver_url = resolver_url;
self
}

/// Compose the executor layers with [layers::Empty] as the default handler.
pub fn build<F>(self) -> DefaultForeignCallLayers<'a, layers::Empty, F>
pub fn build<F>(self) -> DefaultForeignCallLayers<W, layers::Empty, F>
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
F: AcirField + Serialize + for<'de> Deserialize<'de>,
{
self.build_with_base(layers::Empty)
}

/// Compose the executor layers with `base` as the default handler.
pub fn build_with_base<B, F>(self, base: B) -> DefaultForeignCallLayers<'a, B, F>
pub fn build_with_base<B, F>(self, base: B) -> DefaultForeignCallLayers<W, B, F>
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
B: ForeignCallExecutor<F> + 'a,
F: AcirField + Serialize + for<'de> Deserialize<'de>,
B: ForeignCallExecutor<F>,
{
let executor = {
#[cfg(feature = "rpc")]
Expand Down Expand Up @@ -107,16 +120,16 @@ impl<'a> DefaultForeignCallBuilder<'a> {

/// Facilitate static typing of layers on a base layer, so inner layers can be accessed.
#[cfg(feature = "rpc")]
pub type DefaultForeignCallLayers<'a, B, F> = Layer<
PrintForeignCallExecutor<'a>,
pub type DefaultForeignCallLayers<W, B, F> = Layer<
PrintForeignCallExecutor<W>,
Layer<
Either<MockForeignCallExecutor<F>, DisabledMockForeignCallExecutor>,
Layer<Option<RPCForeignCallExecutor>, B>,
>,
>;
#[cfg(not(feature = "rpc"))]
pub type DefaultForeignCallLayers<'a, B, F> = Layer<
PrintForeignCallExecutor<'a>,
pub type DefaultForeignCallLayers<W, B, F> = Layer<
PrintForeignCallExecutor<W>,
Layer<Either<MockForeignCallExecutor<F>, DisabledMockForeignCallExecutor>, B>,
>;

Expand All @@ -131,13 +144,14 @@ pub struct DefaultForeignCallExecutor;
#[cfg(feature = "rpc")]
impl DefaultForeignCallExecutor {
#[allow(clippy::new_ret_no_self)]
pub fn new<'a, F>(
output: PrintOutput<'a>,
pub fn new<'a, W, F>(
output: W,
resolver_url: Option<&str>,
root_path: Option<std::path::PathBuf>,
package_name: Option<String>,
) -> impl ForeignCallExecutor<F> + 'a + use<'a, F>
) -> impl ForeignCallExecutor<F> + 'a + use<'a, W, F>
where
W: std::io::Write + 'a,
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
{
DefaultForeignCallBuilder {
Expand Down
Loading
Loading