Skip to content

Commit

Permalink
move to replay crate
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoGiachetta committed Jan 30, 2025
1 parent 58dff30 commit caedcc1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 80 deletions.
2 changes: 2 additions & 0 deletions replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"

[features]
default = ["block-composition"]
block-composition = ["structured_logging", "dep:serde", "dep:serde_json",]
benchmark = ["dep:serde", "dep:serde_json", "dep:serde_with"]
# The only_cairo_vm feature is designed to avoid executing transitions with cairo_native and instead use cairo_vm exclusively
only_cairo_vm = ["rpc-state-reader/only_casm"]
Expand Down
71 changes: 71 additions & 0 deletions replay/src/block_composition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::{collections::HashMap, error::Error, fs::File, path::Path};

use blockifier::{execution::call_info::CallInfo, transaction::objects::TransactionExecutionInfo};
use serde::Serialize;
use starknet_api::core::{ClassHash, EntryPointSelector};

#[derive(Debug, Serialize)]
struct EntryPointExecution {
class_hash: ClassHash,
selector: EntryPointSelector,
}

pub fn save_entry_point_execution(
path: &Path,
executions: Vec<(u64, TransactionExecutionInfo)>,
) -> Result<(), Box<dyn Error>> {
let mut block_executions: HashMap<u64, Vec<HashMap<String, _>>> = HashMap::new();

for (block_number, execution) in executions {
let mut tx_execution: HashMap<String, _> = HashMap::new();

if let Some(call) = execution.validate_call_info {
tx_execution.insert(
"validate_call_info".to_string(),
get_inner_class_executions(call),
);
}
if let Some(call) = execution.execute_call_info {
tx_execution.insert(
"execute_call_info".to_string(),
get_inner_class_executions(call),
);
}
if let Some(call) = execution.fee_transfer_call_info {
tx_execution.insert(
"fee_transfer_call_info".to_string(),
get_inner_class_executions(call),
);
}

block_executions
.entry(block_number)
.or_insert_with(Vec::new)
.push(tx_execution);
}

let file = File::create(path)?;
serde_json::to_writer_pretty(file, &block_executions)?;

Ok(())
}

fn get_inner_class_executions(call: CallInfo) -> Vec<EntryPointExecution> {
// class hash can initially be None, but it is always added before execution
let class_hash = call.call.class_hash.unwrap();

let mut classes = call
.inner_calls
.into_iter()
.flat_map(get_inner_class_executions)
.collect::<Vec<_>>();

let top_class = EntryPointExecution {
class_hash,
selector: call.call.entry_point_selector,
};

classes.push(top_class);

classes
}
13 changes: 7 additions & 6 deletions replay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use starknet_api::transaction::{TransactionExecutionStatus, TransactionHash};
use tracing::{debug, error, info, info_span};
use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};

#[cfg(feature = "block-composition")]
use block_composition::save_entry_point_execution;
#[cfg(feature = "benchmark")]
use {
crate::benchmark::{
Expand All @@ -24,17 +26,16 @@ use {
std::time::Instant,
};

#[cfg(feature = "structured_logging")]
use rpc_state_reader::utils::save_entry_point_execution;

#[cfg(any(feature = "benchmark", feature = "structured_logging"))]
#[cfg(any(feature = "benchmark", feature = "block-composition"))]
use std::path::PathBuf;

#[cfg(feature = "profiling")]
use {std::thread, std::time::Duration};

#[cfg(feature = "benchmark")]
mod benchmark;
#[cfg(feature = "block-composition")]
mod block_composition;
#[cfg(feature = "state_dump")]
mod state_dump;

Expand Down Expand Up @@ -95,10 +96,10 @@ Caches all rpc data before the benchmark runs to provide accurate results"
#[arg(short, long, default_value=PathBuf::from("data").into_os_string())]
output: PathBuf,
},
#[cfg(feature = "block-composition")]
#[clap(
about = "Executes a range of blocks and writes down to a file every entrypoint executed."
)]
#[cfg(feature = "structured_logging")]
BlockCompose {
block_start: u64,
block_end: u64,
Expand Down Expand Up @@ -335,7 +336,7 @@ fn main() {
);
}
}
#[cfg(feature = "structured_logging")]
#[cfg(feature = "block-composition")]
ReplayExecute::BlockCompose {
block_start,
block_end,
Expand Down
78 changes: 4 additions & 74 deletions rpc-state-reader/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use std::{
collections::HashMap,
error::Error,
fs::{self, File},
fs::{self},
io::{self, Read},
path::{Path, PathBuf},
path::PathBuf,
sync::{OnceLock, RwLock},
time::Instant,
};

use blockifier::{
execution::{call_info::CallInfo, contract_class::CompiledClassV1},
transaction::objects::TransactionExecutionInfo,
};
use blockifier::execution::contract_class::CompiledClassV1;
use cairo_lang_starknet_classes::contract_class::{ContractClass, ContractEntryPoints};
use cairo_lang_utils::bigint::BigUintAsHex;
use cairo_native::{executor::AotContractExecutor, OptLevel};
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use starknet::core::types::{LegacyContractEntryPoint, LegacyEntryPointsByType};
use starknet_api::{
contract_class::EntryPointType,
Expand All @@ -32,12 +28,6 @@ pub struct MiddleSierraContractClass {
pub entry_points_by_type: ContractEntryPoints,
}

#[derive(Debug, Serialize)]
struct EntryPointExecution {
class_hash: ClassHash,
selector: EntryPointSelector,
}

static AOT_PROGRAM_CACHE: OnceLock<RwLock<HashMap<ClassHash, AotContractExecutor>>> =
OnceLock::new();

Expand Down Expand Up @@ -166,63 +156,3 @@ pub fn get_casm_compiled_class(class: ContractClass, _class_hash: ClassHash) ->
pub fn bytecode_size(data: &[BigUintAsHex]) -> usize {
data.iter().map(|n| n.value.to_bytes_be().len()).sum()
}

pub fn save_entry_point_execution(
path: &Path,
executions: Vec<(u64, TransactionExecutionInfo)>,
) -> Result<(), Box<dyn Error>> {
let mut block_executions: HashMap<u64, Vec<HashMap<String, _>>> = HashMap::new();

for (block_number, execution) in executions {
let mut tx_execution: HashMap<String, _> = HashMap::new();

if let Some(call) = execution.validate_call_info {
tx_execution.insert(
"validate_call_info".to_string(),
get_inner_class_executions(call),
);
}
if let Some(call) = execution.execute_call_info {
tx_execution.insert(
"execute_call_info".to_string(),
get_inner_class_executions(call),
);
}
if let Some(call) = execution.fee_transfer_call_info {
tx_execution.insert(
"fee_transfer_call_info".to_string(),
get_inner_class_executions(call),
);
}

block_executions
.entry(block_number)
.or_insert_with(Vec::new)
.push(tx_execution);
}

let file = File::create(path)?;
serde_json::to_writer_pretty(file, &block_executions)?;

Ok(())
}

fn get_inner_class_executions(call: CallInfo) -> Vec<EntryPointExecution> {
// class hash can initially be None, but it is always added before execution
let class_hash = call.call.class_hash.unwrap();

let mut classes = call
.inner_calls
.into_iter()
.flat_map(get_inner_class_executions)
.collect::<Vec<_>>();

let top_class = EntryPointExecution {
class_hash,
selector: call.call.entry_point_selector,
};

classes.push(top_class);

classes
}

0 comments on commit caedcc1

Please sign in to comment.