Skip to content

Commit

Permalink
Merge pull request #77 from lambdaclass/rpc-errors
Browse files Browse the repository at this point in the history
rpc-state-reader refactor
  • Loading branch information
JulianGCalderon authored Oct 25, 2024
2 parents 08aa133 + 77bd3a0 commit 6555d4b
Show file tree
Hide file tree
Showing 12 changed files with 2,943 additions and 1,266 deletions.
2,395 changes: 2,235 additions & 160 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ resolver = "2"
[workspace.dependencies]
thiserror = "1.0.32"
starknet-types-core = "0.1.6"
starknet_api = { git = "https://github.com/lambdaclass/sequencer", rev = "693cd41a893c7de71682b59686bcd0c99a27c9e0"}
blockifier = { git = "https://github.com/lambdaclass/sequencer", rev = "693cd41a893c7de71682b59686bcd0c99a27c9e0"}
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "355c250f37cf0977ef2776b1aae2cb2e87c9da3d" }
tracing = "0.1"
serde_json = "1.0.116"
serde_with = "3.9.0"
serde = "1.0.197"
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "355c250f37cf0977ef2776b1aae2cb2e87c9da3d" }
# Sequencer Dependencies
starknet_api = { git = "https://github.com/lambdaclass/sequencer", rev = "1b1b95cae7ae07b9bc778443ca75ee18008a6bc8"}
blockifier = { git = "https://github.com/lambdaclass/sequencer", rev = "1b1b95cae7ae07b9bc778443ca75ee18008a6bc8"}
starknet_gateway = { git = "https://github.com/lambdaclass/sequencer", rev = "1b1b95cae7ae07b9bc778443ca75ee18008a6bc8"}

[patch.'https://github.com/lambdaclass/cairo_native']
cairo-native = { git = "https://github.com/lambdaclass//cairo_native.git", rev = "355c250f37cf0977ef2776b1aae2cb2e87c9da3d" }
61 changes: 25 additions & 36 deletions replay/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ use blockifier::{
state::{cached_state::CachedState, state_api::StateReader},
};
use rpc_state_reader::{
blockifier_state_reader::{execute_tx_with_blockifier, fetch_block_context, RpcStateReader},
rpc_state::{RpcChain, RpcState},
};
use starknet_api::{
block::BlockNumber,
hash::StarkHash,
transaction::{Transaction as SNTransaction, TransactionHash},
execution::{execute_tx_with_blockifier, fetch_block_context},
objects::TransactionWithHash,
reader::{RpcChain, RpcStateReader},
};
use starknet_api::{block::BlockNumber, hash::StarkHash, transaction::TransactionHash};
use tracing::{error, info, info_span};

pub type BlockCachedData = (
CachedState<OptionalStateReader<RpcStateReader>>,
BlockContext,
Vec<(TransactionHash, SNTransaction)>,
Vec<TransactionWithHash>,
);

