Skip to content

Commit

Permalink
Replaced manual RPC client with starknet-rs client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagle941 committed Jul 26, 2024
1 parent a96685a commit 13a899d
Show file tree
Hide file tree
Showing 20 changed files with 1,323 additions and 752 deletions.
745 changes: 458 additions & 287 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ readme = "README.md"
[workspace.dependencies]
anyhow = "1.0.75"
tracing = "0.1.37"
itertools = { version = "0.11.0", default-features = false }
itertools = { version = "0.12.1", default-features = false }
25 changes: 15 additions & 10 deletions starknet-replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@ publish = false

[dependencies]
# All `cairo-lang-` crates must have the same version
cairo-lang-starknet-classes = "2.6.0"
cairo-lang-sierra-to-casm = "2.6.0"
cairo-lang-runner = "2.6.0"
cairo-lang-compiler = "2.6.0"
cairo-lang-sierra = "2.6.0"
cairo-lang-utils = "2.6.0"
cairo-lang-sierra-generator = "2.6.0"
cairo-lang-starknet-classes = "2.7.0-rc.1"
cairo-lang-sierra-to-casm = "2.7.0-rc.1"
cairo-lang-runner = "2.7.0-rc.1"
cairo-lang-compiler = "2.7.0-rc.1"
cairo-lang-sierra = "2.7.0-rc.1"
cairo-lang-utils = "2.7.0-rc.1"
cairo-lang-sierra-generator = "2.7.0-rc.1"
# Some changes are required to `starkware-libs/blockifier`. These changes are
# in the branch `extract_libfunc` of Reilabs fork and need to be merged
# in main branch.
# Hardcoding the commit hash for the time being.
blockifier = { git = "https:/reilabs/blockifier.git", rev = "fdb86caf85435cb42ae363c7b04b5620bc80c24a" }
#blockifier = { git = "https:/reilabs/blockifier.git", rev = "06abd15b92c8933116c7e5b4e8d1fd3936fe4b5f" }
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.
plotters = { git = "https:/plotters-rs/plotters.git", rev = "a7a3f8989af20931dd9e7e1f204d5254de3a8053" }
flate2 = "1.0.25"
rayon = "1.8.0"
starknet_api = "0.10.0"
starknet = "0.5.0"
serde = "1.0.192"
serde_json = "1.0.105"
serde_with = "3.0.0"
Expand All @@ -43,6 +42,12 @@ jsonrpc = { version = "0.18.0", features = ["minreq_http"] }
url = "2.5.2"
hex = "0.4.3"
once_cell = "1.17.1"
tokio = { version = "1.27.0", features = ["full"] }
# The versions of `starknet` dependencies must match the version used in blockifier.
starknet_api = "0.13.0-rc.0"
starknet-core = "0.11.0"
starknet-providers = "0.11.0"
primitive-types = "0.12.2"
anyhow.workspace = true
tracing.workspace = true
itertools.workspace = true
Expand Down
13 changes: 13 additions & 0 deletions starknet-replay/src/block_number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::fmt;

use serde::{Deserialize, Serialize};
use starknet_core::types::BlockId;

/// `BlockNumber` is represented as a `u64` integer.
#[derive(
Expand All @@ -27,3 +28,15 @@ impl fmt::Display for BlockNumber {
write!(f, "{}", self.0)
}
}

impl From<BlockNumber> for BlockId {
fn from(block: BlockNumber) -> BlockId {
BlockId::Number(block.get())
}
}

impl From<&BlockNumber> for BlockId {
fn from(block: &BlockNumber) -> BlockId {
BlockId::Number(block.get())
}
}
11 changes: 11 additions & 0 deletions starknet-replay/src/contract_address/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use starknet_api::core::PatriciaKey;
use starknet_core::types::Felt;

use crate::error::DatabaseError;

pub fn to_field_element(contract_address: &PatriciaKey) -> Result<Felt, DatabaseError> {
Ok(**contract_address)
// let field_element =
// FieldElement::from_bytes_be(contract_address.bytes().try_into().
// unwrap()).unwrap(); Ok(field_element)
}
1 change: 1 addition & 0 deletions starknet-replay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use error::RunnerError;
use runner::replay_block::ReplayBlock;

pub mod block_number;
pub mod contract_address;
pub mod error;
pub mod histogram;
pub mod profiler;
Expand Down
41 changes: 20 additions & 21 deletions starknet-replay/src/profiler/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cairo_lang_sierra::program::Program;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoContractClass;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use itertools::Itertools;
use starknet::core::types::ContractClass;
use starknet_core::types::ContractClass;

