Skip to content

Commit

Permalink
fix: primary_message typo in errors.rs (AztecProtocol/aztec-packages#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AztecBot committed Apr 15, 2024
1 parent 305bcdc commit 6e4e26a
Show file tree
Hide file tree
Showing 23 changed files with 531 additions and 242 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10d9ad99200a5897417ff5669763ead4e38d87fa
1dfbe7bc3bf3c455d8fb6c8b5fe6a96c1edf7af9
5 changes: 2 additions & 3 deletions acvm-repo/acvm_js/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function run_if_available {
require_command jq
require_command cargo
require_command wasm-bindgen
require_command wasm-opt

self_path=$(dirname "$(readlink -f "$0")")
pname=$(cargo read-manifest | jq -r '.name')
Expand All @@ -49,5 +48,5 @@ BROWSER_WASM=${BROWSER_DIR}/${pname}_bg.wasm
run_or_fail cargo build --lib --release --target $TARGET --package ${pname}
run_or_fail wasm-bindgen $WASM_BINARY --out-dir $NODE_DIR --typescript --target nodejs
run_or_fail wasm-bindgen $WASM_BINARY --out-dir $BROWSER_DIR --typescript --target web
run_or_fail wasm-opt $NODE_WASM -o $NODE_WASM -O
run_or_fail wasm-opt $BROWSER_WASM -o $BROWSER_WASM -O
run_if_available wasm-opt $NODE_WASM -o $NODE_WASM -O
run_if_available wasm-opt $BROWSER_WASM -o $BROWSER_WASM -O
2 changes: 1 addition & 1 deletion aztec_macros/src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<AztecMacroError> for MacroError {
fn from(err: AztecMacroError) -> Self {
match err {
AztecMacroError::AztecDepNotFound {} => MacroError {
primary_message: "Aztec dependency not found. Please add aztec as a dependency in your Cargo.toml. For more information go to https://docs.aztec.network/developers/debugging/aztecnr-errors#aztec-dependency-not-found-please-add-aztec-as-a-dependency-in-your-nargotoml".to_owned(),
primary_message: "Aztec dependency not found. Please add aztec as a dependency in your Nargo.toml. For more information go to https://docs.aztec.network/developers/debugging/aztecnr-errors#aztec-dependency-not-found-please-add-aztec-as-a-dependency-in-your-nargotoml".to_owned(),
secondary_message: None,
span: None,
},
Expand Down
7 changes: 7 additions & 0 deletions noir_stdlib/src/test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ unconstrained fn create_mock_oracle<N>(name: str<N>) -> Field {}
#[oracle(set_mock_params)]
unconstrained fn set_mock_params_oracle<P>(id: Field, params: P) {}

#[oracle(get_mock_last_params)]
unconstrained fn get_mock_last_params_oracle<P>(id: Field) -> P {}

#[oracle(set_mock_returns)]
unconstrained fn set_mock_returns_oracle<R>(id: Field, returns: R) {}

Expand All @@ -27,6 +30,10 @@ impl OracleMock {
self
}

unconstrained pub fn get_last_params<P>(self) -> P {
get_mock_last_params_oracle(self.id)
}

unconstrained pub fn returns<R>(self, returns: R) -> Self {
set_mock_returns_oracle(self.id, returns);
self
Expand Down
2 changes: 0 additions & 2 deletions test_programs/execution_success/mock_oracle/Prover.toml

This file was deleted.

27 changes: 0 additions & 27 deletions test_programs/execution_success/mock_oracle/src/main.nr

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
name = "mock_oracle"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
[dependencies]
Empty file.
130 changes: 130 additions & 0 deletions test_programs/noir_test_success/mock_oracle/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use dep::std::test::OracleMock;

struct Point {
x: Field,
y: Field,
}

impl Eq for Point {
fn eq(self, other: Point) -> bool {
(self.x == other.x) & (self.y == other.y)
}
}

#[oracle(void_field)]
unconstrained fn void_field_oracle() -> Field {}

unconstrained fn void_field() -> Field {
void_field_oracle()
}

#[oracle(field_field)]
unconstrained fn field_field_oracle(_x: Field) -> Field {}

unconstrained fn field_field(x: Field) -> Field {
field_field_oracle(x)
}

#[oracle(struct_field)]
unconstrained fn struct_field_oracle(_point: Point, _array: [Field; 4]) -> Field {}

unconstrained fn struct_field(point: Point, array: [Field; 4]) -> Field {
struct_field_oracle(point, array)
}

#[test(should_fail)]
fn test_mock_no_returns() {
OracleMock::mock("void_field");
void_field(); // Some return value must be set
}

#[test]
fn test_mock() {
OracleMock::mock("void_field").returns(10);
assert_eq(void_field(), 10);
}

#[test]
fn test_multiple_mock() {
let first_mock = OracleMock::mock("void_field").returns(10);
OracleMock::mock("void_field").returns(42);

// The mocks are searched for in creation order, so the first one prevents the second from being called.
assert_eq(void_field(), 10);

first_mock.clear();
assert_eq(void_field(), 42);
}

#[test]
fn test_multiple_mock_times() {
OracleMock::mock("void_field").returns(10).times(2);
OracleMock::mock("void_field").returns(42);

assert_eq(void_field(), 10);
assert_eq(void_field(), 10);
assert_eq(void_field(), 42);
}

#[test]
fn test_mock_with_params() {
OracleMock::mock("field_field").with_params((5,)).returns(10);
assert_eq(field_field(5), 10);
}

#[test]
fn test_multiple_mock_with_params() {
OracleMock::mock("field_field").with_params((5,)).returns(10);
OracleMock::mock("field_field").with_params((7,)).returns(14);

assert_eq(field_field(5), 10);
assert_eq(field_field(7), 14);
}

#[test]
fn test_mock_last_params() {
let mock = OracleMock::mock("field_field").returns(10);
assert_eq(field_field(5), 10);

assert_eq(mock.get_last_params(), 5);
}

#[test]
fn test_mock_last_params_many_calls() {
let mock = OracleMock::mock("field_field").returns(10);
assert_eq(field_field(5), 10);
assert_eq(field_field(7), 10);

assert_eq(mock.get_last_params(), 7);
}

#[test]
fn test_mock_struct_field() {
// Combination of simpler test cases

let array = [1, 2, 3, 4];
let another_array = [4, 3, 2, 1];
let point = Point { x: 14, y: 27 };

OracleMock::mock("struct_field").returns(42).times(2);
let timeless_mock = OracleMock::mock("struct_field").returns(0);

assert_eq(42, struct_field(point, array));
assert_eq(42, struct_field(point, array));
// The times(2) mock is now cleared

assert_eq(0, struct_field(point, array));

let last_params: (Point, [Field; 4]) = timeless_mock.get_last_params();
assert_eq(last_params.0, point);
assert_eq(last_params.1, array);

// We clear the mock with no times() to allow other mocks to be callable
timeless_mock.clear();

OracleMock::mock("struct_field").with_params((point, array)).returns(10);
OracleMock::mock("struct_field").with_params((point, another_array)).returns(20);
assert_eq(10, struct_field(point, array));
assert_eq(20, struct_field(point, another_array));
}

7 changes: 3 additions & 4 deletions tooling/acvm_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use bn254_blackbox_solver::Bn254BlackBoxSolver;
use clap::Args;

use crate::cli::fs::inputs::{read_bytecode_from_file, read_inputs_from_file};
use crate::cli::fs::witness::save_witness_to_dir;
use crate::errors::CliError;
use nargo::ops::{execute_program, DefaultForeignCallExecutor};

use super::fs::witness::create_output_witness_string;
use super::fs::witness::{create_output_witness_string, save_witness_to_dir};

/// Executes a circuit to calculate its return value
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -46,9 +45,9 @@ fn run_command(args: ExecuteCommand) -> Result<String, CliError> {
)?;
if args.output_witness.is_some() {
save_witness_to_dir(
&output_witness_string,
&args.working_directory,
output_witness,
&args.output_witness.unwrap(),
&args.working_directory,
)?;
}
Ok(output_witness_string)
Expand Down
47 changes: 34 additions & 13 deletions tooling/acvm_cli/src/cli/fs/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ use std::{
path::{Path, PathBuf},
};

use acvm::acir::native_types::WitnessMap;
use acvm::acir::native_types::{WitnessMap, WitnessStack};

use crate::errors::{CliError, FilesystemError};

/// Saves the provided output witnesses to a toml file created at the given location
pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
output_witness: &String,
witness_dir: P,
file_name: &String,
) -> Result<PathBuf, FilesystemError> {
let witness_path = witness_dir.as_ref().join(file_name);
fn create_named_dir(named_dir: &Path, name: &str) -> PathBuf {
std::fs::create_dir_all(named_dir)
.unwrap_or_else(|_| panic!("could not create the `{name}` directory"));

PathBuf::from(named_dir)
}

let mut file = File::create(&witness_path)
.map_err(|_| FilesystemError::OutputWitnessCreationFailed(file_name.clone()))?;
write!(file, "{}", output_witness)
.map_err(|_| FilesystemError::OutputWitnessWriteFailed(file_name.clone()))?;
fn write_to_file(bytes: &[u8], path: &Path) -> String {
let display = path.display();

Ok(witness_path)
let mut file = match File::create(path) {
Err(why) => panic!("couldn't create {display}: {why}"),
Ok(file) => file,
};

match file.write_all(bytes) {
Err(why) => panic!("couldn't write to {display}: {why}"),
Ok(_) => display.to_string(),
}
}

/// Creates a toml representation of the provided witness map
Expand All @@ -34,3 +39,19 @@ pub(crate) fn create_output_witness_string(witnesses: &WitnessMap) -> Result<Str

toml::to_string(&witness_map).map_err(|_| CliError::OutputWitnessSerializationFailed())
}

pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
witnesses: WitnessStack,
witness_name: &str,
witness_dir: P,
) -> Result<PathBuf, FilesystemError> {
create_named_dir(witness_dir.as_ref(), "witness");
let witness_path = witness_dir.as_ref().join(witness_name).with_extension("gz");

let buf: Vec<u8> = witnesses
.try_into()
.map_err(|_op| FilesystemError::OutputWitnessCreationFailed(witness_name.to_string()))?;
write_to_file(buf.as_slice(), &witness_path);

Ok(witness_path)
}
3 changes: 0 additions & 3 deletions tooling/acvm_cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub(crate) enum FilesystemError {

#[error(" Error: failed to create output witness file {0}.")]
OutputWitnessCreationFailed(String),

#[error(" Error: failed to write output witness file {0}.")]
OutputWitnessWriteFailed(String),
}

#[derive(Debug, Error)]
Expand Down
Loading

0 comments on commit 6e4e26a

Please sign in to comment.