From 1d49d61d81a473af6f1e74a08c10d1e4563005a0 Mon Sep 17 00:00:00 2001 From: Koby Date: Tue, 21 Mar 2023 23:43:05 +0100 Subject: [PATCH 1/7] feat(wasm): Default arguments for compile --- Cargo.lock | 13 ++++++++++ crates/fm/src/file_reader.rs | 3 ++- crates/wasm/Cargo.toml | 3 ++- crates/wasm/build-wasm | 2 +- crates/wasm/src/lib.rs | 48 ++++++++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3152ba4ca8..b7d3789d171 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2167,10 +2167,12 @@ dependencies = [ "build-data", "console_error_panic_hook", "gloo-utils", + "log", "noirc_driver", "noirc_frontend", "serde", "wasm-bindgen", + "wasm-logger", ] [[package]] @@ -3682,6 +3684,17 @@ dependencies = [ "leb128", ] +[[package]] +name = "wasm-logger" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "074649a66bb306c8f2068c9016395fa65d8e08d2affcbf95acf3c24c3ab19718" +dependencies = [ + "log", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasmer" version = "2.3.0" diff --git a/crates/fm/src/file_reader.rs b/crates/fm/src/file_reader.rs index 6b45d990633..f0a5fe625aa 100644 --- a/crates/fm/src/file_reader.rs +++ b/crates/fm/src/file_reader.rs @@ -26,7 +26,8 @@ cfg_if::cfg_if! { pub(crate) fn read_file_to_string(path_to_file: &Path) -> Result { use std::io::ErrorKind; - match StdLibAssets::get(path_to_file.to_str().unwrap()) { + let path_str = path_to_file.to_str().unwrap(); + match StdLibAssets::get(path_str) { Some(std_lib_asset) => { Ok(std::str::from_utf8(std_lib_asset.data.as_ref()).unwrap().to_string()) diff --git a/crates/wasm/Cargo.toml b/crates/wasm/Cargo.toml index 6f390ad46ae..bbcabe12e0a 100644 --- a/crates/wasm/Cargo.toml +++ b/crates/wasm/Cargo.toml @@ -17,7 +17,8 @@ noirc_driver.workspace = true noirc_frontend.workspace = true wasm-bindgen.workspace = true serde.workspace = true - +log = "0.4.17" +wasm-logger = "0.2.0" console_error_panic_hook = "0.1.7" gloo-utils = { version = "0.1", features = ["serde"] } diff --git a/crates/wasm/build-wasm b/crates/wasm/build-wasm index c7c0de14c7a..c7fd6ddeb3c 100755 --- a/crates/wasm/build-wasm +++ b/crates/wasm/build-wasm @@ -18,7 +18,7 @@ else VERSION_APPENDIX="-NOGIT" fi -jq -s '.[0] * .[1]' pkg/nodejs/package.json pkg/web/package.json | jq '.files = ["nodejs", "web", "package.json"]' | jq ".version += \"$VERSION_APPENDIX\"" | jq '.main = "./nodejs/" + .main | .module = "./web/" + .module | .types = "./web/" + .types | .peerDependencies = { "@noir-lang/noir-source-resolver": "1.0.0" }' | tee ./pkg/package.json +jq -s '.[0] * .[1]' pkg/nodejs/package.json pkg/web/package.json | jq '.files = ["nodejs", "web", "package.json"]' | jq ".version += \"$VERSION_APPENDIX\"" | jq '.main = "./nodejs/" + .main | .module = "./web/" + .module | .types = "./web/" + .types | .peerDependencies = { "@noir-lang/noir-source-resolver": "1.1.0" }' | tee ./pkg/package.json rm pkg/nodejs/package.json pkg/nodejs/README.md pkg/nodejs/.gitignore diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 7088080aac3..95b84d7715a 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -1,9 +1,9 @@ #![forbid(unsafe_code)] #![warn(unused_crate_dependencies, unused_extern_crates)] #![warn(unreachable_pub)] - use acvm::acir::circuit::Circuit; use gloo_utils::format::JsValueSerdeExt; +use log::debug; use noirc_driver::{CompileOptions, Driver}; use noirc_frontend::graph::{CrateName, CrateType}; use serde::{Deserialize, Serialize}; @@ -17,8 +17,14 @@ pub struct BuildInfo { dirty: &'static str, } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct WASMCompileOptions { + #[serde(default = "default_entry_point")] + entry_point: String, + + #[serde(default = "default_circuit_name")] + circuit_name: String, + // Compile each contract function used within the program #[serde(default = "bool::default")] contracts: bool, @@ -30,12 +36,31 @@ pub struct WASMCompileOptions { optional_dependencies_set: Vec, } +fn default_circuit_name() -> String { + String::from("main") +} + +fn default_entry_point() -> String { + String::from("main.nr") +} + +impl Default for WASMCompileOptions { + fn default() -> Self { + Self { + entry_point: default_entry_point(), + circuit_name: default_circuit_name(), + contracts: false, + compile_options: CompileOptions::default(), + optional_dependencies_set: vec![], + } + } +} + const BUILD_INFO: BuildInfo = BuildInfo { git_hash: env!("GIT_COMMIT"), version: env!("CARGO_PKG_VERSION"), dirty: env!("GIT_DIRTY"), }; - pub fn add_noir_lib(driver: &mut Driver, crate_name: &str) { let path_to_lib = PathBuf::from(&crate_name).join("lib.nr"); let library_crate = driver.create_non_local_crate(path_to_lib, CrateType::Library); @@ -46,10 +71,23 @@ pub fn add_noir_lib(driver: &mut Driver, crate_name: &str) { #[wasm_bindgen] pub fn compile(args: JsValue) -> JsValue { console_error_panic_hook::set_once(); - let options: WASMCompileOptions = JsValueSerdeExt::into_serde(&args).unwrap(); + wasm_logger::init(wasm_logger::Config::default()); + + let options: WASMCompileOptions = if args.is_undefined() || args.is_null() { + debug!("Initializing compiler with default values."); + WASMCompileOptions::default() + } else { + JsValueSerdeExt::into_serde(&args) + .unwrap_or_else(|_| panic!("Could not deserialize compile arguments")) + }; + + debug!("Conpiler configureation {:?}", &options); + // For now we default to plonk width = 3, though we can add it as a parameter let language = acvm::Language::PLONKCSat { width: 3 }; - let path = PathBuf::from("main.nr"); + + let path = PathBuf::from(&options.entry_point); + let mut driver = noirc_driver::Driver::new(&language); driver.create_local_crate(path, CrateType::Binary); From bcdfd526fe2b5339cfad487a00e9fcbdb70c45dc Mon Sep 17 00:00:00 2001 From: Koby Date: Wed, 22 Mar 2023 01:04:12 +0100 Subject: [PATCH 2/7] feat(log): Add level initialization --- crates/wasm/src/lib.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 95b84d7715a..d9b7297596a 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -3,11 +3,11 @@ #![warn(unreachable_pub)] use acvm::acir::circuit::Circuit; use gloo_utils::format::JsValueSerdeExt; -use log::debug; +use log::{debug, Level}; use noirc_driver::{CompileOptions, Driver}; use noirc_frontend::graph::{CrateName, CrateType}; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{path::PathBuf, str::FromStr}; use wasm_bindgen::prelude::*; #[derive(Serialize, Deserialize)] @@ -34,6 +34,13 @@ pub struct WASMCompileOptions { #[serde(default)] optional_dependencies_set: Vec, + + #[serde(default = "default_log_level")] + log_level: String, +} + +fn default_log_level() -> String { + String::from("info") } fn default_circuit_name() -> String { @@ -49,6 +56,7 @@ impl Default for WASMCompileOptions { Self { entry_point: default_entry_point(), circuit_name: default_circuit_name(), + log_level: default_log_level(), contracts: false, compile_options: CompileOptions::default(), optional_dependencies_set: vec![], @@ -56,6 +64,17 @@ impl Default for WASMCompileOptions { } } +#[wasm_bindgen] +pub fn init_log_level(level: String) { + // Set the static variable from Rust + use std::sync::Once; + static SET_HOOK: Once = Once::new(); + SET_HOOK.call_once(|| { + wasm_logger::init(wasm_logger::Config::new( + Level::from_str(&level).unwrap_or(Level::Error), + )); + }); +} const BUILD_INFO: BuildInfo = BuildInfo { git_hash: env!("GIT_COMMIT"), version: env!("CARGO_PKG_VERSION"), @@ -71,7 +90,6 @@ pub fn add_noir_lib(driver: &mut Driver, crate_name: &str) { #[wasm_bindgen] pub fn compile(args: JsValue) -> JsValue { console_error_panic_hook::set_once(); - wasm_logger::init(wasm_logger::Config::default()); let options: WASMCompileOptions = if args.is_undefined() || args.is_null() { debug!("Initializing compiler with default values."); @@ -81,7 +99,7 @@ pub fn compile(args: JsValue) -> JsValue { .unwrap_or_else(|_| panic!("Could not deserialize compile arguments")) }; - debug!("Conpiler configureation {:?}", &options); + debug!("Compiler configureation {:?}", &options); // For now we default to plonk width = 3, though we can add it as a parameter let language = acvm::Language::PLONKCSat { width: 3 }; From ed18082156264eccddf5e658c5d7371dfd141f70 Mon Sep 17 00:00:00 2001 From: Koby Date: Wed, 22 Mar 2023 11:39:29 +0100 Subject: [PATCH 3/7] fix(resolver): Bumps resolver module version --- crates/wasm/build-wasm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasm/build-wasm b/crates/wasm/build-wasm index c7fd6ddeb3c..0762d12fff3 100755 --- a/crates/wasm/build-wasm +++ b/crates/wasm/build-wasm @@ -18,7 +18,7 @@ else VERSION_APPENDIX="-NOGIT" fi -jq -s '.[0] * .[1]' pkg/nodejs/package.json pkg/web/package.json | jq '.files = ["nodejs", "web", "package.json"]' | jq ".version += \"$VERSION_APPENDIX\"" | jq '.main = "./nodejs/" + .main | .module = "./web/" + .module | .types = "./web/" + .types | .peerDependencies = { "@noir-lang/noir-source-resolver": "1.1.0" }' | tee ./pkg/package.json +jq -s '.[0] * .[1]' pkg/nodejs/package.json pkg/web/package.json | jq '.files = ["nodejs", "web", "package.json"]' | jq ".version += \"$VERSION_APPENDIX\"" | jq '.main = "./nodejs/" + .main | .module = "./web/" + .module | .types = "./web/" + .types | .peerDependencies = { "@noir-lang/noir-source-resolver": "1.1.1" }' | tee ./pkg/package.json rm pkg/nodejs/package.json pkg/nodejs/README.md pkg/nodejs/.gitignore From dabc90f41e129257e6ea59bcdd2ddad20c466819 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Thu, 23 Mar 2023 12:57:14 +0100 Subject: [PATCH 4/7] Update crates/wasm/src/lib.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- crates/wasm/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 041e0903fcf..a63e9a9662d 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -99,7 +99,7 @@ pub fn compile(args: JsValue) -> JsValue { .unwrap_or_else(|_| panic!("Could not deserialize compile arguments")) }; - debug!("Compiler configureation {:?}", &options); + debug!("Compiler configuration {:?}", &options); // For now we default to plonk width = 3, though we can add it as a parameter let language = acvm::Language::PLONKCSat { width: 3 }; From 778634e1a2f25fb0a5228e9a5c4ba1b0fcdfee3c Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Thu, 23 Mar 2023 13:00:15 +0100 Subject: [PATCH 5/7] Update crates/wasm/src/lib.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- crates/wasm/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index a63e9a9662d..dff1c43c0aa 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -68,11 +68,11 @@ impl Default for WASMCompileOptions { pub fn init_log_level(level: String) { // Set the static variable from Rust use std::sync::Once; + + let log_level = Level::from_str(&level).unwrap_or(Level::Error) static SET_HOOK: Once = Once::new(); SET_HOOK.call_once(|| { - wasm_logger::init(wasm_logger::Config::new( - Level::from_str(&level).unwrap_or(Level::Error), - )); + wasm_logger::init(wasm_logger::Config::new(log_level)); }); } const BUILD_INFO: BuildInfo = BuildInfo { From 3ffd843b862723949eb1b0f3d37515a991adb450 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Thu, 23 Mar 2023 13:01:38 +0100 Subject: [PATCH 6/7] Update crates/wasm/src/lib.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- crates/wasm/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index dff1c43c0aa..7142c229f04 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -103,11 +103,9 @@ pub fn compile(args: JsValue) -> JsValue { // For now we default to plonk width = 3, though we can add it as a parameter let language = acvm::Language::PLONKCSat { width: 3 }; - - let path = PathBuf::from(&options.entry_point); - let mut driver = noirc_driver::Driver::new(&language); + let path = PathBuf::from(&options.entry_point); driver.create_local_crate(path, CrateType::Binary); // We are always adding std lib implicitly. It comes bundled with binary. From f85ee3adf180a8405967878875521d1b8e260838 Mon Sep 17 00:00:00 2001 From: Koby Date: Thu, 23 Mar 2023 13:30:59 +0100 Subject: [PATCH 7/7] Adds usage of contract_id --- crates/wasm/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 7142c229f04..61212dbaacd 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -44,7 +44,7 @@ fn default_log_level() -> String { } fn default_circuit_name() -> String { - String::from("main") + String::from("contract") } fn default_entry_point() -> String { @@ -68,8 +68,8 @@ impl Default for WASMCompileOptions { pub fn init_log_level(level: String) { // Set the static variable from Rust use std::sync::Once; - - let log_level = Level::from_str(&level).unwrap_or(Level::Error) + + let log_level = Level::from_str(&level).unwrap_or(Level::Error); static SET_HOOK: Once = Once::new(); SET_HOOK.call_once(|| { wasm_logger::init(wasm_logger::Config::new(log_level)); @@ -126,8 +126,9 @@ pub fn compile(args: JsValue) -> JsValue { let collected_compiled_programs: Vec<_> = compiled_contracts .into_iter() .flat_map(|contract| { + let contract_id = format!("{}-{}", options.circuit_name, &contract.name); contract.functions.into_iter().map(move |(function, program)| { - let program_name = format!("{}-{}", &contract.name, function); + let program_name = format!("{}-{}", contract_id, function); (program_name, program) }) })