diff --git a/wasm/src/programs/manager/deploy.rs b/wasm/src/programs/manager/deploy.rs index 3f3ffda12..4412e267c 100644 --- a/wasm/src/programs/manager/deploy.rs +++ b/wasm/src/programs/manager/deploy.rs @@ -16,12 +16,7 @@ use super::*; -use crate::{ - log, - PrivateKey, - RecordPlaintext, - Transaction, -}; +use crate::{log, PrivateKey, RecordPlaintext, Transaction}; use js_sys::Object; @@ -66,15 +61,17 @@ impl ProgramManager { deploy.check_fee(fee_microcredits)?; - let (state, fee) = state.execute_fee( - deploy.execution_id()?, - url, - private_key.clone(), - fee_microcredits, - fee_record, - fee_proving_key, - fee_verifying_key, - ).await?; + let (state, fee) = state + .execute_fee( + deploy.execution_id()?, + url, + private_key.clone(), + fee_microcredits, + fee_record, + fee_proving_key, + fee_verifying_key, + ) + .await?; state.deploy_transaction(deploy, fee).await } diff --git a/wasm/src/programs/manager/execute.rs b/wasm/src/programs/manager/execute.rs index 375480f34..43d691419 100644 --- a/wasm/src/programs/manager/execute.rs +++ b/wasm/src/programs/manager/execute.rs @@ -67,18 +67,12 @@ impl ProgramManager { let state = ProgramState::new(program, imports).await?; - let (state, execute) = state.execute_program( - function, - inputs, - private_key.clone(), - proving_key, - verifying_key, - ).await?; + let (state, execute) = + state.execute_program(function, inputs, private_key.clone(), proving_key, verifying_key).await?; if prove_execution { let (state, execution) = state.prove_execution(execute, "https://vm.aleo.org/api".to_string()).await?; state.prove_response(execution, cache) - } else { state.execute_response(execute, cache) } @@ -129,25 +123,22 @@ impl ProgramManager { let state = ProgramState::new(program, imports).await?; - let (state, execute) = state.execute_program( - function, - inputs, - private_key.clone(), - proving_key, - verifying_key, - ).await?; + let (state, execute) = + state.execute_program(function, inputs, private_key.clone(), proving_key, verifying_key).await?; let (state, execution) = state.prove_execution(execute, url.clone()).await?; - let (_state, fee) = state.execute_fee( - execution.execution_id()?, - url, - private_key.clone(), - fee_microcredits, - fee_record, - fee_proving_key, - fee_verifying_key, - ).await?; + let (_state, fee) = state + .execute_fee( + execution.execution_id()?, + url, + private_key.clone(), + fee_microcredits, + fee_record, + fee_proving_key, + fee_verifying_key, + ) + .await?; execution.into_transaction(Some(fee)).await } @@ -190,13 +181,8 @@ impl ProgramManager { let state = ProgramState::new(program, imports).await?; - let (state, execute) = state.execute_program( - function, - inputs, - private_key.clone(), - proving_key, - verifying_key, - ).await?; + let (state, execute) = + state.execute_program(function, inputs, private_key.clone(), proving_key, verifying_key).await?; let (state, execution) = state.prove_execution(execute, url).await?; diff --git a/wasm/src/programs/manager/join.rs b/wasm/src/programs/manager/join.rs index fe3fdc400..ff0e9db76 100644 --- a/wasm/src/programs/manager/join.rs +++ b/wasm/src/programs/manager/join.rs @@ -16,13 +16,7 @@ use super::*; -use crate::{ - log, - types::{ProgramNative}, - PrivateKey, - RecordPlaintext, - Transaction, -}; +use crate::{log, types::ProgramNative, PrivateKey, RecordPlaintext, Transaction}; #[wasm_bindgen] impl ProgramManager { @@ -61,38 +55,30 @@ impl ProgramManager { let state = ProgramState::new(program, None).await?; - let (state, fee_record, join_proving_key, join_verifying_key) = state.insert_proving_keys( - fee_record, - join_proving_key, - join_verifying_key, - ).await?; + let (state, fee_record, join_proving_key, join_verifying_key) = + state.insert_proving_keys(fee_record, join_proving_key, join_verifying_key).await?; - let inputs = vec![ - record_1.to_string(), - record_2.to_string(), - ]; + let inputs = vec![record_1.to_string(), record_2.to_string()]; - let (state, mut execute) = state.execute_program( - "join".to_string(), - inputs, - private_key.clone(), - join_proving_key, - join_verifying_key, - ).await?; + let (state, mut execute) = state + .execute_program("join".to_string(), inputs, private_key.clone(), join_proving_key, join_verifying_key) + .await?; execute.set_locator("credits.aleo/join".to_string()); let (state, execution) = state.prove_execution(execute, url.clone()).await?; - let (_state, fee) = state.execute_fee( - execution.execution_id()?, - url, - private_key.clone(), - fee_microcredits, - fee_record, - fee_proving_key, - fee_verifying_key, - ).await?; + let (_state, fee) = state + .execute_fee( + execution.execution_id()?, + url, + private_key.clone(), + fee_microcredits, + fee_record, + fee_proving_key, + fee_verifying_key, + ) + .await?; execution.into_transaction(Some(fee)).await } diff --git a/wasm/src/programs/manager/mod.rs b/wasm/src/programs/manager/mod.rs index 345f8d8e4..44a0b3786 100644 --- a/wasm/src/programs/manager/mod.rs +++ b/wasm/src/programs/manager/mod.rs @@ -30,6 +30,7 @@ pub mod transfer; pub use transfer::*; use crate::{ + log, types::{ cost_in_microcredits, deployment_cost, @@ -49,6 +50,7 @@ use crate::{ TransactionNative, VerifyingKeyNative, }, + ExecutionResponse, KeyPair, PrivateKey, ProvingKey, @@ -56,22 +58,17 @@ use crate::{ RecordPlaintextNative, Transaction, VerifyingKey, - ExecutionResponse, - log, }; +use snarkvm_ledger_block::{Deployment, Fee}; use snarkvm_synthesizer::Trace; -use snarkvm_ledger_block::{Fee, Deployment}; -use js_sys::{Object, Array, Reflect}; -use rand::{rngs::StdRng, SeedableRng}; use core::ops::Add; -use std::future::Future; -use std::str::FromStr; -use std::collections::HashMap; +use js_sys::{Array, Object, Reflect}; +use rand::{rngs::StdRng, SeedableRng}; +use std::{collections::HashMap, future::Future, str::FromStr}; use wasm_bindgen::prelude::wasm_bindgen; - /// Converts a JS object into a `HashMap`. fn get_imports(imports: Option) -> Result>, String> { if let Some(imports) = imports { @@ -87,19 +84,16 @@ fn get_imports(imports: Option) -> Result } Ok(Some(hash)) - } else { Ok(None) } } - pub(crate) struct ExecuteFee { fee: Fee, private_key: PrivateKey, } - pub(crate) struct ProveExecution { execute: ExecuteProgram, execution: Execution, @@ -121,7 +115,6 @@ impl ProveExecution { } } - pub(crate) struct Deploy { deployment: Deployment, minimum_deployment_cost: u64, @@ -150,7 +143,6 @@ impl Deploy { } } - pub(crate) struct ExecuteProgram { locator: String, response: Response, @@ -163,22 +155,20 @@ impl ExecuteProgram { } } - pub(crate) struct ProgramState { rng: StdRng, process: ProcessNative, program: ProgramNative, } - impl ProgramState { /// Converts a JS array of strings into a `Vec`. pub(crate) fn get_inputs(inputs: Array) -> Result, String> { - inputs.iter() + inputs + .iter() .map(|input| { if let Some(input) = input.as_string() { Ok(input) - } else { Err("Invalid input - all inputs must be a string specifying the type".to_string()) } @@ -186,11 +176,7 @@ impl ProgramState { .collect() } - - pub(crate) fn new( - program_string: String, - imports: Option, - ) -> impl Future> { + pub(crate) fn new(program_string: String, imports: Option) -> impl Future> { let imports = get_imports(imports); crate::thread_pool::spawn(move || { @@ -210,26 +196,21 @@ impl ProgramState { }) } - /// Check if a process contains a keypair for a specific function - fn contains_key( - &self, - program_id: &ProgramIDNative, - function_id: &IdentifierNative, - ) -> bool { + fn contains_key(&self, program_id: &ProgramIDNative, function_id: &IdentifierNative) -> bool { self.process.get_stack(program_id).map_or_else( |_| false, |stack| stack.contains_proving_key(function_id) && stack.contains_verifying_key(function_id), ) } - pub(crate) fn insert_proving_keys( self, fee_record: Option, fee_proving_key: Option, fee_verifying_key: Option, - ) -> impl Future, Option, Option), String>> { + ) -> impl Future, Option, Option), String>> + { crate::thread_pool::spawn(move || { let fee_identifier = if fee_record.is_some() { IdentifierNative::from_str("fee_private").map_err(|e| e.to_string())? @@ -239,7 +220,8 @@ impl ProgramState { let stack = self.process.get_stack("credits.aleo").map_err(|e| e.to_string())?; - if !stack.contains_proving_key(&fee_identifier) && fee_proving_key.is_some() && fee_verifying_key.is_some() { + if !stack.contains_proving_key(&fee_identifier) && fee_proving_key.is_some() && fee_verifying_key.is_some() + { let fee_proving_key = fee_proving_key.clone().unwrap(); let fee_verifying_key = fee_verifying_key.clone().unwrap(); stack @@ -254,7 +236,6 @@ impl ProgramState { }) } - pub(crate) fn execute_program( mut self, function_id: String, @@ -264,8 +245,7 @@ impl ProgramState { verifying_key: Option, ) -> impl Future> { crate::thread_pool::spawn(move || { - if (proving_key.is_some() && verifying_key.is_none()) || - (proving_key.is_none() && verifying_key.is_some()) + if (proving_key.is_some() && verifying_key.is_none()) || (proving_key.is_none() && verifying_key.is_some()) { return Err( "If specifying a key for a program execution, both the proving and verifying key must be specified" @@ -294,38 +274,35 @@ impl ProgramState { if let Some(proving_key) = proving_key { if self.contains_key(id, &function_name) { - log(&format!("Proving & verifying keys were specified for {program_id} - {function_name:?} but a key already exists in the cache. Using cached keys")); - + log(&format!( + "Proving & verifying keys were specified for {program_id} - {function_name:?} but a key already exists in the cache. Using cached keys" + )); } else { - log(&format!("Inserting externally provided proving and verifying keys for {program_id} - {function_name:?}")); + log(&format!( + "Inserting externally provided proving and verifying keys for {program_id} - {function_name:?}" + )); self.process .insert_proving_key(id, &function_name, ProvingKeyNative::from(proving_key)) .map_err(|e| e.to_string())?; if let Some(verifying_key) = verifying_key { - self.process.insert_verifying_key(id, &function_name, VerifyingKeyNative::from(verifying_key)) + self.process + .insert_verifying_key(id, &function_name, VerifyingKeyNative::from(verifying_key)) .map_err(|e| e.to_string())?; } } } - log("Creating authorization"); - let authorization = self.process - .authorize::( - &private_key, - id, - function_name, - inputs.into_iter(), - &mut self.rng, - ) + let authorization = self + .process + .authorize::(&private_key, id, function_name, inputs.into_iter(), &mut self.rng) .map_err(|err| err.to_string())?; log("Executing program"); - let (response, trace) = self.process - .execute::(authorization) - .map_err(|err| err.to_string())?; + let (response, trace) = + self.process.execute::(authorization).map_err(|err| err.to_string())?; let locator = program_id.add("/").add(&function_id); @@ -333,7 +310,6 @@ impl ProgramState { }) } - pub(crate) fn prove_execution( mut self, mut execute: ExecuteProgram, @@ -346,26 +322,26 @@ impl ProgramState { crate::thread_pool::spawn(move || { log("Proving execution"); - let execution = execute.trace.prove_execution::(&execute.locator, &mut self.rng) + let execution = execute + .trace + .prove_execution::(&execute.locator, &mut self.rng) .map_err(|e| e.to_string())?; // Verify the execution - self.process.verify_execution(&execution) - .map_err(|err| err.to_string())?; + self.process.verify_execution(&execution).map_err(|err| err.to_string())?; Ok((self, ProveExecution { execution, execute })) - }).await + }) + .await } } - pub(crate) fn execute_response(self, execute: ExecuteProgram, cache: bool) -> Result { let process = if cache { Some(self.process) } else { None }; Ok(ExecutionResponse::from((execute.response, process))) } - pub(crate) fn prove_response(self, prove: ProveExecution, cache: bool) -> Result { let ProveExecution { execute: ExecuteProgram { response, .. }, execution } = prove; @@ -374,7 +350,6 @@ impl ProgramState { Ok(ExecutionResponse::from((response, execution, process))) } - pub(crate) fn estimate_fee(self, execution: ProveExecution) -> impl Future> { crate::thread_pool::spawn(move || { // Get the storage cost in bytes for the program execution @@ -407,7 +382,6 @@ impl ProgramState { }) } - pub(crate) fn execute_fee( mut self, execution_id: Field, @@ -483,21 +457,22 @@ impl ProgramState { trace.prepare_async(query).await.map_err(|err| err.to_string())?; crate::thread_pool::spawn(move || { - let fee = trace.prove_fee::(&mut state.rng).map_err(|e|e.to_string())?; + let fee = trace.prove_fee::(&mut state.rng).map_err(|e| e.to_string())?; log("Verifying fee execution"); state.process.verify_fee(&fee, execution_id).map_err(|e| e.to_string())?; Ok((state, ExecuteFee { fee, private_key })) - }).await + }) + .await } } - pub(crate) fn deploy(mut self) -> impl Future> { crate::thread_pool::spawn(move || { log("Creating deployment"); - let deployment = self.process.deploy::(&self.program, &mut self.rng).map_err(|err| err.to_string())?; + let deployment = + self.process.deploy::(&self.program, &mut self.rng).map_err(|err| err.to_string())?; if deployment.program().functions().is_empty() { return Err("Attempted to create an empty transaction deployment".to_string()); @@ -511,7 +486,6 @@ impl ProgramState { }) } - pub(crate) fn deploy_transaction( mut self, deploy: Deploy, @@ -531,14 +505,12 @@ impl ProgramState { log("Creating deployment transaction"); Ok(Transaction::from( - TransactionNative::from_deployment(owner, deploy.deployment, fee.fee) - .map_err(|err| err.to_string())?, + TransactionNative::from_deployment(owner, deploy.deployment, fee.fee).map_err(|err| err.to_string())?, )) }) } } - #[wasm_bindgen] #[derive(Clone)] pub struct ProgramManager; diff --git a/wasm/src/programs/manager/split.rs b/wasm/src/programs/manager/split.rs index a7de6af43..a9bbfff50 100644 --- a/wasm/src/programs/manager/split.rs +++ b/wasm/src/programs/manager/split.rs @@ -16,15 +16,9 @@ use super::*; -use crate::{ - log, - types::{ProgramNative}, - PrivateKey, - RecordPlaintext, - Transaction, -}; +use crate::{log, types::ProgramNative, PrivateKey, RecordPlaintext, Transaction}; -use std::{ops::Add}; +use std::ops::Add; #[wasm_bindgen] impl ProgramManager { @@ -55,18 +49,11 @@ impl ProgramManager { let state = ProgramState::new(program, None).await?; - let inputs = vec![ - amount_record.to_string(), - amount_microcredits.to_string().add("u64"), - ]; + let inputs = vec![amount_record.to_string(), amount_microcredits.to_string().add("u64")]; - let (state, mut execute) = state.execute_program( - "split".to_string(), - inputs, - private_key.clone(), - split_proving_key, - split_verifying_key, - ).await?; + let (state, mut execute) = state + .execute_program("split".to_string(), inputs, private_key.clone(), split_proving_key, split_verifying_key) + .await?; execute.set_locator("credits.aleo/split".to_string()); diff --git a/wasm/src/programs/manager/transfer.rs b/wasm/src/programs/manager/transfer.rs index 986b0e76f..a9b07104d 100644 --- a/wasm/src/programs/manager/transfer.rs +++ b/wasm/src/programs/manager/transfer.rs @@ -16,15 +16,9 @@ use super::*; -use crate::{ - log, - types::{ProgramNative}, - PrivateKey, - RecordPlaintext, - Transaction, -}; +use crate::{log, types::ProgramNative, PrivateKey, RecordPlaintext, Transaction}; -use std::{ops::Add}; +use std::ops::Add; #[wasm_bindgen] impl ProgramManager { @@ -111,33 +105,34 @@ impl ProgramManager { let state = ProgramState::new(program, None).await?; - let (state, fee_record, fee_proving_key, fee_verifying_key) = state.insert_proving_keys( - fee_record, - fee_proving_key, - fee_verifying_key, - ).await?; + let (state, fee_record, fee_proving_key, fee_verifying_key) = + state.insert_proving_keys(fee_record, fee_proving_key, fee_verifying_key).await?; - let (state, mut execute) = state.execute_program( - transfer_type.to_string(), - inputs, - private_key.clone(), - transfer_proving_key, - transfer_verifying_key, - ).await?; + let (state, mut execute) = state + .execute_program( + transfer_type.to_string(), + inputs, + private_key.clone(), + transfer_proving_key, + transfer_verifying_key, + ) + .await?; execute.set_locator("credits.aleo/transfer".to_string()); let (state, execution) = state.prove_execution(execute, url.clone()).await?; - let (_state, fee) = state.execute_fee( - execution.execution_id()?, - url, - private_key.clone(), - fee_microcredits, - fee_record, - fee_proving_key, - fee_verifying_key, - ).await?; + let (_state, fee) = state + .execute_fee( + execution.execution_id()?, + url, + private_key.clone(), + fee_microcredits, + fee_record, + fee_proving_key, + fee_verifying_key, + ) + .await?; execution.into_transaction(Some(fee)).await } diff --git a/wasm/src/thread_pool/mod.rs b/wasm/src/thread_pool/mod.rs index 96a95a1df..98d45db45 100644 --- a/wasm/src/thread_pool/mod.rs +++ b/wasm/src/thread_pool/mod.rs @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Aleo SDK library. If not, see . -use futures::future::try_join_all; -use futures::channel::oneshot; +use futures::{channel::oneshot, future::try_join_all}; use rayon::ThreadBuilder; use spmc::{channel, Receiver, Sender}; use std::future::Future; @@ -80,7 +79,6 @@ extern "C" { fn stop_timer(timer: f64); } - /// Runs a function on the Rayon thread-pool. /// /// When the function returns, the Future will resolve @@ -91,9 +89,10 @@ extern "C" { /// This will keep the NodeJS process alive until the /// Future is resolved. pub fn spawn(f: F) -> impl Future - where A: Send + 'static, - F: FnOnce() -> A + Send + 'static { - +where + A: Send + 'static, + F: FnOnce() -> A + Send + 'static, +{ // This is necessary in order to stop the Node process // from exiting while the spawned task is running. struct Timer(f64); @@ -119,7 +118,6 @@ pub fn spawn(f: F) -> impl Future } } - async fn spawn_workers(url: web_sys::Url, num_threads: usize) -> Result, JsValue> { let module = wasm_bindgen::module(); let memory = wasm_bindgen::memory();