From 8f378b799ce7a7a75f6eb56720446d719cf7ca9c Mon Sep 17 00:00:00 2001 From: aner-starkware <147302140+aner-starkware@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:59:11 +0300 Subject: [PATCH] build(blockifier_reexecution): add reexecution errors (#1302) --- Cargo.lock | 1 + crates/blockifier_reexecution/Cargo.toml | 1 + .../src/state_reader.rs | 1 + .../src/state_reader/errors.rs | 18 ++++++++++ .../src/state_reader/test_state_reader.rs | 33 ++++++++++--------- 5 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 crates/blockifier_reexecution/src/state_reader/errors.rs diff --git a/Cargo.lock b/Cargo.lock index b47db208bd..f07ae46094 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1546,6 +1546,7 @@ dependencies = [ "starknet-types-core", "starknet_api", "starknet_gateway", + "thiserror", ] [[package]] diff --git a/crates/blockifier_reexecution/Cargo.toml b/crates/blockifier_reexecution/Cargo.toml index a6201dece0..6967aa3d98 100644 --- a/crates/blockifier_reexecution/Cargo.toml +++ b/crates/blockifier_reexecution/Cargo.toml @@ -21,6 +21,7 @@ starknet-core.workspace = true starknet-types-core.workspace = true starknet_api.workspace = true starknet_gateway.workspace = true +thiserror.workspace = true [dev-dependencies] assert_matches.workspace = true diff --git a/crates/blockifier_reexecution/src/state_reader.rs b/crates/blockifier_reexecution/src/state_reader.rs index 9ce3f8a8ac..4efb7e1044 100644 --- a/crates/blockifier_reexecution/src/state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader.rs @@ -1,4 +1,5 @@ pub mod compile; +mod errors; #[cfg(test)] pub mod raw_rpc_json_test; #[cfg(test)] diff --git a/crates/blockifier_reexecution/src/state_reader/errors.rs b/crates/blockifier_reexecution/src/state_reader/errors.rs new file mode 100644 index 0000000000..e03126cdd4 --- /dev/null +++ b/crates/blockifier_reexecution/src/state_reader/errors.rs @@ -0,0 +1,18 @@ +use blockifier::state::errors::StateError; +use blockifier::versioned_constants::VersionedConstantsError; +use serde_json::Error as SerdeError; +use starknet_gateway::errors::RPCStateReaderError; +use thiserror::Error; + +#[derive(Debug, Error)] +#[allow(clippy::enum_variant_names)] +pub enum ReexecutionError { + #[error(transparent)] + State(#[from] StateError), + #[error(transparent)] + Rpc(#[from] RPCStateReaderError), + #[error(transparent)] + Serde(#[from] SerdeError), + #[error(transparent)] + VersionedConstants(#[from] VersionedConstantsError), +} diff --git a/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs index 66d5129de6..22377a15fb 100644 --- a/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/test_state_reader.rs @@ -22,12 +22,15 @@ use starknet_gateway::rpc_state_reader::RpcStateReader; use starknet_types_core::felt::Felt; use crate::state_reader::compile::{legacy_to_contract_class_v0, sierra_to_contact_class_v1}; +use crate::state_reader::errors::ReexecutionError; use crate::state_reader::utils::{ deserialize_transaction_json_to_starknet_api_tx, get_chain_info, get_rpc_state_reader_config, }; +pub type ReexecutionResult = Result; + pub struct TestStateReader(RpcStateReader); impl StateReader for TestStateReader { @@ -75,7 +78,7 @@ impl TestStateReader { /// Get the block info of the current block. /// If l2_gas_price is not present in the block header, it will be set to 1. - pub fn get_block_info(&self) -> StateResult { + pub fn get_block_info(&self) -> ReexecutionResult { let get_block_params = GetBlockWithTxHashesParams { block_id: self.0.block_id }; let default_l2_price = ResourcePrice { price_in_wei: 1_u8.into(), price_in_fri: 1_u8.into() }; @@ -100,7 +103,7 @@ impl TestStateReader { .try_into()?) } - pub fn get_starknet_version(&self) -> StateResult { + pub fn get_starknet_version(&self) -> ReexecutionResult { let get_block_params = GetBlockWithTxHashesParams { block_id: self.0.block_id }; let raw_version: String = serde_json::from_value( self.0.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)? @@ -108,30 +111,28 @@ impl TestStateReader { .clone(), ) .map_err(serde_err_to_state_err)?; - StarknetVersion::try_from(raw_version.as_str()).map_err(|err| { - StateError::StateReadError(format!("Failed to match starknet version: {}", err)) - }) + Ok(StarknetVersion::try_from(raw_version.as_str())?) } /// Get all transaction hashes in the current block. - pub fn get_tx_hashes(&self) -> StateResult> { + pub fn get_tx_hashes(&self) -> ReexecutionResult> { let get_block_params = GetBlockWithTxHashesParams { block_id: self.0.block_id }; let raw_tx_hashes = serde_json::from_value( self.0.send_rpc_request("starknet_getBlockWithTxHashes", &get_block_params)? ["transactions"] .clone(), - ) - .map_err(serde_err_to_state_err)?; - serde_json::from_value(raw_tx_hashes).map_err(serde_err_to_state_err) + )?; + Ok(serde_json::from_value(raw_tx_hashes)?) } - pub fn get_tx_by_hash(&self, tx_hash: &str) -> StateResult { + pub fn get_tx_by_hash(&self, tx_hash: &str) -> ReexecutionResult { let method = "starknet_getTransactionByHash"; let params = json!({ "transaction_hash": tx_hash, }); - deserialize_transaction_json_to_starknet_api_tx(self.0.send_rpc_request(method, params)?) - .map_err(serde_err_to_state_err) + Ok(deserialize_transaction_json_to_starknet_api_tx( + self.0.send_rpc_request(method, params)?, + )?) } pub fn get_contract_class(&self, class_hash: &ClassHash) -> StateResult { @@ -145,7 +146,7 @@ impl TestStateReader { Ok(contract_class) } - pub fn get_all_txs_in_block(&self) -> StateResult> { + pub fn get_all_txs_in_block(&self) -> ReexecutionResult> { // TODO(Aviv): Use batch request to get all txs in a block. self.get_tx_hashes()? .iter() @@ -156,11 +157,11 @@ impl TestStateReader { .collect::>() } - pub fn get_versioned_constants(&self) -> StateResult<&'static VersionedConstants> { + pub fn get_versioned_constants(&self) -> ReexecutionResult<&'static VersionedConstants> { Ok(self.get_starknet_version()?.into()) } - pub fn get_block_context(&self) -> StateResult { + pub fn get_block_context(&self) -> ReexecutionResult { Ok(BlockContext::new( self.get_block_info()?, get_chain_info(), @@ -171,7 +172,7 @@ impl TestStateReader { pub fn get_transaction_executor( test_state_reader: TestStateReader, - ) -> StateResult> { + ) -> ReexecutionResult> { let block_context = test_state_reader.get_block_context()?; Ok(TransactionExecutor::::new( CachedState::new(test_state_reader),