use crate::profiler::replace_ids::replace_sierra_ids_in_program;
use crate::profiler::replay_statistics::ReplayStatistics;
Expand Down Expand Up @@ -105,26 +105,25 @@ pub fn extract_libfuncs_weight(

let runner = SierraProfiler::new(sierra_program.clone())?;

for pcs in all_pcs {
let raw_profiling_info = runner.collect_profiling_info(pcs.as_slice())?;

let profiling_info_processor = ProfilingInfoProcessor::new(
None,
sierra_program.clone(),
UnorderedHashMap::default(),
);

let profiling_info_processor_params = get_profiling_info_processor_params();
let profiling_info = profiling_info_processor
.process_ex(&raw_profiling_info, &profiling_info_processor_params);
let Some(concrete_libfunc_weights) =
profiling_info.libfunc_weights.concrete_libfunc_weights
else {
continue;
};
local_cumulative_libfuncs_weight =
local_cumulative_libfuncs_weight.add_statistics(&concrete_libfunc_weights);
}
//for pcs in all_pcs {
let mut pcs_vec: Vec<usize> = Vec::with_capacity(all_pcs.len());
pcs_vec.extend(all_pcs.iter());
let raw_profiling_info = runner.collect_profiling_info(pcs_vec.as_slice())?;

let profiling_info_processor =
ProfilingInfoProcessor::new(None, sierra_program.clone(), UnorderedHashMap::default());

let profiling_info_processor_params = get_profiling_info_processor_params();
let profiling_info = profiling_info_processor
.process_ex(&raw_profiling_info, &profiling_info_processor_params);
let Some(concrete_libfunc_weights) =
profiling_info.libfunc_weights.concrete_libfunc_weights
else {
continue;
};
local_cumulative_libfuncs_weight =
local_cumulative_libfuncs_weight.add_statistics(&concrete_libfunc_weights);
//}
}