/// Fetches context data to execute the given block range
Expand All @@ -39,32 +36,17 @@ pub fn fetch_block_range_data(
// For each block
let block_number = BlockNumber(block_number);

let rpc_state = RpcState::new_rpc(chain, block_number.into()).unwrap();
let reader = RpcStateReader::new(chain, block_number);

// Fetch block context
let block_context = fetch_block_context(&rpc_state, block_number);
let block_context = fetch_block_context(&reader);

// Fetch transactions for the block
let transactions = rpc_state
.get_transaction_hashes()
.unwrap()
.into_iter()
.map(|transaction_hash| {
let transaction_hash =
TransactionHash(StarkHash::from_hex(&transaction_hash).unwrap());

// Fetch transaction
let transaction = rpc_state.get_transaction(&transaction_hash).unwrap();

(transaction_hash, transaction)
})
.collect::<Vec<_>>();
let transactions = reader.get_block_with_txs().unwrap().transactions;

// Create cached state
let previous_rpc_state =
RpcState::new_rpc(chain, block_number.prev().unwrap().into()).unwrap();
let previous_rpc_state_reader = RpcStateReader::new(previous_rpc_state);
let cached_state = CachedState::new(OptionalStateReader::new(previous_rpc_state_reader));
let previous_reader = RpcStateReader::new(chain, block_number.prev().unwrap());
let cached_state = CachedState::new(OptionalStateReader::new(previous_reader));

block_caches.push((cached_state, block_context, transactions));
}
Expand All @@ -88,7 +70,11 @@ pub fn execute_block_range(block_range_data: &mut Vec<BlockCachedData>) {
// The transactional state is used to execute a transaction while discarding state changes applied to it.
let mut transactional_state = CachedState::create_transactional(state);

for (transaction_hash, transaction) in transactions {
for TransactionWithHash {
transaction_hash,
transaction,
} in transactions
{
// Execute each transaction
let _tx_span = info_span!(
"tx execution",
Expand Down Expand Up @@ -125,20 +111,23 @@ pub fn execute_block_range(block_range_data: &mut Vec<BlockCachedData>) {
}

pub fn fetch_transaction_data(tx: &str, block: BlockNumber, chain: RpcChain) -> BlockCachedData {
let rpc_state = RpcState::new_rpc(chain, block.into()).unwrap();
let reader = RpcStateReader::new(chain, block);

// Fetch block context
let block_context = fetch_block_context(&rpc_state, block);
let block_context = fetch_block_context(&reader);

// Fetch transactions for the block
let transaction_hash = TransactionHash(StarkHash::from_hex(tx).unwrap());
let transaction = rpc_state.get_transaction(&transaction_hash).unwrap();
let transactions = vec![(transaction_hash, transaction)];
let transaction = reader.get_transaction(&transaction_hash).unwrap();
let transactions = vec![TransactionWithHash {
transaction_hash,
transaction,
}];

// Create cached state
let previous_rpc_state = RpcState::new_rpc(chain, block.prev().unwrap().into()).unwrap();
let previous_rpc_state_reader = RpcStateReader::new(previous_rpc_state);
let cached_state = CachedState::new(OptionalStateReader::new(previous_rpc_state_reader));
let previous_reader = RpcStateReader::new(chain, block.prev().unwrap());

let cached_state = CachedState::new(OptionalStateReader::new(previous_reader));

(cached_state, block_context, transactions)
}
Expand Down
63 changes: 37 additions & 26 deletions replay/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::str::FromStr;

use blockifier::state::cached_state::CachedState;
use blockifier::state::errors::StateError;
use clap::{Parser, Subcommand};
use rpc_state_reader::{
blockifier_state_reader::RpcStateReader,
rpc_state::{BlockValue, RpcChain, RpcState},
rpc_state_errors::RpcStateError,
};

use rpc_state_reader::blockifier_state_reader::execute_tx_configurable;
use rpc_state_reader::execution::execute_tx_configurable;
use rpc_state_reader::reader::{RpcChain, RpcStateReader};
use starknet_api::block::BlockNumber;
use starknet_api::transaction::{TransactionExecutionStatus, TransactionHash};
use tracing::{debug, error, info, info_span};
use tracing_subscriber::filter::Directive;
use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};
Expand Down Expand Up @@ -106,7 +104,13 @@ fn main() {
let transaction_hashes = get_transaction_hashes(&chain, block_number)
.expect("Unable to fetch the transaction hashes.");
for tx_hash in transaction_hashes {
show_execution_data(&mut state, tx_hash, &chain, block_number, charge_fee);
show_execution_data(
&mut state,
tx_hash.0.to_hex_string(),
&chain,
block_number,
charge_fee,
);
}
}
ReplayExecute::BlockRange {
Expand All @@ -126,7 +130,13 @@ fn main() {
.expect("Unable to fetch the transaction hashes.");

for tx_hash in transaction_hashes {
show_execution_data(&mut state, tx_hash, &chain, block_number, charge_fee);
show_execution_data(
&mut state,
tx_hash.0.to_hex_string(),
&chain,
block_number,
charge_fee,
);
}
}
}
Expand Down Expand Up @@ -249,10 +259,7 @@ fn parse_network(network: &str) -> RpcChain {
fn build_cached_state(network: &str, block_number: u64) -> CachedState<RpcStateReader> {
let previous_block_number = BlockNumber(block_number);
let rpc_chain = parse_network(network);
let rpc_reader = RpcStateReader(
RpcState::new_rpc(rpc_chain, previous_block_number.into())
.expect("failed to create state reader"),
);
let rpc_reader = RpcStateReader::new(rpc_chain, previous_block_number);

CachedState::new(rpc_reader)
}
Expand Down Expand Up @@ -285,12 +292,13 @@ fn show_execution_data(
}
};

let execution_status = match &execution_info.revert_error {
Some(_) => "REVERTED",
None => "SUCCEEDED",
};
let rpc_execution_status = rpc_receipt.execution_status;
let status_matches = execution_status == rpc_execution_status;
let reverted = execution_info.is_reverted();
let rpc_reverted = matches!(
rpc_receipt.execution_status,
TransactionExecutionStatus::Reverted(_)
);

let status_matches = reverted == rpc_reverted;

let da_gas = &execution_info.receipt.da_gas;
let da_gas_str = format!(
Expand Down Expand Up @@ -342,8 +350,8 @@ fn show_execution_data(
error!(
transaction_hash = tx_hash,
chain = chain,
execution_status,
rpc_execution_status,
reverted,
rpc_reverted,
root_of_error = root_of_error,
execution_error_message = execution_info.revert_error,
n_events_and_messages = events_and_msgs,
Expand All @@ -356,8 +364,8 @@ fn show_execution_data(
info!(
transaction_hash = tx_hash,
chain = chain,
execution_status,
rpc_execution_status,
reverted,
rpc_reverted,
execution_error_message = execution_info.revert_error,
n_events_and_messages = events_and_msgs,
rpc_n_events_and_msgs = rpc_events_and_msgs,
Expand Down Expand Up @@ -387,11 +395,14 @@ fn show_execution_data(
debug!(?execution_gas, ?rpc_gas, "execution actual fee");
}

fn get_transaction_hashes(network: &str, block_number: u64) -> Result<Vec<String>, RpcStateError> {
fn get_transaction_hashes(
network: &str,
block_number: u64,
) -> Result<Vec<TransactionHash>, StateError> {
let network = parse_network(network);
let block_value = BlockValue::Number(BlockNumber(block_number));
let rpc_state = RpcState::new_rpc(network, block_value)?;
rpc_state.get_transaction_hashes()
let block_value = BlockNumber(block_number);
let rpc_state = RpcStateReader::new(network, block_value);
Ok(rpc_state.get_block_with_tx_hashes()?.transactions)
}

fn set_global_subscriber() {
Expand Down
2 changes: 1 addition & 1 deletion rpc-state-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ cairo-native = { workspace = true }
starknet = "0.7.0"
thiserror = { workspace = true }
flate2 = "1.0.25"
dotenv = "0.15.0"
cairo-vm = "1.0.0-rc5"
blockifier = { workspace = true }
starknet_gateway = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
Expand Down
Loading

0 comments on commit 6555d4b

Please sign in to comment.