Skip to content

Commit

Permalink
feat!: Support workspaces and package selection on every nargo command
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jul 26, 2023
1 parent 551de53 commit b18483f
Show file tree
Hide file tree
Showing 31 changed files with 772 additions and 739 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ rustc_version = "0.4.0"
acvm.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
iter-extended.workspace = true
toml.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
noirc_errors.workspace = true
base64.workspace = true
base64.workspace = true
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Directories
/// The directory for the `nargo contract` command output
pub(crate) const CONTRACT_DIR: &str = "contract";
pub const CONTRACT_DIR: &str = "contract";
/// The directory to store serialized circuit proofs.
pub(crate) const PROOFS_DIR: &str = "proofs";
pub const PROOFS_DIR: &str = "proofs";
/// The directory to store Noir source files
pub(crate) const SRC_DIR: &str = "src";
pub const SRC_DIR: &str = "src";
/// The directory to store circuits' serialized ACIR representations.
pub(crate) const TARGET_DIR: &str = "target";
pub const TARGET_DIR: &str = "target";

// Files
/// The file from which Nargo pulls prover inputs
pub(crate) const PROVER_INPUT_FILE: &str = "Prover";
pub const PROVER_INPUT_FILE: &str = "Prover";
/// The file from which Nargo pulls verifier inputs
pub(crate) const VERIFIER_INPUT_FILE: &str = "Verifier";
pub const VERIFIER_INPUT_FILE: &str = "Verifier";
/// The package definition file for a Noir project.
pub(crate) const PKG_FILE: &str = "Nargo.toml";
pub const PKG_FILE: &str = "Nargo.toml";

// Extensions
/// The extension for files containing circuit proofs.
pub(crate) const PROOF_EXT: &str = "proof";
pub const PROOF_EXT: &str = "proof";
/// The extension for files containing proof witnesses.
pub(crate) const WITNESS_EXT: &str = "tr";
pub const WITNESS_EXT: &str = "tr";
4 changes: 3 additions & 1 deletion crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
//! Noir Package Manager abbreviated is npm, which is already taken.
pub mod artifacts;
pub mod constants;
mod errors;
pub mod manifest;
pub mod ops;
pub mod package;
pub mod workspace;

pub use self::errors::NargoError;
26 changes: 0 additions & 26 deletions crates/nargo/src/manifest/errors.rs

This file was deleted.

126 changes: 0 additions & 126 deletions crates/nargo/src/manifest/mod.rs

This file was deleted.

36 changes: 36 additions & 0 deletions crates/nargo/src/package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::{collections::BTreeMap, path::PathBuf};

use noirc_frontend::graph::{CrateName, CrateType};

use crate::{
constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE},
workspace::Workspace,
};

#[derive(Clone)]
pub enum Dependency {
Local { entry_path: PathBuf, crate_type: CrateType, workspace: Workspace },
Remote { entry_path: PathBuf, crate_type: CrateType, workspace: Workspace },
}

#[derive(Clone)]
pub struct Package {
pub root_dir: PathBuf,
pub crate_type: CrateType,
pub entry_path: PathBuf,
pub name: CrateName,
pub dependencies: BTreeMap<CrateName, Dependency>,
}

impl Package {
pub fn prover_input_path(&self) -> PathBuf {
// TODO: This should be configurable, such as if we are looking for .json or .toml or custom paths
// For now it is hard-coded to be toml.
self.root_dir.join(format!("{PROVER_INPUT_FILE}.toml"))
}
pub fn verifier_input_path(&self) -> PathBuf {
// TODO: This should be configurable, such as if we are looking for .json or .toml or custom paths
// For now it is hard-coded to be toml.
self.root_dir.join(format!("{VERIFIER_INPUT_FILE}.toml"))
}
}
76 changes: 76 additions & 0 deletions crates/nargo/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,79 @@
// Then we use workspace to allow more than one. In the future, do not allow there to be
// both a binary and a library.
// - library will be default

use std::{
iter::{once, Once},
path::PathBuf,
slice,
};

use noirc_frontend::graph::CrateName;

use crate::{
constants::{CONTRACT_DIR, PROOFS_DIR, TARGET_DIR},
package::Package,
};

#[derive(Clone)]
pub struct Workspace {
pub root_dir: PathBuf,
pub members: Vec<Package>,
// Selected is --package or default
pub selected_package: Option<CrateName>,
}

impl Workspace {
pub fn package_build_path(&self, package: &Package) -> PathBuf {
let name: String = package.name.clone().into();
self.target_directory_path().join(name)
}

pub fn contracts_directory_path(&self) -> PathBuf {
self.root_dir.join(CONTRACT_DIR)
}

pub fn proofs_directory_path(&self) -> PathBuf {
self.root_dir.join(PROOFS_DIR)
}

pub fn target_directory_path(&self) -> PathBuf {
self.root_dir.join(TARGET_DIR)
}
}

pub enum IntoIter<'a, T> {
Only(Once<&'a T>),
All(slice::Iter<'a, T>),
}

impl<'a> IntoIterator for &'a Workspace {
type Item = &'a Package;
type IntoIter = IntoIter<'a, Package>;

fn into_iter(self) -> Self::IntoIter {
if let Some(selected_name) = &self.selected_package {
// Precondition: The selected_package needed to be verified when creating the workspace
let member = self
.members
.iter()
.find(|member| matches!(member, Package { name, .. } if name == selected_name))
.expect("ice: Precondition failed - selected_package not a member");

IntoIter::Only(once(member))
} else {
IntoIter::All(self.members.iter())
}
}
}

impl<'a> Iterator for IntoIter<'a, Package> {
type Item = &'a Package;

fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Only(iter) => iter.next(),
Self::All(iter) => iter.next(),
}
}
}
4 changes: 1 addition & 3 deletions crates/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ fn generate_tests(test_file: &mut File) {
if config_data["exclude"].contains(&test_name) { "#[ignore]" } else { "" };

let should_fail = config_data["fail"].contains(&test_name);
let is_workspace = test_dir.to_str().map_or(false, |s| s.contains("workspace"));

write!(
test_file,
Expand All @@ -97,8 +96,7 @@ fn prove_and_verify_{test_sub_dir}_{test_name}() {{
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg(if {is_workspace} {{ "test" }} else {{ "execute" }});
cmd.arg("execute");
if {should_fail} {{
cmd.assert().failure();
Expand Down
Loading

0 comments on commit b18483f

Please sign in to comment.