Skip to content

Commit

Permalink
save everything in a folder and loop through the files
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoGiachetta committed Jan 31, 2025
1 parent 62b19e5 commit 6551fab
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ compiled_programs/
state_dumps/
rpc_cache
bench_data
block_composition/
13 changes: 11 additions & 2 deletions plotting/plot_block_composition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from argparse import ArgumentParser
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -75,10 +76,17 @@ def process(block):
'swaps': count_swaps(block['entrypoints']),
}

blocks = json.load(open(path))
df = pd.DataFrame()

df = pd.DataFrame(blocks).apply(process, axis=1).dropna().apply(pd.Series)
for filename in os.listdir(path):
blocks = json.load(open(path + "/" + filename))

block_df = pd.DataFrame(blocks)

df = pd.concat([df, block_df])

df = df.apply(process, axis=1).dropna().apply(pd.Series)
print(df)
return df


Expand All @@ -92,6 +100,7 @@ def process(block):

figure, ax = plt.subplots()

print(df_by_timestamp)
sns.lineplot(
data=df_by_timestamp,
x='timestamp',
Expand Down
28 changes: 24 additions & 4 deletions replay/src/block_composition.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use std::{collections::HashMap, error::Error, fs::File, path::Path};
use std::{
collections::HashMap,
error::Error,
fs::{self, File},
path::Path,
};

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

type BlockExecutionInfo = Vec<(
u64,
String,
Vec<Result<TransactionExecutionInfo, TransactionExecutionError>>,
)>;

#[derive(Debug, Serialize)]
struct BlockEntryPoints {
block_number: u64,
Expand All @@ -19,16 +33,22 @@ struct EntryPointExecution {

pub fn save_entry_point_execution(
file_path: &Path,
executions: Vec<(u64, String, Vec<TransactionExecutionInfo>)>,
executions: BlockExecutionInfo,
) -> Result<(), Box<dyn Error>> {
if let Some(parent_path) = file_path.parent() {
fs::create_dir_all(parent_path)?;
}

let mut blocks: Vec<BlockEntryPoints> = Vec::new();

for (block_number, block_timestamp, executions) in executions {
let entrypoints = executions
.into_iter()
.map(|execution| {
.map(|execution_rst| {
let mut tx_execution = HashMap::new();

let execution = execution_rst.unwrap();

if let Some(call) = execution.validate_call_info {
tx_execution.insert(
"validate_call_info".to_string(),
Expand Down
49 changes: 12 additions & 37 deletions replay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use starknet_api::block::BlockNumber;
use starknet_api::core::ChainId;
use starknet_api::felt;
use starknet_api::transaction::{TransactionExecutionStatus, TransactionHash};
use state_dump::create_state_dump;
use tracing::{debug, error, info, info_span};
use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};

Expand Down Expand Up @@ -107,8 +108,6 @@ Caches all rpc data before the benchmark runs to provide accurate results"
block_start: u64,
block_end: u64,
chain: String,
#[arg(short, long, default_value=PathBuf::from("data").into_os_string())]
output: PathBuf,
},
}

Expand Down Expand Up @@ -344,7 +343,6 @@ fn main() {
block_start,
block_end,
chain,
output,
} => {
info!("executing block range: {} - {}", block_start, block_end);

Expand Down Expand Up @@ -373,7 +371,10 @@ fn main() {
.map(|hash| {
let (tx, _) =
fetch_transaction_with_state(&reader, &hash, flags.clone()).unwrap();
tx.execute(&mut state, &block_context).unwrap()
let execution = tx.execute(&mut state, &block_context);
#[cfg(feature = "state_dump")]
create_state_dump(&mut state, block_number, &hash.to_string(), &execution);
execution
})
.collect::<Vec<_>>();

Expand All @@ -387,7 +388,12 @@ fn main() {
block_executions.push((block_number, block_timestamp, entrypoints));
}

save_entry_point_execution(&output, block_executions).unwrap();
let path = PathBuf::from(format!(
"block_composition/block-compose-{}-{}-{}.json",
block_start, block_end, chain
));

save_entry_point_execution(&path, block_executions).unwrap();
}
}
}
Expand Down Expand Up @@ -445,38 +451,7 @@ fn show_execution_data(
let execution_info_result = tx.execute(state, &context);

#[cfg(feature = "state_dump")]
{
use std::path::Path;

let root = if cfg!(feature = "only_cairo_vm") {
Path::new("state_dumps/vm")
} else if cfg!(feature = "with-sierra-emu") {
Path::new("state_dumps/emu")
} else {
Path::new("state_dumps/native")
};
let root = root.join(format!("block{}", block_number));

std::fs::create_dir_all(&root).ok();

let mut path = root.join(&tx_hash_str);
path.set_extension("json");

match &execution_info_result {
Ok(execution_info) => {
state_dump::dump_state_diff(state, execution_info, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
Err(err) => {
// If we have no execution info, we write the error
// to a file so that it can be compared anyway
state_dump::dump_error(err, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
}
}
create_state_dump(state, block_number, &tx_hash_str, &execution_info_result);

let execution_info = match execution_info_result {
Ok(x) => x,
Expand Down
43 changes: 41 additions & 2 deletions replay/src/state_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,47 @@ use starknet_api::{
transaction::fields::Calldata,
};
use starknet_types_core::felt::Felt;
use tracing::error;

pub fn dump_state_diff(
pub fn create_state_dump(
state: &mut CachedState<impl StateReader>,
block_number: u64,
tx_hash_str: &str,
execution_info_result: &Result<TransactionExecutionInfo, TransactionExecutionError>,
) {
use std::path::Path;

let root = if cfg!(feature = "only_cairo_vm") {
Path::new("state_dumps/vm")
} else if cfg!(feature = "with-sierra-emu") {
Path::new("state_dumps/emu")
} else {
Path::new("state_dumps/native")
};
let root = root.join(format!("block{}", block_number));

std::fs::create_dir_all(&root).ok();

let mut path = root.join(tx_hash_str);
path.set_extension("json");

match execution_info_result {
Ok(execution_info) => {
dump_state_diff(state, execution_info, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
Err(err) => {
// If we have no execution info, we write the error
// to a file so that it can be compared anyway
dump_error(err, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
}
}

fn dump_state_diff(
state: &mut CachedState<impl StateReader>,
execution_info: &TransactionExecutionInfo,
path: &Path,
Expand All @@ -48,7 +87,7 @@ pub fn dump_state_diff(
Ok(())
}

pub fn dump_error(err: &TransactionExecutionError, path: &Path) -> anyhow::Result<()> {
fn dump_error(err: &TransactionExecutionError, path: &Path) -> anyhow::Result<()> {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
Expand Down

0 comments on commit 6551fab

Please sign in to comment.