Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nargo)!: Replace --contracts flag with contract package type #2204

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions crates/nargo/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};
pub enum PackageType {
Library,
Binary,
Contract,
}

impl Display for PackageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Library => write!(f, "lib"),
Self::Binary => write!(f, "bin"),
Self::Contract => write!(f, "contract"),
}
}
}
Expand Down Expand Up @@ -64,6 +66,10 @@ impl Package {
self.package_type == PackageType::Binary
}

pub fn is_contract(&self) -> bool {
self.package_type == PackageType::Contract
}

pub fn is_library(&self) -> bool {
self.package_type == PackageType::Library
}
Expand Down
16 changes: 5 additions & 11 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ pub(crate) struct CompileCommand {
#[arg(long)]
include_keys: bool,

/// Compile each contract function used within the program
#[arg(short, long)]
contracts: bool,

/// The name of the package to compile
#[clap(long)]
package: Option<CrateName>,
Expand All @@ -60,10 +56,10 @@ pub(crate) fn run<B: Backend>(

let mut common_reference_string = read_cached_common_reference_string();

// If contracts is set we're compiling every function in a 'contract' rather than just 'main'.
if args.contracts {
for package in &workspace {
let (mut context, crate_id) = prepare_package(package);
for package in &workspace {
let (mut context, crate_id) = prepare_package(package);
// If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'.
if package.is_contract() {
let result = compile_contracts(&mut context, crate_id, &args.compile_options);
let contracts = report_errors(result, &context, args.compile_options.deny_warnings)?;

Expand Down Expand Up @@ -105,9 +101,7 @@ pub(crate) fn run<B: Backend>(
&circuit_dir,
);
}
}
} else {
for package in &workspace {
} else {
let (_, program) = compile_package(backend, package, &args.compile_options)?;

common_reference_string =
Expand Down
10 changes: 10 additions & 0 deletions crates/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ fn test_main() {
}
"#;

const CONTRACT_EXAMPLE: &str = r#"contract Main {
fn double(x: Field) -> pub Field { x * 2 }
fn triple(x: Field) -> pub Field { x * 3 }
internal fn quadruple(x: Field) -> pub Field { x * 4 }
}
"#;
phated marked this conversation as resolved.
Show resolved Hide resolved

const LIB_EXAMPLE: &str = r#"fn my_util(x : Field, y : Field) -> bool {
x != y
}
Expand Down Expand Up @@ -88,6 +95,9 @@ compiler_version = "{CARGO_PKG_VERSION}"
// This uses the `match` syntax instead of `if` so we get a compile error when we add new package types (which likely need new template files)
match package_type {
PackageType::Binary => write_to_file(BIN_EXAMPLE.as_bytes(), &src_dir.join("main.nr")),
PackageType::Contract => {
write_to_file(CONTRACT_EXAMPLE.as_bytes(), &src_dir.join("main.nr"))
phated marked this conversation as resolved.
Show resolved Hide resolved
}
PackageType::Library => write_to_file(LIB_EXAMPLE.as_bytes(), &src_dir.join("lib.nr")),
};
println!("Project successfully created! It is located at {}", package_dir.display());
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo_cli/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl PackageConfig {
let package_type = match self.package.package_type.as_deref() {
Some("lib") => PackageType::Library,
Some("bin") => PackageType::Binary,
Some("contract") => PackageType::Contract,
Some(invalid) => {
return Err(ManifestError::InvalidPackageType(
root_dir.join("Nargo.toml"),
Expand All @@ -63,7 +64,7 @@ impl PackageConfig {
PackageType::Library => {
root_dir.join("src").join("lib").with_extension(FILE_EXTENSION)
}
PackageType::Binary => {
PackageType::Binary | PackageType::Contract => {
root_dir.join("src").join("main").with_extension(FILE_EXTENSION)
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "contracts"
type = "bin"
type = "contract"
authors = [""]
compiler_version = "0.1"

Expand Down
1 change: 1 addition & 0 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
let compiled_program = compile_no_check(context, options, main)?;

if options.print_acir {
println!("Compiled ACIR for main (unoptimized):");

Check warning on line 167 in crates/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
println!("{}", compiled_program.circuit);
}

Expand All @@ -179,6 +179,7 @@
) -> Result<(Vec<CompiledContract>, Warnings), ErrorsAndWarnings> {
let warnings = check_crate(context, crate_id, options.deny_warnings)?;

// TODO: We probably want to error if contracts is empty
let contracts = context.get_all_contracts(&crate_id);
let mut compiled_contracts = vec![];
let mut errors = warnings;
Expand All @@ -197,7 +198,7 @@
for compiled_contract in &compiled_contracts {
for contract_function in &compiled_contract.functions {
println!(
"Compiled ACIR for {}::{} (unoptimized):",

Check warning on line 201 in crates/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
compiled_contract.name, contract_function.name
);
println!("{}", contract_function.bytecode);
Expand Down