Skip to content

Commit

Permalink
build(blockifier_reexecution): add reexecution errors (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware authored Oct 14, 2024
1 parent 087af3b commit 8f378b7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/blockifier_reexecution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier_reexecution/src/state_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod compile;
mod errors;
#[cfg(test)]
pub mod raw_rpc_json_test;
#[cfg(test)]
Expand Down
18 changes: 18 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/errors.rs
Original file line number Diff line number Diff line change
@@ -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),
}
33 changes: 17 additions & 16 deletions crates/blockifier_reexecution/src/state_reader/test_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = Result<T, ReexecutionError>;

pub struct TestStateReader(RpcStateReader);

impl StateReader for TestStateReader {
Expand Down Expand Up @@ -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<BlockInfo> {
pub fn get_block_info(&self) -> ReexecutionResult<BlockInfo> {
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() };
Expand All @@ -100,38 +103,36 @@ impl TestStateReader {
.try_into()?)
}

pub fn get_starknet_version(&self) -> StateResult<StarknetVersion> {
pub fn get_starknet_version(&self) -> ReexecutionResult<StarknetVersion> {
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)?
["starknet_version"]
.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<Vec<String>> {
pub fn get_tx_hashes(&self) -> ReexecutionResult<Vec<String>> {
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<Transaction> {
pub fn get_tx_by_hash(&self, tx_hash: &str) -> ReexecutionResult<Transaction> {
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<StarknetContractClass> {
Expand All @@ -145,7 +146,7 @@ impl TestStateReader {
Ok(contract_class)
}

pub fn get_all_txs_in_block(&self) -> StateResult<Vec<(Transaction, TransactionHash)>> {
pub fn get_all_txs_in_block(&self) -> ReexecutionResult<Vec<(Transaction, TransactionHash)>> {
// TODO(Aviv): Use batch request to get all txs in a block.
self.get_tx_hashes()?
.iter()
Expand All @@ -156,11 +157,11 @@ impl TestStateReader {
.collect::<Result<_, _>>()
}

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<BlockContext> {
pub fn get_block_context(&self) -> ReexecutionResult<BlockContext> {
Ok(BlockContext::new(
self.get_block_info()?,
get_chain_info(),
Expand All @@ -171,7 +172,7 @@ impl TestStateReader {

pub fn get_transaction_executor(
test_state_reader: TestStateReader,
) -> StateResult<TransactionExecutor<TestStateReader>> {
) -> ReexecutionResult<TransactionExecutor<TestStateReader>> {
let block_context = test_state_reader.get_block_context()?;
Ok(TransactionExecutor::<TestStateReader>::new(
CachedState::new(test_state_reader),
Expand Down

0 comments on commit 8f378b7

Please sign in to comment.