diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs index 194cab8..f80de89 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/mod.rs @@ -5,6 +5,8 @@ use core::{ visibility::AccountVisibility, }; +use super::sequencer::error::Error; + pub mod transfer_deshielded; pub mod transfer_private; pub mod transfer_public; @@ -32,10 +34,11 @@ impl MockedClient { visibilities: &[AccountVisibility], commitment_tree_root: [u32; 8], sequencer: &mut MockedSequencer, - ) -> Result, nssa::Error> { + ) -> Result, Error> { // Execute and generate proof of the outer program let (receipt, private_outputs) = - nssa::execute_offchain::

(input_accounts, instruction_data, visibilities, commitment_tree_root)?; + nssa::execute_offchain::

(input_accounts, instruction_data, visibilities, commitment_tree_root) + .map_err(|_| Error::Generic)?; // Send proof to the sequencer sequencer.process_privacy_execution(receipt)?; diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs index e22f239..b86ba9d 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_deshielded.rs @@ -1,11 +1,10 @@ +use super::{MockedClient, MockedSequencer}; +use crate::mocked_components::sequencer::error::Error; use core::account::Account; use core::types::Address; use core::visibility::AccountVisibility; - use nssa::program::TransferProgram; -use super::{MockedClient, MockedSequencer}; - impl MockedClient { /// A deshielded transaction of the Transfer program. /// All of this is executed locally by the sender @@ -15,7 +14,7 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result { + ) -> Result { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Compute authenticaton path for the input private account diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs index d96c29f..a1a8d31 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_private.rs @@ -1,11 +1,10 @@ +use super::{MockedClient, MockedSequencer}; +use crate::mocked_components::sequencer::error::Error; use core::account::Account; use core::types::Address; use core::visibility::AccountVisibility; - use nssa::program::TransferProgram; -use super::{MockedClient, MockedSequencer}; - impl MockedClient { /// A private execution of the Transfer program // All of this is executed locally by the sender @@ -15,7 +14,7 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result<[Account; 2], nssa::Error> { + ) -> Result<[Account; 2], Error> { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Compute authenticaton path for the input private account diff --git a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs index ad869a5..b3ef569 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/client/transfer_shielded.rs @@ -1,11 +1,10 @@ +use super::{MockedClient, MockedSequencer}; +use crate::mocked_components::sequencer::error::Error; use core::account::Account; use core::types::Address; use core::visibility::AccountVisibility; - use nssa::program::TransferProgram; -use super::{MockedClient, MockedSequencer}; - impl MockedClient { /// A shielded execution of the Transfer program // All of this is executed locally by the sender @@ -14,14 +13,12 @@ impl MockedClient { to_address: &Address, balance_to_move: u128, sequencer: &mut MockedSequencer, - ) -> Result { + ) -> Result { // Fetch commitment tree root from the sequencer let commitment_tree_root = sequencer.get_commitment_tree_root(); // Fetch sender account from the sequencer - let from_account = sequencer - .get_account(&self.user_address()) - .ok_or(nssa::Error::Generic)?; + let from_account = sequencer.get_account(&self.user_address()).ok_or(Error::Generic)?; // Create a new default private account for the receiver let to_account = Self::fresh_account_for_mint(*to_address); diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/error.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/error.rs new file mode 100644 index 0000000..f1d7fa5 --- /dev/null +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/error.rs @@ -0,0 +1,15 @@ +#[derive(Debug)] +pub enum Error { + /// For simplicity, this POC uses a generic error + Generic, +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Generic => write!(f, "An unexpected error occurred"), + } + } +} + +impl std::error::Error for Error {} diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs index 9931d7e..ce950f0 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/mod.rs @@ -9,6 +9,7 @@ use sparse_merkle_tree::SparseMerkleTree; use crate::mocked_components::USER_CLIENTS; +pub mod error; pub mod process_privacy_execution; pub mod process_public_execution; diff --git a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs index c4dcdff..cf90cba 100644 --- a/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs +++ b/risc0-selective-privacy-poc/examples/mocked_components/sequencer/process_privacy_execution.rs @@ -2,31 +2,32 @@ use core::types::PrivacyExecutionOutput; use risc0_zkvm::Receipt; +use super::error::Error; use super::MockedSequencer; impl MockedSequencer { /// Processes a privacy execution request. /// Verifies the proof of the privacy execution and updates the state of the chain. - pub fn process_privacy_execution(&mut self, receipt: Receipt) -> Result<(), nssa::Error> { + pub fn process_privacy_execution(&mut self, receipt: Receipt) -> Result<(), Error> { // Parse the output of the "outer" program let output: PrivacyExecutionOutput = receipt.journal.decode().unwrap(); // Reject in case the root used in the privacy execution is not the current root. if output.commitment_tree_root != self.get_commitment_tree_root() { - return Err(nssa::Error::Generic); + return Err(Error::Generic); } // Reject in case the number of accounts pre states is different from the post states if output.public_accounts_pre.len() != output.public_accounts_post.len() { - return Err(nssa::Error::Generic); + return Err(Error::Generic); } // Reject if the states of the public input accounts used in the inner execution do not // coincide with the on-chain state. for account in output.public_accounts_pre.iter() { - let current_account = self.get_account(&account.address).ok_or(nssa::Error::Generic)?; + let current_account = self.get_account(&account.address).ok_or(Error::Generic)?; if ¤t_account != account { - return Err(nssa::Error::Generic); + return Err(Error::Generic); } } @@ -36,7 +37,7 @@ impl MockedSequencer { .iter() .any(|nullifier| self.nullifier_set.contains(nullifier)) { - return Err(nssa::Error::Generic); + return Err(Error::Generic); } // Reject if the commitments have already been seen. @@ -45,7 +46,7 @@ impl MockedSequencer { .iter() .any(|commitment| self.commitment_tree.values().contains(commitment)) { - return Err(nssa::Error::Generic); + return Err(Error::Generic); } // Verify the proof of the privacy execution. @@ -55,7 +56,7 @@ impl MockedSequencer { // - The given nullifiers correctly correspond to commitments that currently belong to // the commitment tree. // - The given commitments are correctly computed from valid accounts. - nssa::verify_privacy_execution(receipt)?; + nssa::verify_privacy_execution(receipt).map_err(|_| Error::Generic)?; // At this point the privacy execution is considered valid. //