Skip to content

Commit

Permalink
Created trait VisitedPcsRaw for blockifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagle941 committed Sep 8, 2024
1 parent 59fb8a9 commit 81a4960
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions starknet-replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ cairo-lang-sierra-generator = "2.7.0-rc.1"
# in the branch `extract_libfunc` of Reilabs' fork and need to be merged
# into the main branch.
# Hardcoding the commit hash for the time being.
blockifier = { git = "https://github.com/reilabs/blockifier.git", rev = "b8f98da161a0de30223bfdc1f9fc22de01b1fc41", features = ["vector_of_visited_program_counters"] }
#blockifier = { path = "../../blockifier/crates/blockifier", features = ["vector_of_visited_program_counters"] } #"0.8.0-rc.0"
blockifier = { git = "https://github.com/reilabs/blockifier.git", rev = "04a22355b0c4e1efc1c6f98c92eeb98bf1751ba3" }
#blockifier = { path = "../../blockifier/crates/blockifier" } #"0.8.0-rc.0"
# `plotters` is using the latest (as of 30-May-2024) commit on the branch
# `next-release-devel` because it contains the fix for bug #551 related to
# anchoring of labels when rotated. Issue #26.
Expand Down
1 change: 1 addition & 0 deletions starknet-replay/src/profiler/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub fn extract_libfuncs_weight(
.sorted_by(|a, b| Ord::cmp(&a.1, &b.1))
{
tracing::info!(" libfunc {concrete_name}: {weight}");
//println!(" libfunc {concrete_name}: {weight}");
}

Ok(local_cumulative_libfuncs_weight)
Expand Down
6 changes: 4 additions & 2 deletions starknet-replay/src/storage/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use starknet_providers::jsonrpc::HttpTransport;
use starknet_providers::{JsonRpcClient, Provider};
use url::Url;

use self::visited_pcs::VisitedPcsRaw;
use crate::block_number::BlockNumber;
use crate::contract_address::to_field_element;
use crate::error::{DatabaseError, RunnerError};
Expand All @@ -60,6 +61,7 @@ pub mod class_info;
pub mod contract_class;
pub mod receipt;
pub mod transaction;
pub mod visited_pcs;

/// These versioned constants are needed to replay transactions executed with
/// older Starknet versions.
Expand Down Expand Up @@ -534,7 +536,7 @@ impl ReplayStorage for RpcStorage {
};
let starknet_version = work.header.starknet_version.clone();
let versioned_constants = Self::versioned_constants(&starknet_version);
let mut state = CachedState::new(state_reader);
let mut state: CachedState<_, VisitedPcsRaw> = CachedState::new(state_reader);
let block_context = BlockContext::new(
block_info,
chain_info,
Expand All @@ -549,7 +551,7 @@ impl ReplayStorage for RpcStorage {

let mut transaction_result: Vec<_> = Vec::with_capacity(transactions.len());
for transaction in transactions {
let mut tx_state = CachedState::<_>::create_transactional(&mut state);
let mut tx_state = CachedState::<_, _>::create_transactional(&mut state);
// No fee is being calculated.
let tx_info = transaction.execute(&mut tx_state, &block_context, charge_fee, validate);
tx_state.to_state_diff()?;
Expand Down
67 changes: 67 additions & 0 deletions starknet-replay/src/storage/rpc/visited_pcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::collections::hash_map::{Entry, IntoIter, Iter};
use std::collections::{HashMap, HashSet};

use blockifier::state::state_api::State;
use blockifier::state::visited_pcs::VisitedPcs;
use starknet_api::core::ClassHash;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct VisitedPcsRaw(HashMap<ClassHash, Vec<Vec<usize>>>);
impl VisitedPcsRaw {
pub fn iter(&self) -> impl Iterator<Item = (&ClassHash, &Vec<Vec<usize>>)> {
self.into_iter()
}
}
impl VisitedPcs for VisitedPcsRaw {
type Pcs = Vec<Vec<usize>>;

fn new() -> Self {
VisitedPcsRaw(HashMap::default())
}

fn insert(&mut self, class_hash: &ClassHash, pcs: &[usize]) {
self.0.entry(*class_hash).or_default().push(pcs.to_vec());
}

fn iter(&self) -> impl Iterator<Item = (&ClassHash, &Self::Pcs)> {
self.0.iter()
}

fn entry(&mut self, class_hash: ClassHash) -> Entry<'_, ClassHash, Self::Pcs> {
self.0.entry(class_hash)
}

fn extend(&mut self, class_hash: &ClassHash, pcs: &Self::Pcs) {
self.0.entry(*class_hash).or_default().extend(pcs.clone());
}

fn to_set(pcs: Self::Pcs) -> HashSet<usize> {
let mut set = HashSet::new();
pcs.into_iter().flatten().for_each(|p| {
set.insert(p);
});
set
}

fn add_visited_pcs(state: &mut dyn State, class_hash: &ClassHash, pcs: Self::Pcs) {
for pc in pcs {
state.add_visited_pcs(*class_hash, &pc);
}
}
}
impl IntoIterator for VisitedPcsRaw {
type Item = (ClassHash, Vec<Vec<usize>>);
type IntoIter = IntoIter<ClassHash, Vec<Vec<usize>>>;

fn into_iter(self) -> IntoIter<ClassHash, Vec<Vec<usize>>> {
self.0.into_iter()
}
}
impl<'a> IntoIterator for &'a VisitedPcsRaw {
type Item = (&'a ClassHash, &'a Vec<Vec<usize>>);
type IntoIter = Iter<'a, ClassHash, Vec<Vec<usize>>>;

fn into_iter(self) -> Iter<'a, ClassHash, Vec<Vec<usize>>> {
self.0.iter()
}
}

0 comments on commit 81a4960

Please sign in to comment.