From 6120a00e5ff92833aabfab40049daaf6d84039d5 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 13 Aug 2024 14:06:50 +0300 Subject: [PATCH 1/4] chore: organize methods such that public methods are at bottom --- .../src/execution/contract_class.rs | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 1e257619b7..8ea14307a6 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -122,14 +122,6 @@ impl ContractClassV0 { self.entry_points_by_type.values().map(|vec| vec.len()).sum() } - pub fn n_builtins(&self) -> usize { - self.program.builtins_len() - } - - pub fn bytecode_length(&self) -> usize { - self.program.data_len() - } - fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { let hashed_data_size = (constants::CAIRO0_ENTRY_POINT_STRUCT_SIZE * self.n_entry_points()) + self.n_builtins() @@ -145,6 +137,14 @@ impl ContractClassV0 { } } + pub fn n_builtins(&self) -> usize { + self.program.builtins_len() + } + + pub fn bytecode_length(&self) -> usize { + self.program.data_len() + } + pub fn try_from_json_string(raw_contract_class: &str) -> Result { let contract_class: ContractClassV0Inner = serde_json::from_str(raw_contract_class)?; Ok(ContractClassV0(Arc::new(contract_class))) @@ -185,6 +185,13 @@ impl ContractClassV1 { Some(self.0.entry_points_by_type[&EntryPointType::Constructor].first()?.selector) } + /// Returns the estimated VM resources required for computing Casm hash. + /// This is an empiric measurement of several bytecode lengths, which constitutes as the + /// dominant factor in it. + fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { + estimate_casm_hash_computation_resources(&self.bytecode_segment_lengths) + } + pub fn bytecode_length(&self) -> usize { self.program.data_len() } @@ -219,16 +226,16 @@ impl ContractClassV1 { } } - /// Returns the estimated VM resources required for computing Casm hash. - /// This is an empiric measurement of several bytecode lengths, which constitutes as the - /// dominant factor in it. - fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { - estimate_casm_hash_computation_resources(&self.bytecode_segment_lengths) + pub fn try_from_json_string(raw_contract_class: &str) -> Result { + let casm_contract_class: CasmContractClass = serde_json::from_str(raw_contract_class)?; + let contract_class: ContractClassV1 = casm_contract_class.try_into()?; + + Ok(contract_class) } // Returns the set of segments that were visited according to the given visited PCs. // Each visited segment must have its starting PC visited, and is represented by it. - fn get_visited_segments( + pub fn get_visited_segments( &self, visited_pcs: &HashSet, ) -> Result, TransactionExecutionError> { @@ -236,13 +243,6 @@ impl ContractClassV1 { get_visited_segments(&self.bytecode_segment_lengths, &mut reversed_visited_pcs, &mut 0) } - pub fn try_from_json_string(raw_contract_class: &str) -> Result { - let casm_contract_class: CasmContractClass = serde_json::from_str(raw_contract_class)?; - let contract_class: ContractClassV1 = casm_contract_class.try_into()?; - - Ok(contract_class) - } - /// Returns an empty contract class for testing purposes. #[cfg(any(feature = "testing", test))] pub fn empty_for_testing() -> Self { From 0d41708ba8380c86308d3d93263029bc67e246e4 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 13 Aug 2024 16:08:08 +0300 Subject: [PATCH 2/4] chore: move sn_api_to_cairo_vm_program into snapi --- Cargo.lock | 1 + .../src/execution/contract_class.rs | 2 +- .../src/execution/execution_utils.rs | 42 ------------------- crates/starknet_api/Cargo.toml | 1 + .../src/deprecated_contract_class.rs | 41 ++++++++++++++++++ 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5dec8d9a29..2f3555f1ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8772,6 +8772,7 @@ dependencies = [ "assert_matches", "bitvec", "cairo-lang-starknet-classes", + "cairo-vm", "derive_more", "hex", "indexmap 2.2.6", diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 8ea14307a6..488e6f8895 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -22,6 +22,7 @@ use serde::de::Error as DeserializationError; use serde::{Deserialize, Deserializer}; use starknet_api::core::EntryPointSelector; use starknet_api::deprecated_contract_class::{ + sn_api_to_cairo_vm_program, ContractClass as DeprecatedContractClass, EntryPoint, EntryPointOffset, @@ -35,7 +36,6 @@ use crate::abi::abi_utils::selector_from_name; use crate::abi::constants::{self, CONSTRUCTOR_ENTRY_POINT_NAME}; use crate::execution::entry_point::CallEntryPoint; use crate::execution::errors::{ContractClassError, PreExecutionError}; -use crate::execution::execution_utils::sn_api_to_cairo_vm_program; use crate::fee::eth_gas_constants; use crate::transaction::errors::TransactionExecutionError; diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index 7824e13873..ad2f2ffeed 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -1,16 +1,7 @@ use std::collections::HashMap; use cairo_lang_runner::casm_run::format_next_item; -use cairo_vm::serde::deserialize_program::{ - deserialize_array_of_bigint_hex, - Attribute, - HintParams, - Identifier, - ReferenceManager, -}; use cairo_vm::types::builtin_name::BuiltinName; -use cairo_vm::types::errors::program_errors::ProgramError; -use cairo_vm::types::program::Program; use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; @@ -18,7 +9,6 @@ use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResour use cairo_vm::vm::vm_core::VirtualMachine; use num_bigint::BigUint; use starknet_api::core::ClassHash; -use starknet_api::deprecated_contract_class::Program as DeprecatedProgram; use starknet_api::transaction::Calldata; use starknet_types_core::felt::Felt; @@ -116,38 +106,6 @@ pub fn felt_range_from_ptr( Ok(values) } -// TODO(Elin,01/05/2023): aim to use LC's implementation once it's in a separate crate. -pub fn sn_api_to_cairo_vm_program(program: DeprecatedProgram) -> Result { - let identifiers = serde_json::from_value::>(program.identifiers)?; - let builtins = serde_json::from_value(program.builtins)?; - let data = deserialize_array_of_bigint_hex(program.data)?; - let hints = serde_json::from_value::>>(program.hints)?; - let main = None; - let error_message_attributes = match program.attributes { - serde_json::Value::Null => vec![], - attributes => serde_json::from_value::>(attributes)? - .into_iter() - .filter(|attr| attr.name == "error_message") - .collect(), - }; - - let instruction_locations = None; - let reference_manager = serde_json::from_value::(program.reference_manager)?; - - let program = Program::new( - builtins, - data, - main, - hints, - reference_manager, - identifiers, - error_message_attributes, - instruction_locations, - )?; - - Ok(program) -} - #[derive(Debug)] // Invariant: read-only. pub struct ReadOnlySegment { diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index cc7a44a7f3..b8ee2dbcb3 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -12,6 +12,7 @@ testing = [] [dependencies] bitvec = "1.0.1" cairo-lang-starknet-classes = "2.7.0-dev.0" +cairo-vm.workspace = true derive_more = "0.99.17" hex = "0.4.3" indexmap = { version = "2.1.0", features = ["serde"] } diff --git a/crates/starknet_api/src/deprecated_contract_class.rs b/crates/starknet_api/src/deprecated_contract_class.rs index eb5de5db90..dcb359582a 100644 --- a/crates/starknet_api/src/deprecated_contract_class.rs +++ b/crates/starknet_api/src/deprecated_contract_class.rs @@ -2,6 +2,15 @@ use std::collections::HashMap; use std::num::ParseIntError; use cairo_lang_starknet_classes::casm_contract_class::CasmContractEntryPoint; +use cairo_vm::serde::deserialize_program::{ + deserialize_array_of_bigint_hex, + Attribute, + HintParams, + Identifier, + ReferenceManager, +}; +use cairo_vm::types::errors::program_errors::ProgramError; +use cairo_vm::types::program::Program as CairoVmProgram; use itertools::Itertools; use serde::de::Error as DeserializationError; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -140,6 +149,38 @@ pub struct Program { pub reference_manager: serde_json::Value, } +// TODO(Elin,01/05/2023): aim to use LC's implementation once it's in a separate crate. +pub fn sn_api_to_cairo_vm_program(program: Program) -> Result { + let identifiers = serde_json::from_value::>(program.identifiers)?; + let builtins = serde_json::from_value(program.builtins)?; + let data = deserialize_array_of_bigint_hex(program.data)?; + let hints = serde_json::from_value::>>(program.hints)?; + let main = None; + let error_message_attributes = match program.attributes { + serde_json::Value::Null => vec![], + attributes => serde_json::from_value::>(attributes)? + .into_iter() + .filter(|attr| attr.name == "error_message") + .collect(), + }; + + let instruction_locations = None; + let reference_manager = serde_json::from_value::(program.reference_manager)?; + + let program = CairoVmProgram::new( + builtins, + data, + main, + hints, + reference_manager, + identifiers, + error_message_attributes, + instruction_locations, + )?; + + Ok(program) +} + // Serialize hints as a sorted mapping for correct hash computation. fn serialize_hints_sorted(hints: &serde_json::Value, serializer: S) -> Result where From 0aa46412acfc284a6f1faa105210aded7c497ed4 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 13 Aug 2024 16:09:35 +0300 Subject: [PATCH 3/4] chore: remove redundent try_from methods for declare transaction --- .../src/transaction/transactions.rs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 262acbb657..64b8235de6 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -135,16 +135,6 @@ pub struct DeclareTransaction { pub class_info: ClassInfo, } -impl TryFrom for DeclareTransaction { - type Error = TransactionExecutionError; - - fn try_from( - declare_tx: starknet_api::executable_transaction::DeclareTransaction, - ) -> Result { - Self::new_from_executable_tx(declare_tx, false) - } -} - impl DeclareTransaction { fn create( declare_tx: starknet_api::transaction::DeclareTransaction, @@ -173,17 +163,6 @@ impl DeclareTransaction { Self::create(declare_tx, tx_hash, class_info, true) } - fn new_from_executable_tx( - declare_tx: starknet_api::executable_transaction::DeclareTransaction, - only_query: bool, - ) -> Result { - let starknet_api::executable_transaction::DeclareTransaction { tx, tx_hash, class_info } = - declare_tx; - let class_info: ClassInfo = class_info.try_into()?; - - Self::create(tx, tx_hash, class_info, only_query) - } - implement_inner_tx_getter_calls!( (class_hash, ClassHash), (nonce, Nonce), From 8945659d260b25ba8a24d7e485837643c621292f Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 14 Aug 2024 14:01:27 +0300 Subject: [PATCH 4/4] chore: move contract class structs to common --- Cargo.lock | 18 + Cargo.toml | 2 + crates/blockifier/Cargo.toml | 1 + .../src/blockifier/transaction_executor.rs | 1 + crates/blockifier/src/bouncer.rs | 1 + .../src/concurrency/versioned_state.rs | 2 +- .../src/execution/contract_class.rs | 345 +++++------------- .../src/execution/contract_class_test.rs | 3 +- .../deprecated_entry_point_execution.rs | 2 +- .../blockifier/src/execution/entry_point.rs | 1 + .../src/execution/entry_point_execution.rs | 3 +- .../src/execution/execution_utils.rs | 2 +- .../blockifier/src/execution/syscalls/mod.rs | 2 +- crates/blockifier/src/fee/actual_cost_test.rs | 1 + crates/blockifier/src/state/cached_state.rs | 2 +- crates/blockifier/src/state/global_cache.rs | 3 +- crates/blockifier/src/state/state_api.rs | 2 +- crates/blockifier/src/test_utils/contracts.rs | 3 +- crates/blockifier/src/test_utils/declare.rs | 2 +- .../src/test_utils/dict_state_reader.rs | 2 +- .../blockifier/src/test_utils/struct_impls.rs | 15 +- .../src/transaction/account_transaction.rs | 3 +- .../transaction/account_transactions_test.rs | 3 +- .../blockifier/src/transaction/test_utils.rs | 3 +- .../src/transaction/transaction_execution.rs | 2 +- .../src/transaction/transaction_utils.rs | 2 +- .../src/transaction/transactions.rs | 3 +- .../src/transaction/transactions_test.rs | 1 + crates/common/Cargo.toml | 20 + crates/common/src/contract_class.rs | 219 +++++++++++ crates/common/src/lib.rs | 1 + crates/gateway/Cargo.toml | 1 + crates/gateway/src/compilation.rs | 3 +- crates/gateway/src/compilation_test.rs | 3 +- crates/gateway/src/rpc_state_reader.rs | 2 +- crates/gateway/src/rpc_state_reader_test.rs | 2 +- crates/gateway/src/state_reader.rs | 2 +- crates/gateway/src/state_reader_test_utils.rs | 2 +- .../src/stateful_transaction_validator.rs | 2 +- crates/gateway/src/utils.rs | 2 +- crates/native_blockifier/Cargo.toml | 1 + .../src/py_block_executor_test.rs | 2 +- crates/native_blockifier/src/py_test_utils.rs | 3 +- .../native_blockifier/src/py_transaction.rs | 8 +- .../src/state_readers/papyrus_state.rs | 2 +- .../src/state_readers/py_state_reader.rs | 3 +- crates/papyrus_execution/Cargo.toml | 1 + .../papyrus_execution/src/execution_utils.rs | 10 +- crates/papyrus_execution/src/lib.rs | 3 +- crates/papyrus_execution/src/state_reader.rs | 6 +- .../src/state_reader_test.rs | 8 +- 51 files changed, 418 insertions(+), 318 deletions(-) create mode 100644 crates/common/Cargo.toml create mode 100644 crates/common/src/contract_class.rs create mode 100644 crates/common/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2f3555f1ec..a50797a3e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -982,6 +982,7 @@ dependencies = [ "cairo-lang-starknet-classes", "cairo-lang-utils", "cairo-vm", + "common", "criterion", "derive_more", "glob", @@ -1976,6 +1977,20 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "common" +version = "0.0.0" +dependencies = [ + "cairo-lang-casm", + "cairo-lang-starknet-classes", + "cairo-vm", + "derive_more", + "serde", + "serde_json", + "starknet-types-core", + "starknet_api", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -5597,6 +5612,7 @@ dependencies = [ "cached", "cairo-lang-starknet-classes", "cairo-vm", + "common", "indexmap 2.2.6", "log", "num-bigint 0.4.5", @@ -6215,6 +6231,7 @@ dependencies = [ "cairo-lang-starknet-classes", "cairo-lang-utils", "cairo-vm", + "common", "indexmap 2.2.6", "itertools 0.10.5", "lazy_static", @@ -8898,6 +8915,7 @@ dependencies = [ "blockifier", "cairo-lang-sierra-to-casm", "cairo-lang-starknet-classes", + "common", "enum-assoc", "hyper", "mempool_test_utils", diff --git a/Cargo.toml b/Cargo.toml index de307a04aa..8da2b7f689 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "crates/batcher_types", "crates/blockifier", "crates/committer_cli", + "crates/common", "crates/consensus_manager", "crates/consensus_manager_types", "crates/gateway", @@ -84,6 +85,7 @@ camelpaste = "0.1.0" chrono = "0.4.26" clap = "4.5.4" colored = "2.1.0" +common = { path = "crates/common", version = "0.0" } const_format = "0.2.30" criterion = "0.5.1" deadqueue = "0.2.4" diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 50b2247a17..a1917cb1c5 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -28,6 +28,7 @@ cairo-lang-runner.workspace = true cairo-lang-starknet-classes.workspace = true cairo-lang-utils.workspace = true cairo-vm.workspace = true +common.workspace = true derive_more.workspace = true indexmap.workspace = true itertools.workspace = true diff --git a/crates/blockifier/src/blockifier/transaction_executor.rs b/crates/blockifier/src/blockifier/transaction_executor.rs index 14c13ed932..2f43a466b9 100644 --- a/crates/blockifier/src/blockifier/transaction_executor.rs +++ b/crates/blockifier/src/blockifier/transaction_executor.rs @@ -17,6 +17,7 @@ use crate::bouncer::{Bouncer, BouncerWeights}; #[cfg(feature = "concurrency")] use crate::concurrency::worker_logic::WorkerExecutor; use crate::context::BlockContext; +use crate::execution::contract_class::ContractClassExt; use crate::state::cached_state::{CachedState, CommitmentStateDiff, TransactionalState}; use crate::state::errors::StateError; use crate::state::state_api::StateReader; diff --git a/crates/blockifier/src/bouncer.rs b/crates/blockifier/src/bouncer.rs index 0aa83d08ad..968cf91a2c 100644 --- a/crates/blockifier/src/bouncer.rs +++ b/crates/blockifier/src/bouncer.rs @@ -10,6 +10,7 @@ use crate::blockifier::transaction_executor::{ TransactionExecutorResult, }; use crate::execution::call_info::ExecutionSummary; +use crate::execution::contract_class::ContractClassExt; use crate::fee::gas_usage::get_onchain_data_segment_length; use crate::state::cached_state::{StateChangesKeys, StorageEntry}; use crate::state::state_api::StateReader; diff --git a/crates/blockifier/src/concurrency/versioned_state.rs b/crates/blockifier/src/concurrency/versioned_state.rs index a6edb590ee..8ca54a1447 100644 --- a/crates/blockifier/src/concurrency/versioned_state.rs +++ b/crates/blockifier/src/concurrency/versioned_state.rs @@ -1,13 +1,13 @@ use std::collections::{HashMap, HashSet}; use std::sync::{Arc, Mutex, MutexGuard}; +use common::contract_class::ContractClass; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; use starknet_types_core::felt::Felt; use crate::concurrency::versioned_storage::VersionedStorage; use crate::concurrency::TxIndex; -use crate::execution::contract_class::ContractClass; use crate::state::cached_state::{ContractClassMapping, StateMaps}; use crate::state::errors::StateError; use crate::state::state_api::{StateReader, StateResult, UpdatableState}; diff --git a/crates/blockifier/src/execution/contract_class.rs b/crates/blockifier/src/execution/contract_class.rs index 488e6f8895..9a009576e7 100644 --- a/crates/blockifier/src/execution/contract_class.rs +++ b/crates/blockifier/src/execution/contract_class.rs @@ -1,35 +1,22 @@ use std::collections::{HashMap, HashSet}; -use std::ops::Deref; use std::sync::Arc; -use cairo_lang_casm; -use cairo_lang_casm::hints::Hint; -use cairo_lang_starknet_classes::casm_contract_class::{CasmContractClass, CasmContractEntryPoint}; +use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_lang_starknet_classes::NestedIntList; -use cairo_vm::serde::deserialize_program::{ - ApTracking, - FlowTrackingData, - HintParams, - ReferenceManager, -}; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::types::errors::program_errors::ProgramError; -use cairo_vm::types::program::Program; -use cairo_vm::types::relocatable::MaybeRelocatable; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::{ + ClassInfo, + ContractClass, + ContractClassV0, + ContractClassV0Inner, + ContractClassV1, + EntryPointV1, +}; use itertools::Itertools; -use serde::de::Error as DeserializationError; -use serde::{Deserialize, Deserializer}; use starknet_api::core::EntryPointSelector; -use starknet_api::deprecated_contract_class::{ - sn_api_to_cairo_vm_program, - ContractClass as DeprecatedContractClass, - EntryPoint, - EntryPointOffset, - EntryPointType, - Program as DeprecatedProgram, -}; -use starknet_types_core::felt::Felt; +use starknet_api::deprecated_contract_class::EntryPointType; use super::execution_utils::poseidon_hash_many_cost; use crate::abi::abi_utils::selector_from_name; @@ -50,39 +37,32 @@ pub mod test; pub type ContractClassResult = Result; -#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)] -pub enum ContractClass { - V0(ContractClassV0), - V1(ContractClassV1), -} - -impl TryFrom for ContractClass { - type Error = ProgramError; - - fn try_from( - contract_class: starknet_api::contract_class::ContractClass, - ) -> Result { - let starknet_api::contract_class::ContractClass::V1(contract_class_v1) = contract_class; - Ok(ContractClass::V1(contract_class_v1.try_into()?)) - } +pub trait ContractClassExt { + fn constructor_selector(&self) -> Option; + fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources; + fn get_visited_segments( + &self, + visited_pcs: &HashSet, + ) -> Result, TransactionExecutionError>; + fn bytecode_length(&self) -> usize; } -impl ContractClass { - pub fn constructor_selector(&self) -> Option { +impl ContractClassExt for ContractClass { + fn constructor_selector(&self) -> Option { match self { ContractClass::V0(class) => class.constructor_selector(), ContractClass::V1(class) => class.constructor_selector(), } } - pub fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { + fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { match self { ContractClass::V0(class) => class.estimate_casm_hash_computation_resources(), ContractClass::V1(class) => class.estimate_casm_hash_computation_resources(), } } - pub fn get_visited_segments( + fn get_visited_segments( &self, visited_pcs: &HashSet, ) -> Result, TransactionExecutionError> { @@ -94,7 +74,7 @@ impl ContractClass { } } - pub fn bytecode_length(&self) -> usize { + fn bytecode_length(&self) -> usize { match self { ContractClass::V0(class) => class.bytecode_length(), ContractClass::V1(class) => class.bytecode_length(), @@ -103,17 +83,19 @@ impl ContractClass { } // V0. -#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] -pub struct ContractClassV0(pub Arc); -impl Deref for ContractClassV0 { - type Target = ContractClassV0Inner; +trait ContractClassV0PrivateExt { + fn constructor_selector(&self) -> Option; + fn n_entry_points(&self) -> usize; + fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources; +} - fn deref(&self) -> &Self::Target { - &self.0 - } +pub trait ContractClassV0Ext { + fn n_builtins(&self) -> usize; + fn bytecode_length(&self) -> usize; + fn try_from_json_string(raw_contract_class: &str) -> Result; } -impl ContractClassV0 { +impl ContractClassV0PrivateExt for ContractClassV0 { fn constructor_selector(&self) -> Option { Some(self.entry_points_by_type[&EntryPointType::Constructor].first()?.selector) } @@ -136,51 +118,43 @@ impl ContractClassV0 { builtin_instance_counter: HashMap::from([(BuiltinName::pedersen, hashed_data_size)]), } } +} - pub fn n_builtins(&self) -> usize { +impl ContractClassV0Ext for ContractClassV0 { + fn n_builtins(&self) -> usize { self.program.builtins_len() } - pub fn bytecode_length(&self) -> usize { + fn bytecode_length(&self) -> usize { self.program.data_len() } - pub fn try_from_json_string(raw_contract_class: &str) -> Result { + fn try_from_json_string(raw_contract_class: &str) -> Result { let contract_class: ContractClassV0Inner = serde_json::from_str(raw_contract_class)?; Ok(ContractClassV0(Arc::new(contract_class))) } } -#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] -pub struct ContractClassV0Inner { - #[serde(deserialize_with = "deserialize_program")] - pub program: Program, - pub entry_points_by_type: HashMap>, -} - -impl TryFrom for ContractClassV0 { - type Error = ProgramError; - - fn try_from(class: DeprecatedContractClass) -> Result { - Ok(Self(Arc::new(ContractClassV0Inner { - program: sn_api_to_cairo_vm_program(class.program)?, - entry_points_by_type: class.entry_points_by_type, - }))) - } -} - // V1. -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct ContractClassV1(pub Arc); -impl Deref for ContractClassV1 { - type Target = ContractClassV1Inner; +trait ContractClassV1PrivateExt { + fn constructor_selector(&self) -> Option; + fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources; +} - fn deref(&self) -> &Self::Target { - &self.0 - } +pub trait ContractClassV1Ext { + fn bytecode_length(&self) -> usize; + fn bytecode_segment_lengths(&self) -> &NestedIntList; + fn get_entry_point(&self, call: &CallEntryPoint) -> Result; + fn try_from_json_string(raw_contract_class: &str) -> Result; + fn get_visited_segments( + &self, + visited_pcs: &HashSet, + ) -> Result, TransactionExecutionError>; + #[cfg(any(feature = "testing", test))] + fn empty_for_testing() -> Self; } -impl ContractClassV1 { +impl ContractClassV1PrivateExt for ContractClassV1 { fn constructor_selector(&self) -> Option { Some(self.0.entry_points_by_type[&EntryPointType::Constructor].first()?.selector) } @@ -191,19 +165,18 @@ impl ContractClassV1 { fn estimate_casm_hash_computation_resources(&self) -> ExecutionResources { estimate_casm_hash_computation_resources(&self.bytecode_segment_lengths) } +} - pub fn bytecode_length(&self) -> usize { +impl ContractClassV1Ext for ContractClassV1 { + fn bytecode_length(&self) -> usize { self.program.data_len() } - pub fn bytecode_segment_lengths(&self) -> &NestedIntList { + fn bytecode_segment_lengths(&self) -> &NestedIntList { &self.bytecode_segment_lengths } - pub fn get_entry_point( - &self, - call: &CallEntryPoint, - ) -> Result { + fn get_entry_point(&self, call: &CallEntryPoint) -> Result { if call.entry_point_type == EntryPointType::Constructor && call.entry_point_selector != selector_from_name(CONSTRUCTOR_ENTRY_POINT_NAME) { @@ -226,16 +199,16 @@ impl ContractClassV1 { } } - pub fn try_from_json_string(raw_contract_class: &str) -> Result { + fn try_from_json_string(raw_contract_class: &str) -> Result { let casm_contract_class: CasmContractClass = serde_json::from_str(raw_contract_class)?; - let contract_class: ContractClassV1 = casm_contract_class.try_into()?; + let contract_class = casm_contract_class.try_into()?; Ok(contract_class) } // Returns the set of segments that were visited according to the given visited PCs. // Each visited segment must have its starting PC visited, and is represented by it. - pub fn get_visited_segments( + fn get_visited_segments( &self, visited_pcs: &HashSet, ) -> Result, TransactionExecutionError> { @@ -245,8 +218,8 @@ impl ContractClassV1 { /// Returns an empty contract class for testing purposes. #[cfg(any(feature = "testing", test))] - pub fn empty_for_testing() -> Self { - Self(Arc::new(ContractClassV1Inner { + fn empty_for_testing() -> Self { + Self(Arc::new(common::contract_class::ContractClassV1Inner { program: Default::default(), entry_points_by_type: Default::default(), hints: Default::default(), @@ -346,198 +319,44 @@ fn get_visited_segments( Ok(res) } -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct ContractClassV1Inner { - pub program: Program, - pub entry_points_by_type: HashMap>, - pub hints: HashMap, - bytecode_segment_lengths: NestedIntList, -} - -#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] -pub struct EntryPointV1 { - pub selector: EntryPointSelector, - pub offset: EntryPointOffset, - pub builtins: Vec, -} - -impl EntryPointV1 { - pub fn pc(&self) -> usize { - self.offset.0 - } -} - -impl TryFrom for ContractClassV1 { - type Error = ProgramError; - - fn try_from( - contract_class: starknet_api::contract_class::ContractClassV1, - ) -> Result { - let starknet_api::contract_class::ContractClassV1::Casm(casm_contract_class) = - contract_class; - casm_contract_class.try_into() - } -} - -impl TryFrom for ContractClassV1 { - type Error = ProgramError; - - fn try_from(class: CasmContractClass) -> Result { - let data: Vec = class - .bytecode - .into_iter() - .map(|x| MaybeRelocatable::from(Felt::from(x.value))) - .collect(); - - let mut hints: HashMap> = HashMap::new(); - for (i, hint_list) in class.hints.iter() { - let hint_params: Result, ProgramError> = - hint_list.iter().map(hint_to_hint_params).collect(); - hints.insert(*i, hint_params?); - } - - // Collect a sting to hint map so that the hint processor can fetch the correct [Hint] - // for each instruction. - let mut string_to_hint: HashMap = HashMap::new(); - for (_, hint_list) in class.hints.iter() { - for hint in hint_list.iter() { - string_to_hint.insert(serde_json::to_string(hint)?, hint.clone()); - } - } - - let builtins = vec![]; // The builtins are initialize later. - let main = Some(0); - let reference_manager = ReferenceManager { references: Vec::new() }; - let identifiers = HashMap::new(); - let error_message_attributes = vec![]; - let instruction_locations = None; - - let program = Program::new( - builtins, - data, - main, - hints, - reference_manager, - identifiers, - error_message_attributes, - instruction_locations, - )?; - - let mut entry_points_by_type = HashMap::new(); - entry_points_by_type.insert( - EntryPointType::Constructor, - convert_entry_points_v1(class.entry_points_by_type.constructor), - ); - entry_points_by_type.insert( - EntryPointType::External, - convert_entry_points_v1(class.entry_points_by_type.external), - ); - entry_points_by_type.insert( - EntryPointType::L1Handler, - convert_entry_points_v1(class.entry_points_by_type.l1_handler), - ); - - let bytecode_segment_lengths = class - .bytecode_segment_lengths - .unwrap_or_else(|| NestedIntList::Leaf(program.data_len())); - - Ok(Self(Arc::new(ContractClassV1Inner { - program, - entry_points_by_type, - hints: string_to_hint, - bytecode_segment_lengths, - }))) - } -} - -// V0 utilities. - -/// Converts the program type from SN API into a Cairo VM-compatible type. -pub fn deserialize_program<'de, D: Deserializer<'de>>( - deserializer: D, -) -> Result { - let deprecated_program = DeprecatedProgram::deserialize(deserializer)?; - sn_api_to_cairo_vm_program(deprecated_program) - .map_err(|err| DeserializationError::custom(err.to_string())) -} - -// V1 utilities. - -// TODO(spapini): Share with cairo-lang-runner. -fn hint_to_hint_params(hint: &cairo_lang_casm::hints::Hint) -> Result { - Ok(HintParams { - code: serde_json::to_string(hint)?, - accessible_scopes: vec![], - flow_tracking_data: FlowTrackingData { - ap_tracking: ApTracking::new(), - reference_ids: HashMap::new(), - }, - }) -} - -fn convert_entry_points_v1(external: Vec) -> Vec { - external - .into_iter() - .map(|ep| EntryPointV1 { - selector: EntryPointSelector(Felt::from(ep.selector)), - offset: EntryPointOffset(ep.offset), - builtins: ep - .builtins - .into_iter() - .map(|builtin| BuiltinName::from_str(&builtin).expect("Unrecognized builtin.")) - .collect(), - }) - .collect() -} - -#[derive(Clone, Debug)] -// TODO(Ayelet,10/02/2024): Change to bytes. -pub struct ClassInfo { - contract_class: ContractClass, - sierra_program_length: usize, - abi_length: usize, -} - -impl TryFrom for ClassInfo { - type Error = ProgramError; - - fn try_from(class_info: starknet_api::contract_class::ClassInfo) -> Result { - let starknet_api::contract_class::ClassInfo { - contract_class, - sierra_program_length, - abi_length, - } = class_info; - - let contract_class: ContractClass = contract_class.try_into()?; - Ok(Self { contract_class, sierra_program_length, abi_length }) - } +pub trait ClassInfoExt: Sized { + fn bytecode_length(&self) -> usize; + fn contract_class(&self) -> ContractClass; + fn sierra_program_length(&self) -> usize; + fn abi_length(&self) -> usize; + fn code_size(&self) -> usize; + fn new( + contract_class: &ContractClass, + sierra_program_length: usize, + abi_length: usize, + ) -> ContractClassResult; } -impl ClassInfo { - pub fn bytecode_length(&self) -> usize { +impl ClassInfoExt for ClassInfo { + fn bytecode_length(&self) -> usize { self.contract_class.bytecode_length() } - pub fn contract_class(&self) -> ContractClass { + fn contract_class(&self) -> ContractClass { self.contract_class.clone() } - pub fn sierra_program_length(&self) -> usize { + fn sierra_program_length(&self) -> usize { self.sierra_program_length } - pub fn abi_length(&self) -> usize { + fn abi_length(&self) -> usize { self.abi_length } - pub fn code_size(&self) -> usize { + fn code_size(&self) -> usize { (self.bytecode_length() + self.sierra_program_length()) // We assume each felt is a word. * eth_gas_constants::WORD_WIDTH + self.abi_length() } - pub fn new( + fn new( contract_class: &ContractClass, sierra_program_length: usize, abi_length: usize, diff --git a/crates/blockifier/src/execution/contract_class_test.rs b/crates/blockifier/src/execution/contract_class_test.rs index 2bc8ab09a5..c7a99cca87 100644 --- a/crates/blockifier/src/execution/contract_class_test.rs +++ b/crates/blockifier/src/execution/contract_class_test.rs @@ -3,9 +3,10 @@ use std::sync::Arc; use assert_matches::assert_matches; use cairo_lang_starknet_classes::NestedIntList; +use common::contract_class::{ContractClassV1, ContractClassV1Inner}; use rstest::rstest; -use crate::execution::contract_class::{ContractClassV1, ContractClassV1Inner}; +use crate::execution::contract_class::ContractClassV1Ext; use crate::transaction::errors::TransactionExecutionError; #[rstest] diff --git a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs index c5021fe5fb..ad602283ca 100644 --- a/crates/blockifier/src/execution/deprecated_entry_point_execution.rs +++ b/crates/blockifier/src/execution/deprecated_entry_point_execution.rs @@ -5,6 +5,7 @@ use cairo_vm::types::layout_name::LayoutName; use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources}; +use common::contract_class::ContractClassV0; use starknet_api::core::EntryPointSelector; use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::hash::StarkHash; @@ -13,7 +14,6 @@ use super::execution_utils::SEGMENT_ARENA_BUILTIN_SIZE; use crate::abi::abi_utils::selector_from_name; use crate::abi::constants::{CONSTRUCTOR_ENTRY_POINT_NAME, DEFAULT_ENTRY_POINT_SELECTOR}; use crate::execution::call_info::{CallExecution, CallInfo}; -use crate::execution::contract_class::ContractClassV0; use crate::execution::deprecated_syscalls::hint_processor::DeprecatedSyscallHintProcessor; use crate::execution::entry_point::{ CallEntryPoint, diff --git a/crates/blockifier/src/execution/entry_point.rs b/crates/blockifier/src/execution/entry_point.rs index a2b1482f53..f5bf293485 100644 --- a/crates/blockifier/src/execution/entry_point.rs +++ b/crates/blockifier/src/execution/entry_point.rs @@ -15,6 +15,7 @@ use crate::abi::constants; use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::CallInfo; use crate::execution::common_hints::ExecutionMode; +use crate::execution::contract_class::ContractClassExt; use crate::execution::errors::{ ConstructorEntryPointExecutionError, EntryPointExecutionError, diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 3ebae0ad8e..483564e656 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -9,12 +9,13 @@ use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use cairo_vm::vm::runners::builtin_runner::BuiltinRunner; use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources}; use cairo_vm::vm::security::verify_secure_runner; +use common::contract_class::{ContractClassV1, EntryPointV1}; use num_traits::{ToPrimitive, Zero}; use starknet_api::felt; use starknet_types_core::felt::Felt; use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; -use crate::execution::contract_class::{ContractClassV1, EntryPointV1}; +use crate::execution::contract_class::ContractClassV1Ext; use crate::execution::entry_point::{ CallEntryPoint, EntryPointExecutionContext, diff --git a/crates/blockifier/src/execution/execution_utils.rs b/crates/blockifier/src/execution/execution_utils.rs index ad2f2ffeed..84171372ba 100644 --- a/crates/blockifier/src/execution/execution_utils.rs +++ b/crates/blockifier/src/execution/execution_utils.rs @@ -7,6 +7,7 @@ use cairo_vm::vm::errors::memory_errors::MemoryError; use cairo_vm::vm::errors::vm_errors::VirtualMachineError; use cairo_vm::vm::runners::cairo_runner::{CairoArg, CairoRunner, ExecutionResources}; use cairo_vm::vm::vm_core::VirtualMachine; +use common::contract_class::ContractClass; use num_bigint::BigUint; use starknet_api::core::ClassHash; use starknet_api::transaction::Calldata; @@ -15,7 +16,6 @@ use starknet_types_core::felt::Felt; use super::entry_point::ConstructorEntryPointExecutionResult; use super::errors::ConstructorEntryPointExecutionError; use crate::execution::call_info::{CallInfo, Retdata}; -use crate::execution::contract_class::ContractClass; use crate::execution::entry_point::{ execute_constructor_entry_point, CallEntryPoint, diff --git a/crates/blockifier/src/execution/syscalls/mod.rs b/crates/blockifier/src/execution/syscalls/mod.rs index 7e253f247b..93df5930c4 100644 --- a/crates/blockifier/src/execution/syscalls/mod.rs +++ b/crates/blockifier/src/execution/syscalls/mod.rs @@ -1,5 +1,6 @@ use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable}; use cairo_vm::vm::vm_core::VirtualMachine; +use common::contract_class::ContractClass; use num_traits::ToPrimitive; use starknet_api::block::{BlockHash, BlockNumber}; use starknet_api::core::{ @@ -37,7 +38,6 @@ use self::hint_processor::{ }; use crate::abi::constants; use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message}; -use crate::execution::contract_class::ContractClass; use crate::execution::deprecated_syscalls::DeprecatedSyscallSelector; use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext}; use crate::execution::execution_utils::{ diff --git a/crates/blockifier/src/fee/actual_cost_test.rs b/crates/blockifier/src/fee/actual_cost_test.rs index acddd480c1..5473ab8a3c 100644 --- a/crates/blockifier/src/fee/actual_cost_test.rs +++ b/crates/blockifier/src/fee/actual_cost_test.rs @@ -4,6 +4,7 @@ use starknet_types_core::felt::Felt; use crate::context::BlockContext; use crate::execution::call_info::{CallExecution, CallInfo, MessageToL1, OrderedL2ToL1Message}; +use crate::execution::contract_class::ClassInfoExt; use crate::fee::eth_gas_constants; use crate::fee::gas_usage::{ get_consumed_message_to_l2_emissions_cost, diff --git a/crates/blockifier/src/state/cached_state.rs b/crates/blockifier/src/state/cached_state.rs index 93201f3456..e1bf390b0e 100644 --- a/crates/blockifier/src/state/cached_state.rs +++ b/crates/blockifier/src/state/cached_state.rs @@ -1,6 +1,7 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; +use common::contract_class::ContractClass; use derive_more::IntoIterator; use indexmap::IndexMap; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; @@ -9,7 +10,6 @@ use starknet_types_core::felt::Felt; use crate::abi::abi_utils::get_fee_token_var_address; use crate::context::TransactionContext; -use crate::execution::contract_class::ContractClass; use crate::state::errors::StateError; use crate::state::state_api::{State, StateReader, StateResult, UpdatableState}; use crate::transaction::objects::TransactionExecutionInfo; diff --git a/crates/blockifier/src/state/global_cache.rs b/crates/blockifier/src/state/global_cache.rs index 54d71a1fac..2a0e8de908 100644 --- a/crates/blockifier/src/state/global_cache.rs +++ b/crates/blockifier/src/state/global_cache.rs @@ -1,10 +1,9 @@ use std::sync::{Arc, Mutex, MutexGuard}; use cached::{Cached, SizedCache}; +use common::contract_class::ContractClass; use starknet_api::core::ClassHash; -use crate::execution::contract_class::ContractClass; - // Note: `ContractClassLRUCache` key-value types must align with `ContractClassMapping`. type ContractClassLRUCache = SizedCache; pub type LockedContractClassCache<'a> = MutexGuard<'a, ContractClassLRUCache>; diff --git a/crates/blockifier/src/state/state_api.rs b/crates/blockifier/src/state/state_api.rs index 5d3c308e2f..7de483175e 100644 --- a/crates/blockifier/src/state/state_api.rs +++ b/crates/blockifier/src/state/state_api.rs @@ -1,5 +1,6 @@ use std::collections::{HashMap, HashSet}; +use common::contract_class::ContractClass; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; use starknet_types_core::felt::Felt; @@ -7,7 +8,6 @@ use starknet_types_core::felt::Felt; use super::cached_state::{ContractClassMapping, StateMaps}; use crate::abi::abi_utils::get_fee_token_var_address; use crate::abi::sierra_types::next_storage_key; -use crate::execution::contract_class::ContractClass; use crate::state::errors::StateError; pub type StateResult = Result; diff --git a/crates/blockifier/src/test_utils/contracts.rs b/crates/blockifier/src/test_utils/contracts.rs index 81c736e681..d092fc4bb9 100644 --- a/crates/blockifier/src/test_utils/contracts.rs +++ b/crates/blockifier/src/test_utils/contracts.rs @@ -1,3 +1,4 @@ +use common::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use starknet_api::core::{ ClassHash, CompiledClassHash, @@ -17,8 +18,8 @@ use strum_macros::EnumIter; use crate::abi::abi_utils::selector_from_name; use crate::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME; -use crate::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use crate::test_utils::cairo_compile::{cairo0_compile, cairo1_compile}; +use crate::test_utils::struct_impls::FromFileExt; use crate::test_utils::{get_raw_contract_class, CairoVersion}; // This file contains featured contracts, used for tests. Use the function 'test_state' in diff --git a/crates/blockifier/src/test_utils/declare.rs b/crates/blockifier/src/test_utils/declare.rs index 6973d18ca7..a3324d01b8 100644 --- a/crates/blockifier/src/test_utils/declare.rs +++ b/crates/blockifier/src/test_utils/declare.rs @@ -1,3 +1,4 @@ +use common::contract_class::ClassInfo; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::transaction::{ @@ -14,7 +15,6 @@ use starknet_api::transaction::{ TransactionVersion, }; -use crate::execution::contract_class::ClassInfo; use crate::test_utils::default_testing_resource_bounds; use crate::transaction::account_transaction::AccountTransaction; use crate::transaction::transactions::DeclareTransaction; diff --git a/crates/blockifier/src/test_utils/dict_state_reader.rs b/crates/blockifier/src/test_utils/dict_state_reader.rs index 54fcd890e2..b5bef64669 100644 --- a/crates/blockifier/src/test_utils/dict_state_reader.rs +++ b/crates/blockifier/src/test_utils/dict_state_reader.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; +use common::contract_class::ContractClass; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; use starknet_types_core::felt::Felt; -use crate::execution::contract_class::ContractClass; use crate::state::cached_state::StorageEntry; use crate::state::errors::StateError; use crate::state::state_api::{StateReader, StateResult}; diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index 664041671c..6f8768ebaf 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::{ContractClassV0, ContractClassV1}; use serde_json::Value; use starknet_api::block::{BlockNumber, BlockTimestamp}; use starknet_api::core::{ChainId, ContractAddress, Nonce, PatriciaKey}; @@ -14,7 +15,7 @@ use crate::blockifier::block::{BlockInfo, GasPrices}; use crate::bouncer::{BouncerConfig, BouncerWeights}; use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; -use crate::execution::contract_class::{ContractClassV0, ContractClassV1}; +use crate::execution::contract_class::{ContractClassV0Ext, ContractClassV1Ext}; use crate::execution::entry_point::{ CallEntryPoint, EntryPointExecutionContext, @@ -220,15 +221,19 @@ impl CallExecution { // Contract loaders. -impl ContractClassV0 { - pub fn from_file(contract_path: &str) -> Self { +pub trait FromFileExt { + fn from_file(contract_path: &str) -> Self; +} + +impl FromFileExt for ContractClassV0 { + fn from_file(contract_path: &str) -> Self { let raw_contract_class = get_raw_contract_class(contract_path); Self::try_from_json_string(&raw_contract_class).unwrap() } } -impl ContractClassV1 { - pub fn from_file(contract_path: &str) -> Self { +impl FromFileExt for ContractClassV1 { + fn from_file(contract_path: &str) -> Self { let raw_contract_class = get_raw_contract_class(contract_path); Self::try_from_json_string(&raw_contract_class).unwrap() } diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 2395274b70..b5c6f2e24f 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::ContractClass; use starknet_api::calldata; use starknet_api::core::{ContractAddress, EntryPointSelector}; use starknet_api::deprecated_contract_class::EntryPointType; @@ -16,7 +17,7 @@ use starknet_types_core::felt::Felt; use crate::abi::abi_utils::selector_from_name; use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::{CallInfo, Retdata}; -use crate::execution::contract_class::ContractClass; +use crate::execution::contract_class::ClassInfoExt; use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext}; use crate::fee::actual_cost::TransactionReceipt; use crate::fee::fee_checks::{FeeCheckReportFields, PostExecutionReport}; diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 3f48297d15..5c3d196359 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ResourceTracker; +use common::contract_class::{ContractClass, ContractClassV1}; use pretty_assertions::assert_eq; use rstest::rstest; use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress, PatriciaKey}; @@ -26,7 +27,7 @@ use crate::abi::abi_utils::{ selector_from_name, }; use crate::context::BlockContext; -use crate::execution::contract_class::{ContractClass, ContractClassV1}; +use crate::execution::contract_class::ContractClassV1Ext; use crate::execution::entry_point::EntryPointExecutionContext; use crate::execution::syscalls::SyscallSelector; use crate::fee::fee_utils::{get_fee_by_gas_vector, get_sequencer_balance_keys}; diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index d6b1724e3a..33824cc38f 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -1,3 +1,4 @@ +use common::contract_class::{ClassInfo, ContractClass}; use rstest::fixture; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::transaction::{ @@ -20,7 +21,7 @@ use strum::IntoEnumIterator; use crate::abi::abi_utils::get_fee_token_var_address; use crate::context::{BlockContext, ChainInfo}; -use crate::execution::contract_class::{ClassInfo, ContractClass}; +use crate::execution::contract_class::ClassInfoExt; use crate::state::cached_state::CachedState; use crate::state::state_api::State; use crate::test_utils::contracts::FeatureContract; diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index f2fb174a84..3e4ada1983 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -1,12 +1,12 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::ClassInfo; use starknet_api::core::{calculate_contract_address, ContractAddress}; use starknet_api::transaction::{Fee, Transaction as StarknetApiTransaction, TransactionHash}; use crate::bouncer::verify_tx_weights_in_bounds; use crate::context::BlockContext; -use crate::execution::contract_class::ClassInfo; use crate::execution::entry_point::EntryPointExecutionContext; use crate::fee::actual_cost::TransactionReceipt; use crate::state::cached_state::TransactionalState; diff --git a/crates/blockifier/src/transaction/transaction_utils.rs b/crates/blockifier/src/transaction/transaction_utils.rs index 1f4edc40c4..6758655242 100644 --- a/crates/blockifier/src/transaction/transaction_utils.rs +++ b/crates/blockifier/src/transaction/transaction_utils.rs @@ -1,7 +1,7 @@ +use common::contract_class::ContractClass; use starknet_api::transaction::TransactionVersion; use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::ContractClass; use crate::transaction::errors::TransactionExecutionError; pub fn update_remaining_gas(remaining_gas: &mut u64, call_info: &CallInfo) { diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 64b8235de6..12595dfcf8 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::{ClassInfo, ContractClass}; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::transaction::{ @@ -18,7 +19,7 @@ use starknet_api::transaction::{ use crate::abi::abi_utils::selector_from_name; use crate::context::{BlockContext, TransactionContext}; use crate::execution::call_info::CallInfo; -use crate::execution::contract_class::{ClassInfo, ContractClass}; +use crate::execution::contract_class::ClassInfoExt; use crate::execution::entry_point::{ CallEntryPoint, CallType, diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index a8198cfa9b..2c98cc8e9f 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -43,6 +43,7 @@ use crate::execution::call_info::{ OrderedL2ToL1Message, Retdata, }; +use crate::execution::contract_class::ClassInfoExt; use crate::execution::entry_point::{CallEntryPoint, CallType}; use crate::execution::errors::{ConstructorEntryPointExecutionError, EntryPointExecutionError}; use crate::execution::syscalls::hint_processor::{EmitEventError, L1_GAS, L2_GAS}; diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml new file mode 100644 index 0000000000..0545f49882 --- /dev/null +++ b/crates/common/Cargo.toml @@ -0,0 +1,20 @@ +[package] +edition.workspace = true +license.workspace = true +name = "common" +repository.workspace = true +version = "0.0.0" +description = "Common types for the sequencer repository." + +[features] +testing = [] + +[dependencies] +cairo-lang-casm.workspace = true +cairo-lang-starknet-classes.workspace = true +cairo-vm.workspace = true +derive_more.workspace = true +serde.workspace = true +serde_json.workspace = true +starknet-types-core.workspace = true +starknet_api.workspace = true diff --git a/crates/common/src/contract_class.rs b/crates/common/src/contract_class.rs new file mode 100644 index 0000000000..d0480cb4f2 --- /dev/null +++ b/crates/common/src/contract_class.rs @@ -0,0 +1,219 @@ +use std::collections::HashMap; +use std::ops::Deref; +use std::sync::Arc; + +use cairo_lang_casm::hints::Hint; +use cairo_lang_starknet_classes::casm_contract_class::{CasmContractClass, CasmContractEntryPoint}; +use cairo_lang_starknet_classes::NestedIntList; +use cairo_vm::serde::deserialize_program::{ + ApTracking, + FlowTrackingData, + HintParams, + ReferenceManager, +}; +use cairo_vm::types::builtin_name::BuiltinName; +use cairo_vm::types::errors::program_errors::ProgramError; +use cairo_vm::types::program::Program as CairoVmProgram; +use cairo_vm::types::relocatable::MaybeRelocatable; +use serde::de::Error as DeserializationError; +use serde::{Deserialize, Deserializer}; +use starknet_api::core::EntryPointSelector; +use starknet_api::deprecated_contract_class::{ + sn_api_to_cairo_vm_program, + ContractClass as DeprecatedContractClass, + EntryPoint, + EntryPointOffset, + EntryPointType, + Program as DeprecatedProgram, +}; +use starknet_types_core::felt::Felt; + +/// Executable contract class. +#[derive(Clone, Debug, Eq, PartialEq, derive_more::From)] +pub enum ContractClass { + V0(ContractClassV0), + V1(ContractClassV1), +} + +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] +pub struct ContractClassV0(pub Arc); +impl Deref for ContractClassV0 { + type Target = ContractClassV0Inner; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] +pub struct ContractClassV0Inner { + #[serde(deserialize_with = "deserialize_program")] + pub program: CairoVmProgram, + pub entry_points_by_type: HashMap>, +} + +impl TryFrom for ContractClassV0 { + type Error = ProgramError; + + fn try_from(class: DeprecatedContractClass) -> Result { + Ok(Self(Arc::new(ContractClassV0Inner { + program: sn_api_to_cairo_vm_program(class.program)?, + entry_points_by_type: class.entry_points_by_type, + }))) + } +} + +// V1. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ContractClassV1(pub Arc); +impl Deref for ContractClassV1 { + type Target = ContractClassV1Inner; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ContractClassV1Inner { + pub program: CairoVmProgram, + pub entry_points_by_type: HashMap>, + pub hints: HashMap, + pub bytecode_segment_lengths: NestedIntList, +} + +impl TryFrom for ContractClassV1 { + type Error = ProgramError; + + fn try_from(class: CasmContractClass) -> Result { + let data: Vec = class + .bytecode + .into_iter() + .map(|x| MaybeRelocatable::from(Felt::from(x.value))) + .collect(); + + let mut hints: HashMap> = HashMap::new(); + for (i, hint_list) in class.hints.iter() { + let hint_params: Result, ProgramError> = + hint_list.iter().map(hint_to_hint_params).collect(); + hints.insert(*i, hint_params?); + } + + // Collect a sting to hint map so that the hint processor can fetch the correct [Hint] + // for each instruction. + let mut string_to_hint: HashMap = HashMap::new(); + for (_, hint_list) in class.hints.iter() { + for hint in hint_list.iter() { + string_to_hint.insert(serde_json::to_string(hint)?, hint.clone()); + } + } + + let builtins = vec![]; // The builtins are initialize later. + let main = Some(0); + let reference_manager = ReferenceManager { references: Vec::new() }; + let identifiers = HashMap::new(); + let error_message_attributes = vec![]; + let instruction_locations = None; + + let program = CairoVmProgram::new( + builtins, + data, + main, + hints, + reference_manager, + identifiers, + error_message_attributes, + instruction_locations, + )?; + + let mut entry_points_by_type = HashMap::new(); + entry_points_by_type.insert( + EntryPointType::Constructor, + convert_entry_points_v1(class.entry_points_by_type.constructor), + ); + entry_points_by_type.insert( + EntryPointType::External, + convert_entry_points_v1(class.entry_points_by_type.external), + ); + entry_points_by_type.insert( + EntryPointType::L1Handler, + convert_entry_points_v1(class.entry_points_by_type.l1_handler), + ); + + let bytecode_segment_lengths = class + .bytecode_segment_lengths + .unwrap_or_else(|| NestedIntList::Leaf(program.data_len())); + + Ok(Self(Arc::new(ContractClassV1Inner { + program, + entry_points_by_type, + hints: string_to_hint, + bytecode_segment_lengths, + }))) + } +} + +#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] +pub struct EntryPointV1 { + pub selector: EntryPointSelector, + pub offset: EntryPointOffset, + pub builtins: Vec, +} + +impl EntryPointV1 { + pub fn pc(&self) -> usize { + self.offset.0 + } +} + +// V0 utilities. + +/// Converts the program type from SN API into a Cairo VM-compatible type. +pub fn deserialize_program<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result { + let deprecated_program = DeprecatedProgram::deserialize(deserializer)?; + sn_api_to_cairo_vm_program(deprecated_program) + .map_err(|err| DeserializationError::custom(err.to_string())) +} + +// V1 utilities. + +// TODO(spapini): Share with cairo-lang-runner. +/// Used in TryFrom. +fn hint_to_hint_params(hint: &cairo_lang_casm::hints::Hint) -> Result { + Ok(HintParams { + code: serde_json::to_string(hint)?, + accessible_scopes: vec![], + flow_tracking_data: FlowTrackingData { + ap_tracking: ApTracking::new(), + reference_ids: HashMap::new(), + }, + }) +} + +/// Used in TryFrom. +fn convert_entry_points_v1(external: Vec) -> Vec { + external + .into_iter() + .map(|ep| EntryPointV1 { + selector: EntryPointSelector(Felt::from(ep.selector)), + offset: EntryPointOffset(ep.offset), + builtins: ep + .builtins + .into_iter() + .map(|builtin| BuiltinName::from_str(&builtin).expect("Unrecognized builtin.")) + .collect(), + }) + .collect() +} + +/// All relevant information about a declared contract class, including the compiled contract class +/// and other parameters derived from the original declare transaction required for billing. +#[derive(Clone, Debug, Eq, PartialEq)] +// TODO(Ayelet,10/02/2024): Change to bytes. +pub struct ClassInfo { + pub contract_class: ContractClass, + pub sierra_program_length: usize, + pub abi_length: usize, +} diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs new file mode 100644 index 0000000000..571760b638 --- /dev/null +++ b/crates/common/src/lib.rs @@ -0,0 +1 @@ +pub mod contract_class; diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index ac64da36e8..7ff322629a 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -16,6 +16,7 @@ async-trait.workspace = true axum.workspace = true blockifier = { workspace = true, features = ["testing"] } cairo-lang-starknet-classes.workspace = true +common.workspace = true enum-assoc.workspace = true hyper.workspace = true mempool_test_utils.workspace = true diff --git a/crates/gateway/src/compilation.rs b/crates/gateway/src/compilation.rs index fbd687e802..699d8ec4f8 100644 --- a/crates/gateway/src/compilation.rs +++ b/crates/gateway/src/compilation.rs @@ -1,8 +1,9 @@ use std::sync::Arc; -use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractClassV1}; +use blockifier::execution::contract_class::ClassInfoExt; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_lang_starknet_classes::contract_class::ContractClass as CairoLangContractClass; +use common::contract_class::{ClassInfo, ContractClass, ContractClassV1}; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::RpcDeclareTransaction; use starknet_sierra_compile::cairo_lang_compiler::CairoLangSierraToCasmCompiler; diff --git a/crates/gateway/src/compilation_test.rs b/crates/gateway/src/compilation_test.rs index fd5018ee52..d5d57f42c7 100644 --- a/crates/gateway/src/compilation_test.rs +++ b/crates/gateway/src/compilation_test.rs @@ -1,8 +1,9 @@ use assert_matches::assert_matches; -use blockifier::execution::contract_class::ContractClass; +use blockifier::execution::contract_class::ClassInfoExt; use cairo_lang_sierra_to_casm::compiler::CompilationError; use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError; use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError; +use common::contract_class::ContractClass; use mempool_test_utils::starknet_api_test_utils::declare_tx as rpc_declare_tx; use rstest::{fixture, rstest}; use starknet_api::core::CompiledClassHash; diff --git a/crates/gateway/src/rpc_state_reader.rs b/crates/gateway/src/rpc_state_reader.rs index 60fec240bd..011af28973 100644 --- a/crates/gateway/src/rpc_state_reader.rs +++ b/crates/gateway/src/rpc_state_reader.rs @@ -1,7 +1,7 @@ use blockifier::blockifier::block::BlockInfo; -use blockifier::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; +use common::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use papyrus_rpc::CompiledContractClass; use reqwest::blocking::Client as BlockingClient; use serde::Serialize; diff --git a/crates/gateway/src/rpc_state_reader_test.rs b/crates/gateway/src/rpc_state_reader_test.rs index af344104a0..c61266b446 100644 --- a/crates/gateway/src/rpc_state_reader_test.rs +++ b/crates/gateway/src/rpc_state_reader_test.rs @@ -1,6 +1,6 @@ -use blockifier::execution::contract_class::ContractClass; use blockifier::state::state_api::StateReader; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +use common::contract_class::ContractClass; use papyrus_rpc::CompiledContractClass; use serde::Serialize; use serde_json::json; diff --git a/crates/gateway/src/state_reader.rs b/crates/gateway/src/state_reader.rs index 0aa555b050..59103a0a1b 100644 --- a/crates/gateway/src/state_reader.rs +++ b/crates/gateway/src/state_reader.rs @@ -1,7 +1,7 @@ use blockifier::blockifier::block::BlockInfo; -use blockifier::execution::contract_class::ContractClass; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; +use common::contract_class::ContractClass; #[cfg(test)] use mockall::automock; use starknet_api::block::BlockNumber; diff --git a/crates/gateway/src/state_reader_test_utils.rs b/crates/gateway/src/state_reader_test_utils.rs index a806bed9b6..35bdd1370a 100644 --- a/crates/gateway/src/state_reader_test_utils.rs +++ b/crates/gateway/src/state_reader_test_utils.rs @@ -1,12 +1,12 @@ use blockifier::blockifier::block::BlockInfo; use blockifier::context::BlockContext; -use blockifier::execution::contract_class::ContractClass; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; use blockifier::test_utils::contracts::FeatureContract; use blockifier::test_utils::dict_state_reader::DictStateReader; use blockifier::test_utils::initial_test_state::test_state; use blockifier::test_utils::{CairoVersion, BALANCE}; +use common::contract_class::ContractClass; use starknet_api::block::BlockNumber; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; diff --git a/crates/gateway/src/stateful_transaction_validator.rs b/crates/gateway/src/stateful_transaction_validator.rs index 8d42adff98..4e549d02d8 100644 --- a/crates/gateway/src/stateful_transaction_validator.rs +++ b/crates/gateway/src/stateful_transaction_validator.rs @@ -5,10 +5,10 @@ use blockifier::blockifier::stateful_validator::{ }; use blockifier::bouncer::BouncerConfig; use blockifier::context::BlockContext; -use blockifier::execution::contract_class::ClassInfo; use blockifier::state::cached_state::CachedState; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::versioned_constants::VersionedConstants; +use common::contract_class::ClassInfo; #[cfg(test)] use mockall::automock; use starknet_api::core::{ContractAddress, Nonce}; diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index 27d3f1fabd..7d5bdb318a 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -1,10 +1,10 @@ -use blockifier::execution::contract_class::ClassInfo; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transactions::{ DeclareTransaction as BlockifierDeclareTransaction, DeployAccountTransaction as BlockifierDeployAccountTransaction, InvokeTransaction as BlockifierInvokeTransaction, }; +use common::contract_class::ClassInfo; use starknet_api::core::{calculate_contract_address, ChainId, ClassHash, ContractAddress}; use starknet_api::rpc_transaction::{ RpcDeclareTransaction, diff --git a/crates/native_blockifier/Cargo.toml b/crates/native_blockifier/Cargo.toml index 37d59a8106..6104c51bc4 100644 --- a/crates/native_blockifier/Cargo.toml +++ b/crates/native_blockifier/Cargo.toml @@ -30,6 +30,7 @@ crate-type = ["cdylib"] blockifier = { workspace = true, features = ["concurrency", "testing"] } cairo-lang-starknet-classes.workspace = true cairo-vm.workspace = true +common.workspace = true indexmap.workspace = true log.workspace = true num-bigint.workspace = true diff --git a/crates/native_blockifier/src/py_block_executor_test.rs b/crates/native_blockifier/src/py_block_executor_test.rs index ffc5908ad8..e4687cf270 100644 --- a/crates/native_blockifier/src/py_block_executor_test.rs +++ b/crates/native_blockifier/src/py_block_executor_test.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; use blockifier::blockifier::transaction_executor::BLOCK_STATE_ACCESS_ERR; -use blockifier::execution::contract_class::{ContractClass, ContractClassV1}; use blockifier::state::state_api::StateReader; use cached::Cached; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; +use common::contract_class::{ContractClass, ContractClassV1}; use pretty_assertions::assert_eq; use starknet_api::core::ClassHash; use starknet_api::{class_hash, felt}; diff --git a/crates/native_blockifier/src/py_test_utils.rs b/crates/native_blockifier/src/py_test_utils.rs index 0e66423790..5722415315 100644 --- a/crates/native_blockifier/src/py_test_utils.rs +++ b/crates/native_blockifier/src/py_test_utils.rs @@ -1,8 +1,9 @@ use std::collections::HashMap; -use blockifier::execution::contract_class::ContractClassV0; use blockifier::state::cached_state::CachedState; use blockifier::test_utils::dict_state_reader::DictStateReader; +use blockifier::test_utils::struct_impls::FromFileExt; +use common::contract_class::ContractClassV0; use starknet_api::core::ClassHash; use starknet_api::{class_hash, felt}; diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index 7b84d89d71..e091648095 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -1,14 +1,10 @@ use std::collections::BTreeMap; -use blockifier::execution::contract_class::{ - ClassInfo, - ContractClass, - ContractClassV0, - ContractClassV1, -}; +use blockifier::execution::contract_class::{ClassInfoExt, ContractClassV0Ext, ContractClassV1Ext}; use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transaction_execution::Transaction; use blockifier::transaction::transaction_types::TransactionType; +use common::contract_class::{ClassInfo, ContractClass, ContractClassV0, ContractClassV1}; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use starknet_api::transaction::{Resource, ResourceBounds}; diff --git a/crates/native_blockifier/src/state_readers/papyrus_state.rs b/crates/native_blockifier/src/state_readers/papyrus_state.rs index 463d242b0c..8439bd5e4f 100644 --- a/crates/native_blockifier/src/state_readers/papyrus_state.rs +++ b/crates/native_blockifier/src/state_readers/papyrus_state.rs @@ -1,7 +1,7 @@ -use blockifier::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use blockifier::state::errors::StateError; use blockifier::state::global_cache::GlobalContractCache; use blockifier::state::state_api::{StateReader, StateResult}; +use common::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use papyrus_storage::compiled_class::CasmStorageReader; use papyrus_storage::db::RO; use papyrus_storage::state::StateStorageReader; diff --git a/crates/native_blockifier/src/state_readers/py_state_reader.rs b/crates/native_blockifier/src/state_readers/py_state_reader.rs index 35d78eea15..4c39f977c6 100644 --- a/crates/native_blockifier/src/state_readers/py_state_reader.rs +++ b/crates/native_blockifier/src/state_readers/py_state_reader.rs @@ -1,6 +1,7 @@ -use blockifier::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; +use blockifier::execution::contract_class::{ContractClassV0Ext, ContractClassV1Ext}; use blockifier::state::errors::StateError; use blockifier::state::state_api::{StateReader, StateResult}; +use common::contract_class::{ContractClass, ContractClassV0, ContractClassV1}; use pyo3::{FromPyObject, PyAny, PyErr, PyObject, PyResult, Python}; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::state::StorageKey; diff --git a/crates/papyrus_execution/Cargo.toml b/crates/papyrus_execution/Cargo.toml index fbfff50d3c..692af7d198 100644 --- a/crates/papyrus_execution/Cargo.toml +++ b/crates/papyrus_execution/Cargo.toml @@ -14,6 +14,7 @@ anyhow.workspace = true blockifier.workspace = true cairo-lang-starknet-classes.workspace = true cairo-vm.workspace = true +common.workspace = true indexmap.workspace = true itertools.workspace = true lazy_static.workspace = true diff --git a/crates/papyrus_execution/src/execution_utils.rs b/crates/papyrus_execution/src/execution_utils.rs index 928728acab..580a33c269 100644 --- a/crates/papyrus_execution/src/execution_utils.rs +++ b/crates/papyrus_execution/src/execution_utils.rs @@ -4,15 +4,15 @@ use std::path::PathBuf; // Expose the tool for creating entry point selectors from function names. pub use blockifier::abi::abi_utils::selector_from_name; -use blockifier::execution::contract_class::{ - ContractClass as BlockifierContractClass, - ContractClassV0, - ContractClassV1, -}; use blockifier::state::cached_state::{CachedState, CommitmentStateDiff, MutRefState}; use blockifier::state::state_api::StateReader; use blockifier::transaction::objects::TransactionExecutionInfo; use cairo_vm::types::errors::program_errors::ProgramError; +use common::contract_class::{ + ContractClass as BlockifierContractClass, + ContractClassV0, + ContractClassV1, +}; use indexmap::IndexMap; use papyrus_common::state::{DeployedContract, ReplacedClass, StorageEntry}; use papyrus_storage::compiled_class::CasmStorageReader; diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 46536125d4..03953983b1 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -29,7 +29,7 @@ use blockifier::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHas use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use blockifier::execution::call_info::CallExecution; -use blockifier::execution::contract_class::{ClassInfo, ContractClass as BlockifierContractClass}; +use blockifier::execution::contract_class::ClassInfoExt; use blockifier::execution::entry_point::{ CallEntryPoint, CallType as BlockifierCallType, @@ -48,6 +48,7 @@ use blockifier::versioned_constants::VersionedConstants; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use common::contract_class::{ClassInfo, ContractClass as BlockifierContractClass}; use execution_utils::{get_trace_constructor, induced_state_diff}; use objects::{PriceUnit, TransactionSimulationOutput}; use once_cell::sync::Lazy; diff --git a/crates/papyrus_execution/src/state_reader.rs b/crates/papyrus_execution/src/state_reader.rs index a15df3c3c8..b6f33f5264 100644 --- a/crates/papyrus_execution/src/state_reader.rs +++ b/crates/papyrus_execution/src/state_reader.rs @@ -4,13 +4,13 @@ mod state_reader_test; use std::cell::Cell; -use blockifier::execution::contract_class::{ +use blockifier::state::errors::StateError; +use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; +use common::contract_class::{ ContractClass as BlockifierContractClass, ContractClassV0, ContractClassV1, }; -use blockifier::state::errors::StateError; -use blockifier::state::state_api::{StateReader as BlockifierStateReader, StateResult}; use papyrus_common::pending_classes::{ApiContractClass, PendingClassesTrait}; use papyrus_common::state::DeclaredClassHashEntry; use papyrus_storage::state::StateStorageReader; diff --git a/crates/papyrus_execution/src/state_reader_test.rs b/crates/papyrus_execution/src/state_reader_test.rs index 332eec48c9..57c7ee34ae 100644 --- a/crates/papyrus_execution/src/state_reader_test.rs +++ b/crates/papyrus_execution/src/state_reader_test.rs @@ -1,14 +1,14 @@ use std::cell::Cell; use assert_matches::assert_matches; -use blockifier::execution::contract_class::{ +use blockifier::state::errors::StateError; +use blockifier::state::state_api::StateReader; +use cairo_lang_utils::bigint::BigUintAsHex; +use common::contract_class::{ ContractClass as BlockifierContractClass, ContractClassV0, ContractClassV1, }; -use blockifier::state::errors::StateError; -use blockifier::state::state_api::StateReader; -use cairo_lang_utils::bigint::BigUintAsHex; use indexmap::indexmap; use papyrus_common::pending_classes::{ApiContractClass, PendingClasses, PendingClassesTrait}; use papyrus_common::state::{