for (concrete_name, weight) in local_cumulative_libfuncs_weight
Expand Down
4 changes: 2 additions & 2 deletions starknet-replay/src/profiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl SierraProfiler {
self.casm_program
.debug_info
.sierra_statement_info
.partition_point(|x| x.code_offset <= pc)
.partition_point(|x| x.start_offset <= pc)
- 1,
)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ impl SierraProfiler {
.sierra_statement_info
.last()
.ok_or(ProfilerError::EmptyStatementList)?
.code_offset;
.end_offset;
// The CASM program starts with a header of instructions to wrap the
// real program. `real_pc_0` is the PC in the trace that points
// to the same CASM instruction which is in the real PC=0 in the
Expand Down
3 changes: 2 additions & 1 deletion starknet-replay/src/profiler/replace_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl SierraIdReplacer for DebugReplacer {

fn replace_type_id(&self, id: &ConcreteTypeId) -> ConcreteTypeId {
match self.lookup_intern_concrete_type(id) {
SierraGeneratorTypeLongId::CycleBreaker(ty) => todo!("{:?}", ty),
SierraGeneratorTypeLongId::Phantom(ty)
| SierraGeneratorTypeLongId::CycleBreaker(ty) => todo!("{:?}", ty),
SierraGeneratorTypeLongId::Regular(long_id) => {
let mut long_id = long_id.as_ref().clone();
self.replace_generic_args(&mut long_id.generic_args);
Expand Down
4 changes: 2 additions & 2 deletions starknet-replay/src/runner/replay_class_hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the definition of the struct [`ReplayClassHash`].

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};
use starknet_api::core::ClassHash as StarknetClassHash;
Expand All @@ -23,4 +23,4 @@ pub struct ReplayClassHash {

/// The type [`VisitedPcs`] is a hashmap to store the visited program counters
/// for each contract invocation during replay.
pub type VisitedPcs = HashMap<ReplayClassHash, Vec<Vec<usize>>>;
pub type VisitedPcs = HashMap<ReplayClassHash, HashSet<usize>>;
18 changes: 7 additions & 11 deletions starknet-replay/src/runner/replay_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use blockifier::execution::contract_class::ContractClass as BlockifierContractClass;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use starknet::core::types::ContractClass as StarknetContractClass;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_core::types::{ContractClass as StarknetContractClass, Felt};

use crate::block_number::BlockNumber;
use crate::runner::replay_class_hash::ReplayClassHash;
Expand Down Expand Up @@ -38,10 +37,10 @@ impl ReplayStateReader<'_> {
}
impl StateReader for ReplayStateReader<'_> {
fn get_storage_at(
&mut self,
&self,
contract_address: ContractAddress,
key: StorageKey,
) -> StateResult<StarkFelt> {
) -> StateResult<Felt> {
let storage_value = self
.storage
.starknet_get_storage_at(&self.block_number, &contract_address, &key)
Expand All @@ -51,7 +50,7 @@ impl StateReader for ReplayStateReader<'_> {
Ok(storage_value)
}

fn get_nonce_at(&mut self, contract_address: ContractAddress) -> StateResult<Nonce> {
fn get_nonce_at(&self, contract_address: ContractAddress) -> StateResult<Nonce> {
let nonce = self
.storage
.starknet_get_nonce(&self.block_number, &contract_address)
Expand All @@ -61,7 +60,7 @@ impl StateReader for ReplayStateReader<'_> {
Ok(nonce)
}

fn get_class_hash_at(&mut self, contract_address: ContractAddress) -> StateResult<ClassHash> {
fn get_class_hash_at(&self, contract_address: ContractAddress) -> StateResult<ClassHash> {
let class_hash = self
.storage
.starknet_get_class_hash_at(&self.block_number, &contract_address)
Expand All @@ -72,7 +71,7 @@ impl StateReader for ReplayStateReader<'_> {
}

fn get_compiled_contract_class(
&mut self,
&self,
class_hash: ClassHash,
) -> StateResult<BlockifierContractClass> {
let replay_class_hash = ReplayClassHash {
Expand Down Expand Up @@ -107,10 +106,7 @@ impl StateReader for ReplayStateReader<'_> {
}
}

fn get_compiled_class_hash(
&mut self,
_class_hash: ClassHash,
) -> StateResult<CompiledClassHash> {
fn get_compiled_class_hash(&self, _class_hash: ClassHash) -> StateResult<CompiledClassHash> {
todo!()
}
}
10 changes: 5 additions & 5 deletions starknet-replay/src/runner/report.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::OpenOptions;
use std::io::{BufWriter, Write};
use std::io::BufWriter;
use std::path::PathBuf;

use blockifier::transaction::objects::TransactionExecutionInfo;
Expand Down Expand Up @@ -27,10 +27,10 @@ pub fn write_to_file(
.append(true)
.create(true)
.open(filename)?;
let mut f = BufWriter::new(output_file);
for (trace, _) in traces {
let output = serde_json::to_string(&trace)?;
f.write_all(output.as_bytes())?;
let mut _f = BufWriter::new(output_file);
for (_trace, _) in traces {
//let output = serde_json::to_string(&trace)?;
//f.write_all(output.as_bytes())?;
}
Ok(())
}
2 changes: 1 addition & 1 deletion starknet-replay/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! with a new `Starknet` node.

use blockifier::transaction::objects::TransactionExecutionInfo;
use starknet::core::types::ContractClass;
use starknet_api::block::BlockHeader;
use starknet_api::transaction::{Transaction, TransactionReceipt};
use starknet_core::types::ContractClass;

use crate::block_number::BlockNumber;
use crate::error::DatabaseError;
Expand Down
2 changes: 1 addition & 1 deletion starknet-replay/src/storage/rpc/class_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//! replay.

use blockifier::execution::contract_class::ClassInfo;
use starknet::core::types::ContractClass;
use starknet_api::core::ClassHash;
use starknet_api::transaction::{DeclareTransaction, Transaction};
use starknet_core::types::ContractClass;

use super::contract_class::decompress_sierra;
use crate::block_number::BlockNumber;
Expand Down
4 changes: 2 additions & 2 deletions starknet-replay/src/storage/rpc/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use blockifier::execution::contract_class::{
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoContractClass;
use flate2::bufread;
use starknet::core::types::contract::legacy::{LegacyContractClass, LegacyProgram};
use starknet::core::types::{CompressedLegacyContractClass, FlattenedSierraClass};
use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass;
use starknet_core::types::contract::legacy::{LegacyContractClass, LegacyProgram};
use starknet_core::types::{CompressedLegacyContractClass, FlattenedSierraClass};

use crate::error::DatabaseError;
use crate::storage::rpc::contract_class;
Expand Down
Loading

0 comments on commit 13a899d

Please sign in to comment.