From bd8b4b8fa85cd95f788ec016fb364f84649c3da8 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Wed, 5 Feb 2025 12:24:09 +0200 Subject: [PATCH] fix: error propagation --- Cargo.lock | 16 +++++ Cargo.toml | 1 + common/Cargo.toml | 15 +++++ common/src/lib.rs | 67 +++++++++++++++++++++ node_core/Cargo.toml | 3 + node_core/src/lib.rs | 57 +++++++++--------- node_core/src/sequencer_client/json.rs | 8 --- node_core/src/sequencer_client/mod.rs | 31 +--------- node_rpc/Cargo.toml | 3 + node_rpc/src/process.rs | 82 +++++++++++++++++--------- node_rpc/src/types/err_rpc.rs | 19 +++++- sequencer_core/src/lib.rs | 10 ++-- zkvm/Cargo.toml | 3 + zkvm/src/lib.rs | 29 +-------- 14 files changed, 219 insertions(+), 125 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 00ae759..839cef3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -994,6 +994,19 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "common" +version = "0.1.0" +dependencies = [ + "anyhow", + "reqwest 0.11.27", + "risc0-zkvm", + "rpc_primitives", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "consensus" version = "0.1.0" @@ -2674,6 +2687,7 @@ dependencies = [ "actix-rt", "anyhow", "bincode", + "common", "elliptic-curve", "env_logger", "hex", @@ -2705,6 +2719,7 @@ dependencies = [ "actix-cors", "actix-web", "anyhow", + "common", "consensus", "env_logger", "futures", @@ -5408,6 +5423,7 @@ version = "0.1.0" dependencies = [ "accounts", "anyhow", + "common", "env_logger", "log", "risc0-zkvm", diff --git a/Cargo.toml b/Cargo.toml index e04fca5..548f216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ members = [ "node_core", "sequencer_core", "rpc_primitives", + "common", ] [workspace.dependencies] diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 0000000..c2cda7d --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "common" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow.workspace = true +thiserror.workspace = true +serde_json.workspace = true +serde.workspace = true +reqwest.workspace = true +risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-1.2" } + +[dependencies.rpc_primitives] +path = "../rpc_primitives" \ No newline at end of file diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 0000000..121c403 --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1,67 @@ +use rpc_primitives::errors::RpcError; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize)] +pub struct SequencerRpcError { + pub jsonrpc: String, + pub error: RpcError, + pub id: u64, +} + +#[derive(thiserror::Error, Debug)] +pub enum SequencerClientError { + #[error("HTTP error")] + HTTPError(reqwest::Error), + #[error("Serde error")] + SerdeError(serde_json::Error), + #[error("Internal error")] + InternalError(SequencerRpcError), +} + +impl From for SequencerClientError { + fn from(value: reqwest::Error) -> Self { + SequencerClientError::HTTPError(value) + } +} + +impl From for SequencerClientError { + fn from(value: serde_json::Error) -> Self { + SequencerClientError::SerdeError(value) + } +} + +impl From for SequencerClientError { + fn from(value: SequencerRpcError) -> Self { + SequencerClientError::InternalError(value) + } +} + +#[derive(Debug, thiserror::Error)] +pub enum ExecutionFailureKind { + #[error("Failed to write into builder err: {0:?}")] + WriteError(anyhow::Error), + #[error("Failed to build builder err: {0:?}")] + BuilderError(anyhow::Error), + #[error("Failed prove execution err: {0:?}")] + ProveError(anyhow::Error), + #[error("Failed to decode data from VM: {0:?}")] + DecodeError(#[from] risc0_zkvm::serde::Error), + #[error("Inputs amounts does not match outputs")] + AmountMismatchError, + #[error("Sequencer client error: {0:?}")] + SequencerClientError(#[from] SequencerClientError), +} + +impl ExecutionFailureKind { + pub fn write_error(err: anyhow::Error) -> Self { + Self::WriteError(err) + } + + pub fn builder_error(err: anyhow::Error) -> Self { + Self::BuilderError(err) + } + + pub fn prove_error(err: anyhow::Error) -> Self { + Self::ProveError(err) + } +} diff --git a/node_core/Cargo.toml b/node_core/Cargo.toml index fd6c25b..0cd711d 100644 --- a/node_core/Cargo.toml +++ b/node_core/Cargo.toml @@ -38,6 +38,9 @@ path = "../zkvm" [dependencies.rpc_primitives] path = "../rpc_primitives" +[dependencies.common] +path = "../common" + [dependencies.secp256k1-zkp] workspace = true features = ["std", "rand-std", "rand", "serde", "global-context"] diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index 68c3692..aa1b288 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -3,6 +3,7 @@ use std::sync::{ Arc, }; +use common::ExecutionFailureKind; use k256::elliptic_curve::group::GroupEncoding; use ::storage::transaction::{Transaction, TransactionPayload, TxKind}; @@ -23,7 +24,7 @@ use tokio::{sync::RwLock, task::JoinHandle}; use utxo::utxo_core::UTXO; use zkvm::{ prove_mint_utxo, prove_mint_utxo_multiple_assets, prove_send_utxo, prove_send_utxo_deshielded, - prove_send_utxo_multiple_assets_one_receiver, prove_send_utxo_shielded, ExecutionFailureKind, + prove_send_utxo_multiple_assets_one_receiver, prove_send_utxo_shielded, }; pub const BLOCK_GEN_DELAY_SECS: u64 = 20; @@ -145,11 +146,10 @@ impl NodeCore { pub async fn get_roots(&self) -> [[u8; 32]; 3] { let storage = self.storage.read().await; - //All roots are non-None after start of node main loop [ - storage.nullifier_store.curr_root.unwrap(), - storage.utxo_commitments_store.get_root().unwrap(), - storage.pub_tx_store.get_root().unwrap(), + storage.nullifier_store.curr_root.unwrap_or([0; 32]), + storage.utxo_commitments_store.get_root().unwrap_or([0; 32]), + storage.pub_tx_store.get_root().unwrap_or([0; 32]), ] } @@ -603,7 +603,7 @@ impl NodeCore { &self, acc: AccountAddress, amount: u128, - ) -> Result<(SendTxResponse, [u8; 32], [u8; 32])> { + ) -> Result<(SendTxResponse, [u8; 32], [u8; 32]), ExecutionFailureKind> { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -629,7 +629,7 @@ impl NodeCore { acc: AccountAddress, amount: u128, number_of_assets: usize, - ) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>)> { + ) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -656,7 +656,7 @@ impl NodeCore { &self, acc: AccountAddress, amount: u128, - ) -> Result { + ) -> Result { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -671,7 +671,7 @@ impl NodeCore { utxo: UTXO, comm_hash: [u8; 32], receivers: Vec<(u128, AccountAddress)>, - ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>)> { + ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>), ExecutionFailureKind> { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -697,7 +697,7 @@ impl NodeCore { comm_hashes: Vec<[u8; 32]>, number_to_send: usize, receiver: AccountAddress, - ) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>)> { + ) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -723,7 +723,7 @@ impl NodeCore { acc: AccountAddress, amount: u64, receivers: Vec<(u128, AccountAddress)>, - ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>)> { + ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>), ExecutionFailureKind> { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -748,7 +748,7 @@ impl NodeCore { utxo: UTXO, comm_gen_hash: [u8; 32], receivers: Vec<(u128, AccountAddress)>, - ) -> Result { + ) -> Result { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -769,7 +769,7 @@ impl NodeCore { &mut self, acc_addr: AccountAddress, amount: u128, - ) -> Result<(UTXO, [u8; 32])> { + ) -> Result<(UTXO, [u8; 32]), ExecutionFailureKind> { let (resp, new_utxo_hash, comm_gen_hash) = self.send_private_mint_tx(acc_addr, amount).await?; info!("Response for mint private is {resp:?}"); @@ -808,7 +808,7 @@ impl NodeCore { acc_addr: AccountAddress, amount: u128, number_of_assets: usize, - ) -> Result<(Vec, Vec<[u8; 32]>)> { + ) -> Result<(Vec, Vec<[u8; 32]>), ExecutionFailureKind> { let (resp, new_utxo_hashes, comm_gen_hashes) = self .send_private_mint_multiple_assets_tx(acc_addr, amount, number_of_assets) .await?; @@ -858,7 +858,7 @@ impl NodeCore { acc_addr_rec: AccountAddress, utxo: UTXO, comm_gen_hash: [u8; 32], - ) -> Result<()> { + ) -> Result<(), ExecutionFailureKind> { let amount = utxo.amount; let old_balance = { @@ -904,7 +904,7 @@ impl NodeCore { &mut self, acc_addr: AccountAddress, amount: u128, - ) -> Result<()> { + ) -> Result<(), ExecutionFailureKind> { let old_balance = { let acc_map_read_guard = self.storage.read().await; @@ -946,7 +946,7 @@ impl NodeCore { acc_addr_sender: AccountAddress, acc_addr_rec: AccountAddress, amount: u128, - ) -> Result { + ) -> Result { let (resp, new_utxo_hashes) = self .send_shielded_send_tx(acc_addr_sender, amount as u64, vec![(amount, acc_addr_rec)]) .await?; @@ -988,7 +988,7 @@ impl NodeCore { acc_addr_rec: AccountAddress, utxo: UTXO, comm_gen_hash: [u8; 32], - ) -> Result { + ) -> Result { let amount = utxo.amount; let (resp, new_utxo_hashes) = self @@ -1035,7 +1035,7 @@ impl NodeCore { utxos: Vec, comm_gen_hashes: Vec<[u8; 32]>, number_to_send: usize, - ) -> Result<()> { + ) -> Result<(), ExecutionFailureKind> { let (resp, new_utxo_hashes_rec, new_utxo_hashes_not_sp) = self .send_private_multiple_assets_send_tx( utxos, @@ -1201,7 +1201,8 @@ impl NodeCore { comm_hash: [u8; 32], receivers: Vec<(u128, AccountAddress)>, visibility_list: [bool; 3], - ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>, Vec<[u8; 32]>)> { + ) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>, Vec<[u8; 32]>), ExecutionFailureKind> + { //Considering proof time, needs to be done before proof let tx_roots = self.get_roots().await; @@ -1230,7 +1231,7 @@ impl NodeCore { utxo: UTXO, comm_gen_hash: [u8; 32], visibility_list: [bool; 3], - ) -> Result<(Vec, Vec<[u8; 32]>)> { + ) -> Result<(Vec, Vec<[u8; 32]>), ExecutionFailureKind> { let (resp, new_utxo_hashes, commitments_hashes) = self .send_split_tx( utxo.clone(), @@ -1283,7 +1284,7 @@ impl NodeCore { } ///Mint utxo, make it public - pub async fn subscenario_1(&mut self) -> Result<()> { + pub async fn subscenario_1(&mut self) -> Result<(), ExecutionFailureKind> { let acc_addr = self.create_new_account().await; let (new_utxo, comm_gen_hash) = self.operate_account_mint_private(acc_addr, 100).await?; @@ -1300,7 +1301,7 @@ impl NodeCore { } ///Deposit balance, make it private - pub async fn subscenario_2(&mut self) -> Result<()> { + pub async fn subscenario_2(&mut self) -> Result<(), ExecutionFailureKind> { let acc_addr = self.create_new_account().await; self.operate_account_deposit_public(acc_addr, 100).await?; @@ -1312,7 +1313,7 @@ impl NodeCore { } ///Mint utxo, privately send it to another user - pub async fn subscenario_3(&mut self) -> Result<()> { + pub async fn subscenario_3(&mut self) -> Result<(), ExecutionFailureKind> { let acc_addr = self.create_new_account().await; let acc_addr_rec = self.create_new_account().await; @@ -1325,7 +1326,7 @@ impl NodeCore { } ///Deposit balance, shielded send it to another user - pub async fn subscenario_4(&mut self) -> Result<()> { + pub async fn subscenario_4(&mut self) -> Result<(), ExecutionFailureKind> { let acc_addr = self.create_new_account().await; let acc_addr_rec = self.create_new_account().await; @@ -1338,7 +1339,7 @@ impl NodeCore { } ///Mint utxo, deshielded send it to another user - pub async fn subscenario_5(&mut self) -> Result<()> { + pub async fn subscenario_5(&mut self) -> Result<(), ExecutionFailureKind> { let acc_addr = self.create_new_account().await; let acc_addr_rec = self.create_new_account().await; @@ -1364,7 +1365,7 @@ impl NodeCore { &mut self, visibility_list: [bool; 3], publication_index: usize, - ) -> Result<()> { + ) -> Result<(), ExecutionFailureKind> { let acc_addr_sender = self.create_new_account().await; let acc_addr_rec_1 = self.create_new_account().await; @@ -1402,7 +1403,7 @@ impl NodeCore { &mut self, number_of_assets: usize, number_to_send: usize, - ) -> Result<()> { + ) -> Result<(), ExecutionFailureKind> { let acc_addr_sender = self.create_new_account().await; let acc_addr_receiver = self.create_new_account().await; diff --git a/node_core/src/sequencer_client/json.rs b/node_core/src/sequencer_client/json.rs index 7683ea8..c1edfe7 100644 --- a/node_core/src/sequencer_client/json.rs +++ b/node_core/src/sequencer_client/json.rs @@ -1,4 +1,3 @@ -use rpc_primitives::errors::RpcError; use serde::{Deserialize, Serialize}; use storage::{block::Block, transaction::Transaction}; @@ -77,10 +76,3 @@ pub struct SequencerRpcResponse { pub result: serde_json::Value, pub id: u64, } - -#[derive(Debug, Clone, Deserialize)] -pub struct SequencerRpcError { - pub jsonrpc: String, - pub error: RpcError, - pub id: u64, -} diff --git a/node_core/src/sequencer_client/mod.rs b/node_core/src/sequencer_client/mod.rs index 6a65b9e..095978e 100644 --- a/node_core/src/sequencer_client/mod.rs +++ b/node_core/src/sequencer_client/mod.rs @@ -1,9 +1,10 @@ use accounts::account_core::Account; use anyhow::Result; +use common::{SequencerClientError, SequencerRpcError}; use json::{ GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse, RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse, - SequencerRpcError, SequencerRpcRequest, SequencerRpcResponse, + SequencerRpcRequest, SequencerRpcResponse, }; use k256::elliptic_curve::group::GroupEncoding; use reqwest::Client; @@ -20,34 +21,6 @@ pub struct SequencerClient { pub config: NodeConfig, } -#[derive(thiserror::Error, Debug)] -pub enum SequencerClientError { - #[error("HTTP error")] - HTTPError(reqwest::Error), - #[error("Serde error")] - SerdeError(serde_json::Error), - #[error("Internal error")] - InternalError(SequencerRpcError), -} - -impl From for SequencerClientError { - fn from(value: reqwest::Error) -> Self { - SequencerClientError::HTTPError(value) - } -} - -impl From for SequencerClientError { - fn from(value: serde_json::Error) -> Self { - SequencerClientError::SerdeError(value) - } -} - -impl From for SequencerClientError { - fn from(value: SequencerRpcError) -> Self { - SequencerClientError::InternalError(value) - } -} - impl SequencerClient { pub fn new(config: NodeConfig) -> Result { Ok(Self { diff --git a/node_rpc/Cargo.toml b/node_rpc/Cargo.toml index 6188e5a..bba8368 100644 --- a/node_rpc/Cargo.toml +++ b/node_rpc/Cargo.toml @@ -43,3 +43,6 @@ path = "../node_core" [dependencies.rpc_primitives] path = "../rpc_primitives" + +[dependencies.common] +path = "../common" diff --git a/node_rpc/src/process.rs b/node_rpc/src/process.rs index 38b5843..01d2c39 100644 --- a/node_rpc/src/process.rs +++ b/node_rpc/src/process.rs @@ -11,19 +11,23 @@ use rpc_primitives::{ }; use storage::transaction::ActionData; -use crate::types::rpc_structs::{ - ExecuteScenarioMultipleSendRequest, ExecuteScenarioMultipleSendResponse, - ExecuteScenarioSplitRequest, ExecuteScenarioSplitResponse, ExecuteSubscenarioRequest, - ExecuteSubscenarioResponse, GetBlockDataRequest, GetBlockDataResponse, GetLastBlockRequest, - GetLastBlockResponse, RegisterAccountRequest, RegisterAccountResponse, - ShowAccountPublicBalanceRequest, ShowAccountPublicBalanceResponse, ShowAccountUTXORequest, - ShowAccountUTXOResponse, ShowTransactionRequest, ShowTransactionResponse, - UTXOShortEssentialStruct, WriteDepositPublicBalanceRequest, WriteDepositPublicBalanceResponse, - WriteMintPrivateUTXOMultipleAssetsRequest, WriteMintPrivateUTXOMultipleAssetsResponse, - WriteMintPrivateUTXORequest, WriteMintPrivateUTXOResponse, WriteSendDeshieldedBalanceRequest, - WriteSendDeshieldedUTXOResponse, WriteSendPrivateUTXORequest, WriteSendPrivateUTXOResponse, - WriteSendShieldedUTXORequest, WriteSendShieldedUTXOResponse, WriteSendSplitUTXOResponse, - WriteSplitUTXORequest, +use crate::types::{ + err_rpc::cast_common_execution_error_into_rpc_error, + rpc_structs::{ + ExecuteScenarioMultipleSendRequest, ExecuteScenarioMultipleSendResponse, + ExecuteScenarioSplitRequest, ExecuteScenarioSplitResponse, ExecuteSubscenarioRequest, + ExecuteSubscenarioResponse, GetBlockDataRequest, GetBlockDataResponse, GetLastBlockRequest, + GetLastBlockResponse, RegisterAccountRequest, RegisterAccountResponse, + ShowAccountPublicBalanceRequest, ShowAccountPublicBalanceResponse, ShowAccountUTXORequest, + ShowAccountUTXOResponse, ShowTransactionRequest, ShowTransactionResponse, + UTXOShortEssentialStruct, WriteDepositPublicBalanceRequest, + WriteDepositPublicBalanceResponse, WriteMintPrivateUTXOMultipleAssetsRequest, + WriteMintPrivateUTXOMultipleAssetsResponse, WriteMintPrivateUTXORequest, + WriteMintPrivateUTXOResponse, WriteSendDeshieldedBalanceRequest, + WriteSendDeshieldedUTXOResponse, WriteSendPrivateUTXORequest, WriteSendPrivateUTXOResponse, + WriteSendShieldedUTXORequest, WriteSendShieldedUTXOResponse, WriteSendSplitUTXOResponse, + WriteSplitUTXORequest, + }, }; use super::{respond, types::err_rpc::RpcErr, JsonHandler}; @@ -51,11 +55,26 @@ impl JsonHandler { let mut store = self.node_chain_store.lock().await; match req.scenario_id { - 1 => store.subscenario_1().await?, - 2 => store.subscenario_2().await?, - 3 => store.subscenario_3().await?, - 4 => store.subscenario_4().await?, - 5 => store.subscenario_5().await?, + 1 => store + .subscenario_1() + .await + .map_err(cast_common_execution_error_into_rpc_error)?, + 2 => store + .subscenario_2() + .await + .map_err(cast_common_execution_error_into_rpc_error)?, + 3 => store + .subscenario_3() + .await + .map_err(cast_common_execution_error_into_rpc_error)?, + 4 => store + .subscenario_4() + .await + .map_err(cast_common_execution_error_into_rpc_error)?, + 5 => store + .subscenario_5() + .await + .map_err(cast_common_execution_error_into_rpc_error)?, _ => return Err(RpcErr(RpcError::invalid_params("Scenario id not found"))), } } @@ -78,7 +97,8 @@ impl JsonHandler { store .scenario_1(req.visibility_list, req.publication_index) - .await?; + .await + .map_err(cast_common_execution_error_into_rpc_error)?; } let helperstruct = ExecuteScenarioSplitResponse { @@ -99,7 +119,8 @@ impl JsonHandler { store .scenario_2(req.number_of_assets, req.number_to_send) - .await?; + .await + .map_err(cast_common_execution_error_into_rpc_error)?; } let helperstruct = ExecuteScenarioMultipleSendResponse { @@ -331,7 +352,8 @@ impl JsonHandler { cover_guard .operate_account_deposit_public(acc_addr, req.amount as u128) - .await?; + .await + .map_err(cast_common_execution_error_into_rpc_error)?; }; let helperstruct = WriteDepositPublicBalanceResponse { @@ -357,7 +379,8 @@ impl JsonHandler { cover_guard .operate_account_mint_private(acc_addr, req.amount as u128) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteMintPrivateUTXOResponse { @@ -395,7 +418,8 @@ impl JsonHandler { req.amount as u128, req.num_of_assets, ) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteMintPrivateUTXOMultipleAssetsResponse { @@ -477,7 +501,8 @@ impl JsonHandler { cover_guard .operate_account_send_private_one_receiver(acc_addr, utxo_to_send, comm_hash) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteSendPrivateUTXOResponse { @@ -529,7 +554,8 @@ impl JsonHandler { acc_addr_rec, req.amount as u128, ) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteSendShieldedUTXOResponse { @@ -615,7 +641,8 @@ impl JsonHandler { utxo_to_send, comm_hash, ) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteSendDeshieldedUTXOResponse { @@ -707,7 +734,8 @@ impl JsonHandler { comm_hash, req.visibility_list, ) - .await? + .await + .map_err(cast_common_execution_error_into_rpc_error)? }; let helperstruct = WriteSendSplitUTXOResponse { diff --git a/node_rpc/src/types/err_rpc.rs b/node_rpc/src/types/err_rpc.rs index c7ef25b..3f44956 100644 --- a/node_rpc/src/types/err_rpc.rs +++ b/node_rpc/src/types/err_rpc.rs @@ -1,6 +1,6 @@ +use common::{ExecutionFailureKind, SequencerClientError}; use log::debug; -use node_core::sequencer_client::SequencerClientError; use rpc_primitives::errors::{RpcError, RpcParseError}; pub struct RpcErr(pub RpcError); @@ -59,3 +59,20 @@ pub fn cast_seq_client_error_into_rpc_error(seq_cli_err: SequencerClientError) - ), } } + +pub fn cast_common_execution_error_into_rpc_error(comm_exec_err: ExecutionFailureKind) -> RpcError { + let error_string = comm_exec_err.to_string(); + + match comm_exec_err { + ExecutionFailureKind::BuilderError(_) => RpcError::new_internal_error(None, &error_string), + ExecutionFailureKind::WriteError(_) => RpcError::new_internal_error(None, &error_string), + ExecutionFailureKind::DecodeError(_) => RpcError::new_internal_error(None, &error_string), + ExecutionFailureKind::ProveError(_) => RpcError::new_internal_error(None, &error_string), + ExecutionFailureKind::AmountMismatchError => { + RpcError::new_internal_error(None, &error_string) + } + ExecutionFailureKind::SequencerClientError(seq_cli_err) => { + cast_seq_client_error_into_rpc_error(seq_cli_err) + } + } +} diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index c6be871..d0163f1 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -60,11 +60,13 @@ impl SequencerCore { } pub fn get_tree_roots(&self) -> [[u8; 32]; 3] { - //All roots are non-None after start of sequencer main loop [ - self.store.nullifier_store.curr_root.unwrap(), - self.store.utxo_commitments_store.get_root().unwrap(), - self.store.pub_tx_store.get_root().unwrap(), + self.store.nullifier_store.curr_root.unwrap_or([0; 32]), + self.store + .utxo_commitments_store + .get_root() + .unwrap_or([0; 32]), + self.store.pub_tx_store.get_root().unwrap_or([0; 32]), ] } diff --git a/zkvm/Cargo.toml b/zkvm/Cargo.toml index 55d7e15..f0c0576 100644 --- a/zkvm/Cargo.toml +++ b/zkvm/Cargo.toml @@ -23,6 +23,9 @@ path = "../storage" [dependencies.utxo] path = "../utxo" +[dependencies.common] +path = "../common" + [features] cuda = ["risc0-zkvm/cuda"] default = [] diff --git a/zkvm/src/lib.rs b/zkvm/src/lib.rs index aff0ced..2ec508f 100644 --- a/zkvm/src/lib.rs +++ b/zkvm/src/lib.rs @@ -1,35 +1,8 @@ use accounts::account_core::AccountAddress; +use common::ExecutionFailureKind; use risc0_zkvm::{default_executor, default_prover, sha::Digest, ExecutorEnv, Receipt}; use utxo::utxo_core::{UTXOPayload, UTXO}; -#[derive(Debug, thiserror::Error)] -pub enum ExecutionFailureKind { - #[error("Failed to write into builder err: {0:?}")] - WriteError(anyhow::Error), - #[error("Failed to build builder err: {0:?}")] - BuilderError(anyhow::Error), - #[error("Failed prove execution err: {0:?}")] - ProveError(anyhow::Error), - #[error("Failed to decode data from VM: {0:?}")] - DecodeError(#[from] risc0_zkvm::serde::Error), - #[error("Inputs amounts does not match outputs")] - AmountMismatchError, -} - -impl ExecutionFailureKind { - pub fn write_error(err: anyhow::Error) -> Self { - Self::WriteError(err) - } - - pub fn builder_error(err: anyhow::Error) -> Self { - Self::BuilderError(err) - } - - pub fn prove_error(err: anyhow::Error) -> Self { - Self::ProveError(err) - } -} - pub fn prove_mint_utxo( amount_to_mint: u128, owner: AccountAddress,