From e634b27fded3acf866e6455fa537f1fdf213f7e1 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 23 Aug 2023 21:58:01 +0200 Subject: [PATCH 1/5] Unify deprecated and casm contract caches. --- bench/internals.rs | 10 +- cli/src/main.rs | 3 +- fuzzer/src/main.rs | 13 +- rpc_state_reader/src/lib.rs | 4 +- src/bin/fibonacci.rs | 20 +- src/bin/invoke.rs | 20 +- src/bin/invoke_with_cachedstate.rs | 14 +- src/execution/execution_entry_point.rs | 1 - src/lib.rs | 63 ++-- src/state/cached_state.rs | 187 ++++-------- src/state/in_memory_state_reader.rs | 45 ++- src/state/mod.rs | 17 +- src/syscalls/deprecated_syscall_handler.rs | 22 +- src/syscalls/deprecated_syscall_response.rs | 4 +- src/testing/erc20.rs | 13 +- src/testing/mod.rs | 14 +- src/testing/state.rs | 20 +- src/transaction/declare.rs | 48 ++- src/transaction/declare_v2.rs | 10 +- src/transaction/deploy.rs | 6 +- src/transaction/deploy_account.rs | 20 +- src/transaction/fee.rs | 4 +- src/transaction/invoke_function.rs | 33 +- src/transaction/l1_handler.rs | 2 +- tests/cairo_1_syscalls.rs | 285 ++++++++++-------- tests/complex_contracts/amm_contracts/amm.rs | 54 +--- .../amm_contracts/amm_proxy.rs | 30 +- tests/complex_contracts/nft/erc721.rs | 84 +----- tests/delegate_call.rs | 13 +- tests/delegate_l1_handler.rs | 13 +- tests/deploy_account.rs | 9 +- tests/fibonacci.rs | 12 +- tests/increase_balance.rs | 8 +- tests/internal_calls.rs | 8 +- tests/internals.rs | 128 ++++---- tests/multi_syscall_test.rs | 9 +- tests/storage.rs | 8 +- tests/syscalls.rs | 104 ++++--- tests/syscalls_errors.rs | 32 +- 39 files changed, 651 insertions(+), 739 deletions(-) diff --git a/bench/internals.rs b/bench/internals.rs index 075f216a2..ea6906013 100644 --- a/bench/internals.rs +++ b/bench/internals.rs @@ -17,7 +17,7 @@ use starknet_in_rust::{ transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction}, utils::Address, }; -use std::{hint::black_box, sync::Arc}; +use std::{collections::HashMap, hint::black_box, sync::Arc}; lazy_static! { // include_str! doesn't seem to work in CI @@ -61,7 +61,7 @@ fn deploy_account() { const RUNS: usize = 500; let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) @@ -97,7 +97,7 @@ fn declare() { const RUNS: usize = 5; let state_reader = Arc::new(InMemoryStateReader::default()); - let state = CachedState::new(state_reader, Some(Default::default()), None); + let state = CachedState::new(state_reader, HashMap::new()); let block_context = &Default::default(); @@ -129,7 +129,7 @@ fn deploy() { const RUNS: usize = 8; let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) @@ -164,7 +164,7 @@ fn invoke() { const RUNS: usize = 100; let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) diff --git a/cli/src/main.rs b/cli/src/main.rs index f37bdf544..bc9e0d6bc 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -313,8 +313,7 @@ pub async fn start_devnet(port: u16) -> Result<(), std::io::Error> { let cached_state = web::Data::new(AppState { cached_state: Mutex::new(CachedState::::new( Arc::new(InMemoryStateReader::default()), - Some(HashMap::new()), - None, + HashMap::new(), )), }); diff --git a/fuzzer/src/main.rs b/fuzzer/src/main.rs index edbfb1cf5..2b14e11b4 100644 --- a/fuzzer/src/main.rs +++ b/fuzzer/src/main.rs @@ -25,6 +25,7 @@ use std::{ path::PathBuf, }; +use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass; use std::fs; use std::process::Command; use std::thread; @@ -44,11 +45,11 @@ fn main() { let file_content1 = " %lang starknet from starkware.cairo.common.cairo_builtins import HashBuiltin - + @storage_var func _counter() -> (res: felt) { } - + @external func write_and_read{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (res:felt) { _counter.write('"; @@ -116,7 +117,10 @@ fn main() { let address = Address(1111.into()); let class_hash = [1; 32]; - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -126,8 +130,7 @@ fn main() { //* Create state with previous data //* --------------------------------------- - let mut state = - CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* ------------------------------------ //* Create execution entry point diff --git a/rpc_state_reader/src/lib.rs b/rpc_state_reader/src/lib.rs index 280f14aa0..254b26b00 100644 --- a/rpc_state_reader/src/lib.rs +++ b/rpc_state_reader/src/lib.rs @@ -741,7 +741,7 @@ mod transaction_tests { felt::felt_str, state::cached_state::CachedState, }; - use std::sync::Arc; + use std::{collections::HashMap, sync::Arc}; fn test_tx( tx_hash: &str, @@ -754,7 +754,7 @@ mod transaction_tests { // Instantiate the RPC StateReader and the CachedState let block = BlockValue::Number(serde_json::to_value(block_number).unwrap()); let rpc_state = Arc::new(RpcState::new(network, block)); - let mut state = CachedState::new(rpc_state.clone(), None, None); + let mut state = CachedState::new(rpc_state.clone(), HashMap::new()); let fee_token_address = Address(felt_str!( "049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", diff --git a/src/bin/fibonacci.rs b/src/bin/fibonacci.rs index b14be188a..df61410cf 100644 --- a/src/bin/fibonacci.rs +++ b/src/bin/fibonacci.rs @@ -5,9 +5,13 @@ use num_traits::Zero; use lazy_static::lazy_static; use starknet_in_rust::{ - services::api::contract_classes::deprecated_contract_class::ContractClass, - state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader, - testing::state::StarknetState, utils::Address, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, + state::cached_state::CachedState, + state::in_memory_state_reader::InMemoryStateReader, + testing::state::StarknetState, + utils::Address, }; #[cfg(feature = "with_mimalloc")] @@ -78,17 +82,17 @@ fn create_initial_state() -> CachedState { state_reader .address_to_nonce_mut() .insert(CONTRACT_ADDRESS.clone(), Felt252::zero()); - state_reader - .class_hash_to_contract_class_mut() - .insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + *CONTRACT_CLASS_HASH, + CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ); state_reader .address_to_storage_mut() .insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero()); Arc::new(state_reader) }, - Some(HashMap::new()), - None, + HashMap::new(), ); cached_state diff --git a/src/bin/invoke.rs b/src/bin/invoke.rs index afec929fa..f29a78ad0 100644 --- a/src/bin/invoke.rs +++ b/src/bin/invoke.rs @@ -4,9 +4,13 @@ use cairo_vm::felt::{felt_str, Felt252}; use num_traits::Zero; use starknet_in_rust::{ - services::api::contract_classes::deprecated_contract_class::ContractClass, - state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader, - testing::state::StarknetState, utils::Address, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, + state::cached_state::CachedState, + state::in_memory_state_reader::InMemoryStateReader, + testing::state::StarknetState, + utils::Address, }; use lazy_static::lazy_static; @@ -92,17 +96,17 @@ fn create_initial_state() -> CachedState { state_reader .address_to_nonce_mut() .insert(CONTRACT_ADDRESS.clone(), Felt252::zero()); - state_reader - .class_hash_to_contract_class_mut() - .insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + *CONTRACT_CLASS_HASH, + CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ); state_reader .address_to_storage_mut() .insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero()); Arc::new(state_reader) }, - Some(HashMap::new()), - None, + HashMap::new(), ); cached_state diff --git a/src/bin/invoke_with_cachedstate.rs b/src/bin/invoke_with_cachedstate.rs index e5c7cac2f..4868a0ab4 100644 --- a/src/bin/invoke_with_cachedstate.rs +++ b/src/bin/invoke_with_cachedstate.rs @@ -8,7 +8,9 @@ use starknet_in_rust::{ block_context::{BlockContext, StarknetChainId, StarknetOsConfig}, constants::TRANSACTION_VERSION, }, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::in_memory_state_reader::InMemoryStateReader, state::{cached_state::CachedState, BlockInfo}, transaction::InvokeFunction, @@ -99,17 +101,17 @@ fn create_initial_state() -> CachedState { state_reader .address_to_nonce_mut() .insert(CONTRACT_ADDRESS.clone(), Felt252::zero()); - state_reader - .class_hash_to_contract_class_mut() - .insert(*CONTRACT_CLASS_HASH, CONTRACT_CLASS.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + *CONTRACT_CLASS_HASH, + CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ); state_reader .address_to_storage_mut() .insert((CONTRACT_ADDRESS.clone(), [0; 32]), Felt252::zero()); Arc::new(state_reader) }, - Some(HashMap::new()), - None, + HashMap::new(), ); cached_state diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 554463471..1b6d33870 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -131,7 +131,6 @@ impl ExecutionEntryPoint { let mut tmp_state = CachedState::new( state.state_reader.clone(), state.contract_classes.clone(), - state.casm_contract_classes.clone(), ); tmp_state.cache = state.cache.clone(); diff --git a/src/lib.rs b/src/lib.rs index 6dc8c24fa..42677df79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ pub fn simulate_transaction( ignore_max_fee: bool, skip_nonce_check: bool, ) -> Result, TransactionError> { - let mut cache_state = CachedState::new(Arc::new(state), None, Some(HashMap::new())); + let mut cache_state = CachedState::new(Arc::new(state), HashMap::new()); let mut result = Vec::with_capacity(transactions.len()); for transaction in transactions { let tx_for_simulation = transaction.create_for_simulation( @@ -89,7 +89,7 @@ where T: StateReader, { // This is used as a copy of the original state, we can update this cached state freely. - let mut cached_state = CachedState::::new(Arc::new(state), None, None); + let mut cached_state = CachedState::::new(Arc::new(state), HashMap::new()); let mut result = Vec::with_capacity(transactions.len()); for transaction in transactions { @@ -177,7 +177,7 @@ where T: StateReader, { // This is used as a copy of the original state, we can update this cached state freely. - let mut cached_state = CachedState::::new(Arc::new(state), None, None); + let mut cached_state = CachedState::::new(Arc::new(state), HashMap::new()); // Check if the contract is deployed. cached_state.get_class_hash_at(l1_handler.contract_address())?; @@ -251,6 +251,7 @@ mod test { utils::{Address, ClassHash}, }; + use crate::services::api::contract_classes::compiled_class::CompiledClass; use lazy_static::lazy_static; lazy_static! { @@ -317,7 +318,7 @@ mod test { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -326,7 +327,7 @@ mod test { .address_to_nonce_mut() .insert(address.clone(), nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let calldata = [1.into(), 1.into(), 10.into()].to_vec(); let retdata = call_contract( @@ -378,10 +379,13 @@ mod test { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes - let contract_classes = HashMap::from([(class_hash, contract_class)]); + let contract_classes = HashMap::from([( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + )]); state.set_contract_classes(contract_classes).unwrap(); let mut block_context = BlockContext::default(); @@ -407,7 +411,8 @@ mod test { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -416,7 +421,7 @@ mod test { .address_to_nonce_mut() .insert(address.clone(), nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let calldata = [1.into(), 1.into(), 10.into()].to_vec(); let invoke = InvokeFunction::new( @@ -483,9 +488,10 @@ mod test { .insert(address.clone(), nonce); // simulate deploy - state_reader - .class_hash_to_contract_class_mut() - .insert(acc_class_hash, contract_class); + state_reader.class_hash_to_compiled_class_mut().insert( + acc_class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let hash = felt_str!( "134328839377938040543570691566621575472567895629741043448357033688476792132" @@ -494,9 +500,10 @@ mod test { state_reader .class_hash_to_compiled_class_hash_mut() .insert(fib_address, class_hash); - state_reader - .casm_contract_classes - .insert(fib_address, casm_contract_class); + state_reader.class_hash_to_compiled_class.insert( + fib_address, + CompiledClass::Casm(Arc::new(casm_contract_class)), + ); let calldata = [ address.0.clone(), @@ -610,9 +617,10 @@ mod test { .insert(address.clone(), nonce); // simulate deploy - state_reader - .class_hash_to_contract_class_mut() - .insert(acc_class_hash, contract_class); + state_reader.class_hash_to_compiled_class_mut().insert( + acc_class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let hash = felt_str!( "134328839377938040543570691566621575472567895629741043448357033688476792132" @@ -621,9 +629,10 @@ mod test { state_reader .class_hash_to_compiled_class_hash_mut() .insert(fib_address, class_hash); - state_reader - .casm_contract_classes - .insert(fib_address, casm_contract_class); + state_reader.class_hash_to_compiled_class.insert( + fib_address, + CompiledClass::Casm(Arc::new(casm_contract_class)), + ); let calldata = [ address.0.clone(), @@ -672,7 +681,7 @@ mod test { #[test] fn test_simulate_deploy() { let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) @@ -711,7 +720,7 @@ mod test { #[test] fn test_simulate_declare() { let state_reader = Arc::new(InMemoryStateReader::default()); - let state = CachedState::new(state_reader, Some(Default::default()), None); + let state = CachedState::new(state_reader, HashMap::new()); let block_context = &Default::default(); @@ -748,7 +757,7 @@ mod test { #[test] fn test_simulate_invoke() { let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) @@ -809,7 +818,7 @@ mod test { #[test] fn test_simulate_deploy_account() { let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); state .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) @@ -930,7 +939,7 @@ mod test { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -959,7 +968,7 @@ mod test { #[test] fn test_deploy_and_invoke_simulation() { let state_reader = Arc::new(InMemoryStateReader::default()); - let state = CachedState::new(state_reader, Some(Default::default()), None); + let state = CachedState::new(state_reader, HashMap::new()); let block_context = &Default::default(); diff --git a/src/state/cached_state.rs b/src/state/cached_state.rs index 8b2298de8..350877bd6 100644 --- a/src/state/cached_state.rs +++ b/src/state/cached_state.rs @@ -19,11 +19,7 @@ use std::{ sync::Arc, }; -// K: class_hash V: ContractClass -pub type ContractClassCache = HashMap; -pub type CasmClassCache = HashMap; - -pub const UNINITIALIZED_CLASS_HASH: &ClassHash = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +pub const UNINITIALIZED_CLASS_HASH: &ClassHash = &[0u8; 32]; /// Represents a cached state of contract classes with optional caches. #[derive(Default, Clone, Debug, Eq, Getters, MutGetters, PartialEq)] @@ -32,60 +28,51 @@ pub struct CachedState { #[getset(get = "pub", get_mut = "pub")] pub(crate) cache: StateCache, #[get = "pub"] - pub(crate) contract_classes: Option, - #[get = "pub"] - pub(crate) casm_contract_classes: Option, + pub(crate) contract_classes: HashMap, } impl CachedState { /// Constructor, creates a new cached state. - pub fn new( - state_reader: Arc, - contract_class_cache: Option, - casm_class_cache: Option, - ) -> Self { + pub fn new(state_reader: Arc, contract_classes: HashMap) -> Self { Self { cache: StateCache::default(), - contract_classes: contract_class_cache, state_reader, - casm_contract_classes: casm_class_cache, + contract_classes, } } /// Creates a CachedState for testing purposes. pub fn new_for_testing( state_reader: Arc, - contract_classes: Option, cache: StateCache, - casm_contract_classes: Option, + contract_classes: HashMap, ) -> Self { Self { cache, contract_classes, state_reader, - casm_contract_classes, } } /// Sets the contract classes cache. pub fn set_contract_classes( &mut self, - contract_classes: ContractClassCache, + contract_classes: HashMap, ) -> Result<(), StateError> { - if self.contract_classes.is_some() { + if !self.contract_classes.is_empty() { return Err(StateError::AssignedContractClassCache); } - self.contract_classes = Some(contract_classes); + self.contract_classes = contract_classes; Ok(()) } - /// Returns the casm classes. - #[allow(dead_code)] - pub(crate) fn get_casm_classes(&mut self) -> Result<&CasmClassCache, StateError> { - self.casm_contract_classes - .as_ref() - .ok_or(StateError::MissingCasmClassCache) - } + // /// Returns the casm classes. + // #[allow(dead_code)] + // pub(crate) fn get_casm_classes(&mut self) -> Result<&CasmClassCache, StateError> { + // self.casm_contract_classes + // .as_ref() + // .ok_or(StateError::MissingCasmClassCache) + // } } impl StateReader for CachedState { @@ -172,37 +159,24 @@ impl StateReader for CachedState { if class_hash == UNINITIALIZED_CLASS_HASH { return Err(StateError::UninitiaizedClassHash); } + // I: FETCHING FROM CACHE - // I: DEPRECATED CONTRACT CLASS - // deprecated contract classes dont have compiled class hashes, so we only have one case - if let Some(compiled_class) = self - .contract_classes - .as_ref() - .and_then(|x| x.get(class_hash)) - { - return Ok(CompiledClass::Deprecated(Arc::new(compiled_class.clone()))); - } - // I: CASM CONTRACT CLASS : COMPILED_CLASS_HASH - if let Some(compiled_class) = self - .casm_contract_classes - .as_ref() - .and_then(|x| x.get(class_hash)) - { - return Ok(CompiledClass::Casm(Arc::new(compiled_class.clone()))); + if let Some(compiled_class) = self.contract_classes.get(class_hash) { + return Ok(compiled_class.clone()); } + // I: CASM CONTRACT CLASS : CLASS_HASH if let Some(compiled_class_hash) = self.cache.class_hash_to_compiled_class_hash.get(class_hash) { - if let Some(casm_class) = &mut self - .casm_contract_classes - .as_ref() - .and_then(|m| m.get(compiled_class_hash)) - { - return Ok(CompiledClass::Casm(Arc::new(casm_class.clone()))); + if let Some(casm_class) = self.contract_classes.get(compiled_class_hash) { + return Ok(casm_class.clone()); } } + // II: FETCHING FROM STATE_READER + // TODO: Should this modify the cache? + // TODO: Related: Isn't this exact method available in `impl State for CachedState`? self.state_reader.get_contract_class(class_hash) } } @@ -214,15 +188,13 @@ impl State for CachedState { class_hash: &ClassHash, contract_class: &ContractClass, ) -> Result<(), StateError> { - match self.contract_classes.as_mut() { - Some(x) => { - x.insert(*class_hash, contract_class.clone()); - } - None => { - self.contract_classes = Some(HashMap::new()); - self.set_contract_class(class_hash, contract_class)?; - } - } + // TODO: Could this method's signature use `CompiledClass` instead of `ContractClass`? Or + // even better, could the entire trait use `Arc`? + self.contract_classes.insert( + *class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); + Ok(()) } @@ -291,11 +263,11 @@ impl State for CachedState { casm_class: CasmContractClass, ) -> Result<(), StateError> { let compiled_class_hash = compiled_class_hash.to_be_bytes(); + self.contract_classes.insert( + compiled_class_hash, + CompiledClass::Casm(Arc::new(casm_class)), + ); - self.casm_contract_classes - .as_mut() - .ok_or(StateError::MissingCasmClassCache)? - .insert(compiled_class_hash, casm_class); Ok(()) } @@ -437,45 +409,32 @@ impl State for CachedState { if class_hash == UNINITIALIZED_CLASS_HASH { return Err(StateError::UninitiaizedClassHash); } + // I: FETCHING FROM CACHE - // I: DEPRECATED CONTRACT CLASS // deprecated contract classes dont have compiled class hashes, so we only have one case - if let Some(compiled_class) = self - .contract_classes - .as_ref() - .and_then(|x| x.get(class_hash)) - { - return Ok(CompiledClass::Deprecated(Arc::new(compiled_class.clone()))); - } - // I: CASM CONTRACT CLASS : COMPILED_CLASS_HASH - if let Some(compiled_class) = self - .casm_contract_classes - .as_ref() - .and_then(|x| x.get(class_hash)) - { - return Ok(CompiledClass::Casm(Arc::new(compiled_class.clone()))); + if let Some(compiled_class) = self.contract_classes.get(class_hash) { + return Ok(compiled_class.clone()); } + // I: CASM CONTRACT CLASS : CLASS_HASH if let Some(compiled_class_hash) = self.cache.class_hash_to_compiled_class_hash.get(class_hash) { - if let Some(casm_class) = &mut self - .casm_contract_classes - .as_ref() - .and_then(|m| m.get(compiled_class_hash)) - { - return Ok(CompiledClass::Casm(Arc::new(casm_class.clone()))); + if let Some(casm_class) = self.contract_classes.get(compiled_class_hash) { + return Ok(casm_class.clone()); } } + // II: FETCHING FROM STATE_READER let contract = self.state_reader.get_contract_class(class_hash)?; match contract { - CompiledClass::Casm(ref class) => { + CompiledClass::Casm(ref casm_class) => { // We call this method instead of state_reader's in order to update the cache's class_hash_initial_values map let compiled_class_hash = self.get_compiled_class_hash(class_hash)?; - self.casm_contract_classes - .as_mut() - .and_then(|m| m.insert(compiled_class_hash, class.as_ref().clone())); + self.set_compiled_class( + &Felt252::from_bytes_be(&compiled_class_hash), + casm_class.as_ref().clone(), + )?; } CompiledClass::Deprecated(ref contract) => { self.set_contract_class(class_hash, &contract.clone())? @@ -503,7 +462,6 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_address = Address(4242.into()); @@ -522,7 +480,7 @@ mod tests { .address_to_storage_mut() .insert(storage_entry, storage_value); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); assert_eq!( cached_state.get_class_hash_at(&contract_address).unwrap(), @@ -545,20 +503,18 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_class = ContractClass::from_path("starknet_programs/raw_contract_classes/class_with_abi.json") .unwrap(); state_reader - .class_hash_to_contract_class - .insert([1; 32], contract_class); + .class_hash_to_compiled_class + .insert([1; 32], CompiledClass::Deprecated(Arc::new(contract_class))); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); cached_state.set_contract_classes(HashMap::new()).unwrap(); - assert!(cached_state.contract_classes.is_some()); assert_eq!( cached_state.get_contract_class(&[1; 32]).unwrap(), @@ -573,7 +529,7 @@ mod tests { #[test] fn cached_state_storage_test() { let mut cached_state = - CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let storage_entry: StorageEntry = (Address(31.into()), [0; 32]); let value = Felt252::new(10); @@ -595,7 +551,7 @@ mod tests { let contract_address = Address(32123.into()); - let mut cached_state = CachedState::new(state_reader, None, None); + let mut cached_state = CachedState::new(state_reader, HashMap::new()); assert!(cached_state .deploy_contract(contract_address, [10; 32]) @@ -611,7 +567,7 @@ mod tests { let storage_key = [18; 32]; let value = Felt252::new(912); - let mut cached_state = CachedState::new(state_reader, None, None); + let mut cached_state = CachedState::new(state_reader, HashMap::new()); // set storage_key cached_state.set_storage_at(&(contract_address.clone(), storage_key), value.clone()); @@ -629,27 +585,6 @@ mod tests { assert_eq!(new_result.unwrap(), new_value); } - /// This test ensures that an error is thrown when trying to set contract classes twice. - #[test] - fn set_contract_classes_twice_error_test() { - let state_reader = InMemoryStateReader::new( - HashMap::new(), - HashMap::new(), - HashMap::new(), - HashMap::new(), - HashMap::new(), - HashMap::new(), - ); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); - - cached_state.set_contract_classes(HashMap::new()).unwrap(); - let result = cached_state - .set_contract_classes(HashMap::new()) - .unwrap_err(); - - assert_matches!(result, StateError::AssignedContractClassCache); - } - /// This test ensures that an error is thrown if a contract address is out of range. #[test] fn deploy_contract_address_out_of_range_error_test() { @@ -659,12 +594,11 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_address = Address(0.into()); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); let result = cached_state .deploy_contract(contract_address.clone(), [10; 32]) @@ -685,12 +619,11 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_address = Address(42.into()); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); cached_state .deploy_contract(contract_address.clone(), [10; 32]) @@ -714,12 +647,11 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_address = Address(32123.into()); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); cached_state .deploy_contract(contract_address.clone(), [10; 32]) @@ -744,12 +676,11 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let address_one = Address(Felt252::one()); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); let state_diff = StateDiff { address_to_class_hash: HashMap::from([( @@ -784,7 +715,7 @@ mod tests { #[test] fn count_actual_storage_changes_test() { let state_reader = InMemoryStateReader::default(); - let mut cached_state = CachedState::new(Arc::new(state_reader), None, None); + let mut cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); let address_one = Address(1.into()); let address_two = Address(2.into()); diff --git a/src/state/in_memory_state_reader.rs b/src/state/in_memory_state_reader.rs index de9b3e827..7a7f1c372 100644 --- a/src/state/in_memory_state_reader.rs +++ b/src/state/in_memory_state_reader.rs @@ -1,19 +1,15 @@ use crate::{ core::errors::state_errors::StateError, - services::api::contract_classes::{ - compiled_class::CompiledClass, deprecated_contract_class::ContractClass, - }, + services::api::contract_classes::compiled_class::CompiledClass, state::{ - cached_state::{CasmClassCache, UNINITIALIZED_CLASS_HASH}, - state_api::StateReader, - state_cache::StorageEntry, + cached_state::UNINITIALIZED_CLASS_HASH, state_api::StateReader, state_cache::StorageEntry, }, utils::{Address, ClassHash, CompiledClassHash}, }; use cairo_vm::felt::Felt252; use getset::{Getters, MutGetters}; use num_traits::Zero; -use std::{collections::HashMap, sync::Arc}; +use std::collections::HashMap; /// A [StateReader] that holds all the data in memory. /// @@ -28,9 +24,7 @@ pub struct InMemoryStateReader { #[getset(get_mut = "pub")] pub address_to_storage: HashMap, #[getset(get_mut = "pub")] - pub class_hash_to_contract_class: HashMap, - #[getset(get_mut = "pub")] - pub(crate) casm_contract_classes: CasmClassCache, + pub class_hash_to_compiled_class: HashMap, #[getset(get_mut = "pub")] pub(crate) class_hash_to_compiled_class_hash: HashMap, } @@ -49,16 +43,14 @@ impl InMemoryStateReader { address_to_class_hash: HashMap, address_to_nonce: HashMap, address_to_storage: HashMap, - class_hash_to_contract_class: HashMap, - casm_contract_classes: CasmClassCache, + class_hash_to_compiled_class: HashMap, class_hash_to_compiled_class_hash: HashMap, ) -> Self { Self { address_to_class_hash, address_to_nonce, address_to_storage, - class_hash_to_contract_class, - casm_contract_classes, + class_hash_to_compiled_class, class_hash_to_compiled_class_hash, } } @@ -79,13 +71,10 @@ impl InMemoryStateReader { &self, compiled_class_hash: &CompiledClassHash, ) -> Result { - if let Some(compiled_class) = self.casm_contract_classes.get(compiled_class_hash) { - return Ok(CompiledClass::Casm(Arc::new(compiled_class.clone()))); + match self.class_hash_to_compiled_class.get(compiled_class_hash) { + Some(compiled_class) => Ok(compiled_class.clone()), + None => Err(StateError::NoneCompiledClass(*compiled_class_hash)), } - if let Some(compiled_class) = self.class_hash_to_contract_class.get(compiled_class_hash) { - return Ok(CompiledClass::Deprecated(Arc::new(compiled_class.clone()))); - } - Err(StateError::NoneCompiledClass(*compiled_class_hash)) } } @@ -127,9 +116,10 @@ impl StateReader for InMemoryStateReader { fn get_contract_class(&self, class_hash: &ClassHash) -> Result { // Deprecated contract classes dont have a compiled_class_hash, we dont need to fetch it - if let Some(compiled_class) = self.class_hash_to_contract_class.get(class_hash) { - return Ok(CompiledClass::Deprecated(Arc::new(compiled_class.clone()))); + if let Some(compiled_class) = self.class_hash_to_compiled_class.get(class_hash) { + return Ok(compiled_class.clone()); } + let compiled_class_hash = self.get_compiled_class_hash(class_hash)?; if compiled_class_hash != *UNINITIALIZED_CLASS_HASH { let compiled_class = self.get_compiled_class(&compiled_class_hash)?; @@ -143,6 +133,8 @@ impl StateReader for InMemoryStateReader { #[cfg(test)] mod tests { use super::*; + use crate::services::api::contract_classes::deprecated_contract_class::ContractClass; + use std::sync::Arc; #[test] fn get_contract_state_test() { @@ -152,7 +144,6 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_address = Address(37810.into()); @@ -190,7 +181,6 @@ mod tests { HashMap::new(), HashMap::new(), HashMap::new(), - HashMap::new(), ); let contract_class_key = [0; 32]; @@ -198,9 +188,10 @@ mod tests { ContractClass::from_path("starknet_programs/raw_contract_classes/class_with_abi.json") .unwrap(); - state_reader - .class_hash_to_contract_class - .insert([0; 32], contract_class.clone()); + state_reader.class_hash_to_compiled_class.insert( + [0; 32], + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); assert_eq!( state_reader .get_contract_class(&contract_class_key) diff --git a/src/state/mod.rs b/src/state/mod.rs index 8b6ace0cc..3920165a8 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -166,7 +166,7 @@ impl StateDiff { where T: StateReader + Clone, { - let mut cache_state = CachedState::new(state_reader, None, None); + let mut cache_state = CachedState::new(state_reader, HashMap::new()); let cache_storage_mapping = to_cache_state_storage_mapping(&self.storage_updates); cache_state.cache_mut().set_initial_values( @@ -240,7 +240,7 @@ mod test { use crate::{ state::in_memory_state_reader::InMemoryStateReader, state::{ - cached_state::{CachedState, ContractClassCache}, + cached_state::CachedState, state_api::StateReader, state_cache::{StateCache, StorageEntry}, }, @@ -263,7 +263,7 @@ mod test { .address_to_nonce .insert(contract_address, nonce); - let cached_state = CachedState::new(Arc::new(state_reader), None, None); + let cached_state = CachedState::new(Arc::new(state_reader), HashMap::new()); let diff = StateDiff::from_cached_state(cached_state).unwrap(); @@ -323,7 +323,8 @@ mod test { .address_to_nonce .insert(contract_address.clone(), nonce); - let cached_state_original = CachedState::new(Arc::new(state_reader.clone()), None, None); + let cached_state_original = + CachedState::new(Arc::new(state_reader.clone()), HashMap::new()); let diff = StateDiff::from_cached_state(cached_state_original.clone()).unwrap(); @@ -370,12 +371,8 @@ mod test { storage_writes, HashMap::new(), ); - let cached_state = CachedState::new_for_testing( - Arc::new(state_reader), - Some(ContractClassCache::new()), - cache, - None, - ); + let cached_state = + CachedState::new_for_testing(Arc::new(state_reader), cache, HashMap::new()); let mut diff = StateDiff::from_cached_state(cached_state).unwrap(); diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index d131d5ff1..fb9b38c1a 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -730,7 +730,7 @@ mod tests { ] ); - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -766,7 +766,7 @@ mod tests { let hint_data = HintProcessorData::new_default(GET_CONTRACT_ADDRESS.to_string(), ids_data); // invoke syscall - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -808,7 +808,7 @@ mod tests { let hint_data = HintProcessorData::new_default(GET_TX_SIGNATURE.to_string(), ids_data); // invoke syscall - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -847,7 +847,7 @@ mod tests { ); } - /// Tests the correct behavior of a storage read operation within a blockchain. + /// Tests the correct behavior of a storage read operation within a blockchain. #[test] fn test_bl_storage_read_hint_ok() { let mut vm = vm!(); @@ -876,7 +876,7 @@ mod tests { let hint_data = HintProcessorData::new_default(STORAGE_READ.to_string(), ids_data); - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -911,7 +911,7 @@ mod tests { assert_matches!(get_big_int(&vm, relocatable!(2, 2)), Ok(response) if response == storage_value ); } - /// Tests the correct behavior of a storage write operation within a blockchain. + /// Tests the correct behavior of a storage write operation within a blockchain. #[test] fn test_bl_storage_write_hint_ok() { let mut vm = vm!(); @@ -941,7 +941,7 @@ mod tests { let hint_data = HintProcessorData::new_default(STORAGE_WRITE.to_string(), ids_data); - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -980,7 +980,7 @@ mod tests { assert_eq!(write, Felt252::new(45)); } - /// Tests the correct behavior of a deploy operation within a blockchain. + /// Tests the correct behavior of a deploy operation within a blockchain. #[test] fn test_bl_deploy_ok() { let mut vm = vm!(); @@ -1015,7 +1015,7 @@ mod tests { let hint_data = HintProcessorData::new_default(DEPLOY.to_string(), ids_data); // Create SyscallHintProcessor - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), @@ -1071,7 +1071,7 @@ mod tests { ); } - /// Tests the correct behavior of a storage deploy and invoke operations within a blockchain. + /// Tests the correct behavior of a storage deploy and invoke operations within a blockchain. #[test] fn test_deploy_and_invoke() { /* @@ -1113,7 +1113,7 @@ mod tests { ); // Create SyscallHintProcessor - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut syscall_handler_hint_processor = SyscallHintProcessor::new( DeprecatedBLSyscallHandler::default_with(&mut state), RunResources::default(), diff --git a/src/syscalls/deprecated_syscall_response.rs b/src/syscalls/deprecated_syscall_response.rs index 85e3e9cc1..bc7d3523f 100644 --- a/src/syscalls/deprecated_syscall_response.rs +++ b/src/syscalls/deprecated_syscall_response.rs @@ -309,7 +309,7 @@ impl DeprecatedWriteSyscallResponse for DeprecatedStorageReadResponse { #[cfg(test)] mod tests { - use std::sync::Arc; + use std::{collections::HashMap, sync::Arc}; use super::*; use crate::{ @@ -330,7 +330,7 @@ mod tests { #[test] fn write_get_caller_address_response() { // Initialize a VM and syscall handler - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let syscall = DeprecatedBLSyscallHandler::default_with(&mut state); let mut vm = vm!(); diff --git a/src/testing/erc20.rs b/src/testing/erc20.rs index eab540352..717097e53 100644 --- a/src/testing/erc20.rs +++ b/src/testing/erc20.rs @@ -10,7 +10,9 @@ use crate::{ execution::{ execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext, }, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::{ cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, @@ -58,8 +60,11 @@ fn test_erc20_cairo2() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); - contract_class_cache.insert(erc20_class_hash, test_contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + contract_class_cache.insert( + erc20_class_hash, + CompiledClass::Casm(Arc::new(test_contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -70,7 +75,7 @@ fn test_erc20_cairo2() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let name_ = Felt252::from_bytes_be(b"some-token"); let symbol_ = Felt252::from_bytes_be(b"my-super-awesome-token"); diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 7772aae10..018082643 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -14,7 +14,9 @@ use crate::{ block_context::{BlockContext, StarknetChainId, StarknetOsConfig}, constants::DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS, }, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::{ cached_state::CachedState, in_memory_state_reader::InMemoryStateReader, state_cache::StorageEntry, BlockInfo, @@ -149,14 +151,14 @@ pub fn create_account_tx_test_state( state_reader.address_to_storage_mut().extend(stored); } for (class_hash, contract_class) in class_hash_to_class { - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); } Arc::new(state_reader) }, - Some(HashMap::new()), - Some(HashMap::new()), + HashMap::new(), ); Ok((block_context, cached_state)) diff --git a/src/testing/state.rs b/src/testing/state.rs index 28c1ef84a..a16bf66fe 100644 --- a/src/testing/state.rs +++ b/src/testing/state.rs @@ -40,7 +40,7 @@ impl StarknetState { let block_context = context.unwrap_or_default(); let state_reader = Arc::new(InMemoryStateReader::default()); - let state = CachedState::new(state_reader, Some(HashMap::new()), Some(HashMap::new())); + let state = CachedState::new(state_reader, HashMap::new()); let l2_to_l1_messages = HashMap::new(); let l2_to_l1_messages_log = Vec::new(); @@ -330,6 +330,7 @@ mod tests { }, execution::{CallType, OrderedL2ToL1Message}, hash_utils::calculate_contract_address, + services::api::contract_classes::compiled_class::CompiledClass, state::state_cache::StorageEntry, utils::{calculate_sn_keccak, felt_to_hash}, }; @@ -399,11 +400,10 @@ mod tests { starknet_state .state .contract_classes - .unwrap() .get(&class_hash) .unwrap() .to_owned(), - contract_class + CompiledClass::Deprecated(Arc::new(contract_class)) ); } @@ -419,7 +419,10 @@ mod tests { // hack store account contract let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class.clone()); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); // store sender_address let sender_address = Address(1.into()); @@ -440,11 +443,12 @@ mod tests { state_reader .address_to_storage_mut() .insert(storage_entry.clone(), storage.clone()); - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); - let state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* -------------------------------------------- //* Create starknet state with previous data diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index abb20c0b7..b7bce7833 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -332,7 +332,9 @@ mod tests { transaction_type::TransactionType, }, execution::CallType, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader, utils::{felt_to_hash, Address}, @@ -353,7 +355,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = hash.to_be_bytes(); - contract_class_cache.insert(class_hash, contract_class.clone()); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); // store sender_address let sender_address = Address(1.into()); @@ -369,7 +374,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::new(1)); - let mut state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data @@ -510,7 +515,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); // store sender_address let sender_address = Address(1.into()); @@ -526,7 +534,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::new(1)); - let _state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let _state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data @@ -573,7 +581,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); // store sender_address let sender_address = Address(1.into()); @@ -589,7 +600,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::new(1)); - let _state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let _state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data @@ -635,7 +646,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); // store sender_address let sender_address = Address(1.into()); @@ -651,7 +665,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::zero()); - let mut state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data @@ -711,7 +725,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); // store sender_address let sender_address = Address(1.into()); @@ -727,7 +744,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::zero()); - let mut state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data @@ -774,7 +791,7 @@ mod tests { let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(contract_class_cache), None); + let mut state = CachedState::new(state_reader, contract_class_cache); // There are no account contracts in the state, so the transaction should fail let fib_contract_class = @@ -815,7 +832,10 @@ mod tests { let hash = compute_deprecated_class_hash(&contract_class).unwrap(); let class_hash = felt_to_hash(&hash); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); // store sender_address let sender_address = Address(1.into()); @@ -831,7 +851,7 @@ mod tests { .address_to_nonce_mut() .insert(sender_address, Felt252::zero()); - let mut state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* --------------------------------------- //* Test declare with previous data diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 0b80174c5..406e4b50c 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -510,7 +510,7 @@ mod tests { // crate state to store casm contract class let casm_contract_class_cache = HashMap::new(); let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + let mut state = CachedState::new(state_reader, casm_contract_class_cache); // call compile and store assert!(internal_declare @@ -579,7 +579,7 @@ mod tests { // crate state to store casm contract class let casm_contract_class_cache = HashMap::new(); let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + let mut state = CachedState::new(state_reader, casm_contract_class_cache); // call compile and store assert!(internal_declare @@ -650,7 +650,7 @@ mod tests { // crate state to store casm contract class let casm_contract_class_cache = HashMap::new(); let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + let mut state = CachedState::new(state_reader, casm_contract_class_cache); // call compile and store assert!(internal_declare @@ -719,7 +719,7 @@ mod tests { // crate state to store casm contract class let casm_contract_class_cache = HashMap::new(); let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + let mut state = CachedState::new(state_reader, casm_contract_class_cache); // call compile and store assert!(internal_declare @@ -789,7 +789,7 @@ mod tests { // crate state to store casm contract class let casm_contract_class_cache = HashMap::new(); let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + let mut state = CachedState::new(state_reader, casm_contract_class_cache); let expected_err = format!( "Invalid compiled class, expected class hash: {}, but received: {}", diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index 23606ca65..c6132445c 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -328,7 +328,7 @@ mod tests { fn invoke_constructor_test() { // Instantiate CachedState let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); // Set contract_class let contract_class = @@ -376,7 +376,7 @@ mod tests { fn invoke_constructor_no_calldata_should_fail() { // Instantiate CachedState let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); let contract_class = ContractClass::from_path("starknet_programs/constructor.json").unwrap(); @@ -402,7 +402,7 @@ mod tests { fn deploy_contract_without_constructor_should_fail() { // Instantiate CachedState let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); + let mut state = CachedState::new(state_reader, HashMap::new()); let contract_path = "starknet_programs/amm.json"; let contract_class = ContractClass::from_path(contract_path).unwrap(); diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 2833004e1..1a048d450 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -385,7 +385,7 @@ impl DeployAccount { #[cfg(test)] mod tests { - use std::{path::PathBuf, sync::Arc}; + use std::{collections::HashMap, path::PathBuf, sync::Arc}; use super::*; use crate::{ @@ -406,11 +406,7 @@ mod tests { let class_hash = felt_to_hash(&hash); let block_context = BlockContext::default(); - let mut _state = CachedState::new( - Arc::new(InMemoryStateReader::default()), - Some(Default::default()), - None, - ); + let mut _state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let internal_deploy = DeployAccount::new( class_hash, @@ -442,11 +438,7 @@ mod tests { let class_hash = felt_to_hash(&hash); let block_context = BlockContext::default(); - let mut state = CachedState::new( - Arc::new(InMemoryStateReader::default()), - Some(Default::default()), - None, - ); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let internal_deploy = DeployAccount::new( class_hash, @@ -494,11 +486,7 @@ mod tests { let class_hash = felt_to_hash(&hash); let block_context = BlockContext::default(); - let mut state = CachedState::new( - Arc::new(InMemoryStateReader::default()), - Some(Default::default()), - None, - ); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let internal_deploy = DeployAccount::new( class_hash, diff --git a/src/transaction/fee.rs b/src/transaction/fee.rs index 12d9ec686..818c62a54 100644 --- a/src/transaction/fee.rs +++ b/src/transaction/fee.rs @@ -196,7 +196,7 @@ mod tests { #[test] fn test_charge_fee_v0_actual_fee_exceeds_max_fee_should_return_error() { - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut tx_execution_context = TransactionExecutionContext::default(); let mut block_context = BlockContext::default(); block_context.starknet_os_config.gas_price = 1; @@ -222,7 +222,7 @@ mod tests { #[test] fn test_charge_fee_v1_actual_fee_exceeds_max_fee_should_return_error() { - let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); + let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), HashMap::new()); let mut tx_execution_context = TransactionExecutionContext { version: 1.into(), ..Default::default() diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index f4ec8a579..33a5d1b3f 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -401,8 +401,11 @@ pub(crate) fn preprocess_invoke_function_fields( mod tests { use super::*; use crate::{ - services::api::contract_classes::deprecated_contract_class::ContractClass, - state::cached_state::CachedState, state::in_memory_state_reader::InMemoryStateReader, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, + state::cached_state::CachedState, + state::in_memory_state_reader::InMemoryStateReader, utils::calculate_sn_keccak, }; use cairo_lang_starknet::casm_contract_class::CasmContractClass; @@ -449,7 +452,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -518,7 +521,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -583,7 +586,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -642,7 +645,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -707,7 +710,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -766,7 +769,7 @@ mod tests { skip_nonce_check: false, }; - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -823,7 +826,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -881,7 +884,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -944,7 +947,7 @@ mod tests { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); @@ -1087,13 +1090,9 @@ mod tests { let mut casm_contract_class_cache = HashMap::new(); - casm_contract_class_cache.insert(class_hash, contract_class); + casm_contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); - let mut state = CachedState::new( - Arc::new(state_reader), - None, - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), casm_contract_class_cache); let state_before_execution = state.clone(); diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 9b479a340..7cb1766d9 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -266,7 +266,7 @@ mod test { .address_to_nonce .insert(contract_address, nonce); - let mut state = CachedState::new(Arc::new(state_reader), None, None); + let mut state = CachedState::new(Arc::new(state_reader), HashMap::new()); // Initialize state.contract_classes state.set_contract_classes(HashMap::new()).unwrap(); diff --git a/tests/cairo_1_syscalls.rs b/tests/cairo_1_syscalls.rs index 1f32b20e6..9e7cc02a3 100644 --- a/tests/cairo_1_syscalls.rs +++ b/tests/cairo_1_syscalls.rs @@ -65,7 +65,7 @@ fn storage_write_read() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -75,7 +75,7 @@ fn storage_write_read() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -204,7 +204,7 @@ fn library_call() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -226,7 +226,10 @@ fn library_call() { let lib_class_hash: ClassHash = [2; 32]; let lib_nonce = Felt252::zero(); - contract_class_cache.insert(lib_class_hash, lib_contract_class); + contract_class_cache.insert( + lib_class_hash, + CompiledClass::Casm(Arc::new(lib_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(lib_address.clone(), lib_class_hash); @@ -235,7 +238,7 @@ fn library_call() { .insert(lib_address, lib_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), Felt252::from_bytes_be(&lib_class_hash)].to_vec(); @@ -361,7 +364,7 @@ fn call_contract_storage_write_read() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -392,7 +395,10 @@ fn call_contract_storage_write_read() { let simple_wallet_class_hash: ClassHash = [2; 32]; let simple_wallet_nonce = Felt252::zero(); - contract_class_cache.insert(simple_wallet_class_hash, simple_wallet_contract_class); + contract_class_cache.insert( + simple_wallet_class_hash, + CompiledClass::Casm(Arc::new(simple_wallet_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(simple_wallet_address.clone(), simple_wallet_class_hash); @@ -401,7 +407,7 @@ fn call_contract_storage_write_read() { .insert(simple_wallet_address.clone(), simple_wallet_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -547,7 +553,7 @@ fn emit_event() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -557,7 +563,7 @@ fn emit_event() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [].to_vec(); @@ -659,8 +665,11 @@ fn deploy_cairo1_from_cairo1() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); - contract_class_cache.insert(test_class_hash, test_contract_class.clone()); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Casm(Arc::new(test_contract_class.clone())), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -671,7 +680,7 @@ fn deploy_cairo1_from_cairo1() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt].to_vec(); @@ -750,15 +759,17 @@ fn deploy_cairo0_from_cairo1_without_constructor() { let entrypoint_selector = &entrypoints.external.get(0).unwrap().selector; // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - casm_contract_class_cache.insert(class_hash, contract_class); - contract_class_cache.insert(test_class_hash, test_contract_class.clone()); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Deprecated(Arc::new(test_contract_class.clone())), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -769,11 +780,7 @@ fn deploy_cairo0_from_cairo1_without_constructor() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt].to_vec(); @@ -850,7 +857,6 @@ fn deploy_cairo0_from_cairo1_with_constructor() { let entrypoint_selector = &entrypoints.external.get(0).unwrap().selector; // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); @@ -858,8 +864,11 @@ fn deploy_cairo0_from_cairo1_with_constructor() { let nonce = Felt252::zero(); // simulate contract declare - casm_contract_class_cache.insert(class_hash, contract_class); - contract_class_cache.insert(test_class_hash, test_contract_class.clone()); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Deprecated(Arc::new(test_contract_class.clone())), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -870,11 +879,7 @@ fn deploy_cairo0_from_cairo1_with_constructor() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt, address.0.clone(), Felt252::zero()].to_vec(); @@ -953,15 +958,17 @@ fn deploy_cairo0_and_invoke() { let entrypoint_selector = &entrypoints.external.get(0).unwrap().selector; // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - casm_contract_class_cache.insert(class_hash, contract_class); - contract_class_cache.insert(test_class_hash, test_contract_class.clone()); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Deprecated(Arc::new(test_contract_class.clone())), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -972,11 +979,7 @@ fn deploy_cairo0_and_invoke() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state: CachedState<_> = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state: CachedState<_> = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt].to_vec(); @@ -1085,7 +1088,7 @@ fn test_send_message_to_l1_syscall() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -1096,7 +1099,7 @@ fn test_send_message_to_l1_syscall() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // RUN SEND_MSG // Create an execution entry point @@ -1179,7 +1182,7 @@ fn test_get_execution_info() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1189,7 +1192,7 @@ fn test_get_execution_info() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -1274,7 +1277,10 @@ fn replace_class_internal() { let class_hash_a: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash_a, contract_class_a); + contract_class_cache.insert( + class_hash_a, + CompiledClass::Casm(Arc::new(contract_class_a)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1292,10 +1298,13 @@ fn replace_class_internal() { let class_hash_b: ClassHash = [2; 32]; - contract_class_cache.insert(class_hash_b, contract_class_b.clone()); + contract_class_cache.insert( + class_hash_b, + CompiledClass::Casm(Arc::new(contract_class_b.clone())), + ); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Run upgrade entrypoint and check that the storage was updated with the new contract class // Create an execution entry point @@ -1370,7 +1379,10 @@ fn replace_class_contract_call() { let class_hash_a: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash_a, contract_class_a); + contract_class_cache.insert( + class_hash_a, + CompiledClass::Casm(Arc::new(contract_class_a)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1391,7 +1403,10 @@ fn replace_class_contract_call() { let class_hash_b: ClassHash = [2; 32]; - contract_class_cache.insert(class_hash_b, contract_class_b); + contract_class_cache.insert( + class_hash_b, + CompiledClass::Casm(Arc::new(contract_class_b)), + ); // SET GET_NUMBER_WRAPPER @@ -1408,7 +1423,10 @@ fn replace_class_contract_call() { let wrapper_address = Address(Felt252::from(2)); let wrapper_class_hash: ClassHash = [3; 32]; - contract_class_cache.insert(wrapper_class_hash, wrapper_contract_class); + contract_class_cache.insert( + wrapper_class_hash, + CompiledClass::Casm(Arc::new(wrapper_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(wrapper_address.clone(), wrapper_class_hash); @@ -1417,7 +1435,7 @@ fn replace_class_contract_call() { .insert(wrapper_address, nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // INITIALIZE STARKNET CONFIG let block_context = BlockContext::default(); @@ -1537,7 +1555,10 @@ fn replace_class_contract_call_same_transaction() { let class_hash_a: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash_a, contract_class_a); + contract_class_cache.insert( + class_hash_a, + CompiledClass::Casm(Arc::new(contract_class_a)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1558,7 +1579,10 @@ fn replace_class_contract_call_same_transaction() { let class_hash_b: ClassHash = [2; 32]; - contract_class_cache.insert(class_hash_b, contract_class_b); + contract_class_cache.insert( + class_hash_b, + CompiledClass::Casm(Arc::new(contract_class_b)), + ); // SET GET_NUMBER_WRAPPER @@ -1574,7 +1598,10 @@ fn replace_class_contract_call_same_transaction() { let wrapper_address = Address(Felt252::from(2)); let wrapper_class_hash: ClassHash = [3; 32]; - contract_class_cache.insert(wrapper_class_hash, wrapper_contract_class); + contract_class_cache.insert( + wrapper_class_hash, + CompiledClass::Casm(Arc::new(wrapper_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(wrapper_address.clone(), wrapper_class_hash); @@ -1583,7 +1610,7 @@ fn replace_class_contract_call_same_transaction() { .insert(wrapper_address, nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // INITIALIZE STARKNET CONFIG let block_context = BlockContext::default(); @@ -1645,14 +1672,16 @@ fn call_contract_upgrade_cairo_0_to_cairo_1_same_transaction() { let contract_class_c = ContractClass::from_path("starknet_programs/get_number_c.json").unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); - let mut deprecated_contract_class_cache = HashMap::new(); + let mut contract_class_cache = HashMap::new(); let address = Address(Felt252::one()); let class_hash_c: ClassHash = Felt252::one().to_be_bytes(); let nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(class_hash_c, contract_class_c); + contract_class_cache.insert( + class_hash_c, + CompiledClass::Deprecated(Arc::new(contract_class_c)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1673,7 +1702,10 @@ fn call_contract_upgrade_cairo_0_to_cairo_1_same_transaction() { let class_hash_b: ClassHash = Felt252::from(2).to_be_bytes(); - casm_contract_class_cache.insert(class_hash_b, contract_class_b); + contract_class_cache.insert( + class_hash_b, + CompiledClass::Casm(Arc::new(contract_class_b)), + ); // SET GET_NUMBER_WRAPPER @@ -1689,7 +1721,10 @@ fn call_contract_upgrade_cairo_0_to_cairo_1_same_transaction() { let wrapper_address = Address(Felt252::from(2)); let wrapper_class_hash: ClassHash = [3; 32]; - casm_contract_class_cache.insert(wrapper_class_hash, wrapper_contract_class); + contract_class_cache.insert( + wrapper_class_hash, + CompiledClass::Casm(Arc::new(wrapper_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(wrapper_address.clone(), wrapper_class_hash); @@ -1698,11 +1733,7 @@ fn call_contract_upgrade_cairo_0_to_cairo_1_same_transaction() { .insert(wrapper_address, nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // INITIALIZE STARKNET CONFIG let block_context = BlockContext::default(); @@ -1762,14 +1793,16 @@ fn call_contract_downgrade_cairo_1_to_cairo_0_same_transaction() { let contract_class_c = ContractClass::from_path("starknet_programs/get_number_c.json").unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); - let mut deprecated_contract_class_cache = HashMap::new(); + let mut contract_class_cache = HashMap::new(); let address = Address(Felt252::one()); let class_hash_c: ClassHash = Felt252::one().to_be_bytes(); let nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(class_hash_c, contract_class_c); + contract_class_cache.insert( + class_hash_c, + CompiledClass::Deprecated(Arc::new(contract_class_c)), + ); // SET GET_NUMBER_B @@ -1783,7 +1816,10 @@ fn call_contract_downgrade_cairo_1_to_cairo_0_same_transaction() { let class_hash_b: ClassHash = Felt252::from(2).to_be_bytes(); - casm_contract_class_cache.insert(class_hash_b, contract_class_b); + contract_class_cache.insert( + class_hash_b, + CompiledClass::Casm(Arc::new(contract_class_b)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1806,7 +1842,10 @@ fn call_contract_downgrade_cairo_1_to_cairo_0_same_transaction() { let wrapper_address = Address(Felt252::from(2)); let wrapper_class_hash: ClassHash = [3; 32]; - casm_contract_class_cache.insert(wrapper_class_hash, wrapper_contract_class); + contract_class_cache.insert( + wrapper_class_hash, + CompiledClass::Casm(Arc::new(wrapper_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(wrapper_address.clone(), wrapper_class_hash); @@ -1815,11 +1854,7 @@ fn call_contract_downgrade_cairo_1_to_cairo_0_same_transaction() { .insert(wrapper_address, nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // INITIALIZE STARKNET CONFIG let block_context = BlockContext::default(); @@ -1879,14 +1914,16 @@ fn call_contract_replace_class_cairo_0() { let contract_class_c = ContractClass::from_path("starknet_programs/get_number_c.json").unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); - let mut deprecated_contract_class_cache = HashMap::new(); + let mut contract_class_cache = HashMap::new(); let address = Address(Felt252::one()); let class_hash_c: ClassHash = Felt252::one().to_be_bytes(); let nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(class_hash_c, contract_class_c); + contract_class_cache.insert( + class_hash_c, + CompiledClass::Deprecated(Arc::new(contract_class_c)), + ); // SET GET_NUMBER_B @@ -1896,7 +1933,10 @@ fn call_contract_replace_class_cairo_0() { let class_hash_d: ClassHash = Felt252::from(2).to_be_bytes(); - deprecated_contract_class_cache.insert(class_hash_d, contract_class_d); + contract_class_cache.insert( + class_hash_d, + CompiledClass::Deprecated(Arc::new(contract_class_d)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1919,7 +1959,10 @@ fn call_contract_replace_class_cairo_0() { let wrapper_address = Address(Felt252::from(2)); let wrapper_class_hash: ClassHash = [3; 32]; - casm_contract_class_cache.insert(wrapper_class_hash, wrapper_contract_class); + contract_class_cache.insert( + wrapper_class_hash, + CompiledClass::Casm(Arc::new(wrapper_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(wrapper_address.clone(), wrapper_class_hash); @@ -1928,11 +1971,7 @@ fn call_contract_replace_class_cairo_0() { .insert(wrapper_address, nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // INITIALIZE STARKNET CONFIG let block_context = BlockContext::default(); @@ -1998,7 +2037,7 @@ fn test_out_of_gas_failure() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2008,7 +2047,7 @@ fn test_out_of_gas_failure() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [].to_vec(); @@ -2075,7 +2114,7 @@ fn deploy_syscall_failure_uninitialized_class_hash() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2085,7 +2124,7 @@ fn deploy_syscall_failure_uninitialized_class_hash() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [Felt252::zero()].to_vec(); @@ -2151,7 +2190,7 @@ fn deploy_syscall_failure_in_constructor() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2167,10 +2206,13 @@ fn deploy_syscall_failure_in_constructor() { let f_c_program_data = include_bytes!("../starknet_programs/cairo1/failing_constructor.casm"); let f_c_contract_class: CasmContractClass = serde_json::from_slice(f_c_program_data).unwrap(); let f_c_class_hash = Felt252::one(); - contract_class_cache.insert(f_c_class_hash.to_be_bytes(), f_c_contract_class); + contract_class_cache.insert( + f_c_class_hash.to_be_bytes(), + CompiledClass::Casm(Arc::new(f_c_contract_class)), + ); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [f_c_class_hash].to_vec(); @@ -2238,7 +2280,7 @@ fn storage_read_no_value() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2248,7 +2290,7 @@ fn storage_read_no_value() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -2309,7 +2351,7 @@ fn storage_read_unavailable_address_domain() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2319,7 +2361,7 @@ fn storage_read_unavailable_address_domain() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -2383,7 +2425,7 @@ fn storage_write_unavailable_address_domain() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2393,7 +2435,7 @@ fn storage_write_unavailable_address_domain() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( @@ -2455,7 +2497,7 @@ fn library_call_failure() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2476,7 +2518,10 @@ fn library_call_failure() { let lib_class_hash: ClassHash = [2; 32]; let lib_nonce = Felt252::zero(); - contract_class_cache.insert(lib_class_hash, lib_contract_class); + contract_class_cache.insert( + lib_class_hash, + CompiledClass::Casm(Arc::new(lib_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(lib_address.clone(), lib_class_hash); @@ -2485,7 +2530,7 @@ fn library_call_failure() { .insert(lib_address, lib_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), Felt252::from_bytes_be(&lib_class_hash)].to_vec(); @@ -2564,7 +2609,7 @@ fn send_messages_to_l1_different_contract_calls() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2585,7 +2630,10 @@ fn send_messages_to_l1_different_contract_calls() { let send_msg_class_hash: ClassHash = [2; 32]; let send_msg_nonce = Felt252::zero(); - contract_class_cache.insert(send_msg_class_hash, send_msg_contract_class); + contract_class_cache.insert( + send_msg_class_hash, + CompiledClass::Casm(Arc::new(send_msg_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(send_msg_address.clone(), send_msg_class_hash); @@ -2594,7 +2642,7 @@ fn send_messages_to_l1_different_contract_calls() { .insert(send_msg_address, send_msg_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), 50.into(), 75.into()].to_vec(); @@ -2679,13 +2727,12 @@ fn send_messages_to_l1_different_contract_calls_cairo1_to_cairo0() { // Create state reader with class hash data let mut contract_class_cache = HashMap::new(); - let mut deprecated_contract_class_cache = HashMap::new(); let address = Address(1111.into()); let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2703,7 +2750,10 @@ fn send_messages_to_l1_different_contract_calls_cairo1_to_cairo0() { let send_msg_class_hash: ClassHash = [2; 32]; let send_msg_nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(send_msg_class_hash, send_msg_contract_class); + contract_class_cache.insert( + send_msg_class_hash, + CompiledClass::Deprecated(Arc::new(send_msg_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(send_msg_address.clone(), send_msg_class_hash); @@ -2712,11 +2762,7 @@ fn send_messages_to_l1_different_contract_calls_cairo1_to_cairo0() { .insert(send_msg_address, send_msg_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - Some(contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), 50.into(), 75.into()].to_vec(); @@ -2796,13 +2842,15 @@ fn send_messages_to_l1_different_contract_calls_cairo0_to_cairo1() { // Create state reader with class hash data let mut contract_class_cache = HashMap::new(); - let mut deprecated_contract_class_cache = HashMap::new(); let address = Address(1111.into()); let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2823,7 +2871,10 @@ fn send_messages_to_l1_different_contract_calls_cairo0_to_cairo1() { let send_msg_class_hash: ClassHash = [2; 32]; let send_msg_nonce = Felt252::zero(); - contract_class_cache.insert(send_msg_class_hash, send_msg_contract_class); + contract_class_cache.insert( + send_msg_class_hash, + CompiledClass::Casm(Arc::new(send_msg_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(send_msg_address.clone(), send_msg_class_hash); @@ -2832,11 +2883,7 @@ fn send_messages_to_l1_different_contract_calls_cairo0_to_cairo1() { .insert(send_msg_address, send_msg_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - Some(contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), 50.into(), 75.into()].to_vec(); @@ -2920,7 +2967,7 @@ fn keccak_syscall() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -2930,7 +2977,7 @@ fn keccak_syscall() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new(Arc::new(state_reader), None, Some(contract_class_cache)); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); let block_context = BlockContext::default(); let mut tx_execution_context = TransactionExecutionContext::new( diff --git a/tests/complex_contracts/amm_contracts/amm.rs b/tests/complex_contracts/amm_contracts/amm.rs index bbab48eb9..6f987ed64 100644 --- a/tests/complex_contracts/amm_contracts/amm.rs +++ b/tests/complex_contracts/amm_contracts/amm.rs @@ -54,11 +54,7 @@ fn swap(calldata: &[Felt252], call_config: &mut CallConfig) -> Result CachedState { let in_memory_state_reader = initial_in_memory_state_reader(); - let state_cache = ContractClassCache::new(); - - CachedState::new( - Arc::new(in_memory_state_reader), - Some(state_cache), - Some(HashMap::new()), - ) + CachedState::new(Arc::new(in_memory_state_reader), HashMap::new()) } fn expected_state_after_tx(fee: u128) -> CachedState { let in_memory_state_reader = initial_in_memory_state_reader(); - let contract_classes_cache = ContractClassCache::from([ + let contract_classes_cache = HashMap::from([ ( felt_to_hash(&TEST_CLASS_HASH.clone()), - ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&TEST_ACCOUNT_CONTRACT_CLASS_HASH.clone()), - ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&TEST_ERC20_CONTRACT_CLASS_HASH.clone()), - ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + )), ), ]); CachedState::new_for_testing( Arc::new(in_memory_state_reader), - Some(contract_classes_cache), state_cache_after_invoke_tx(fee), - Some(HashMap::new()), + contract_classes_cache, ) } @@ -372,19 +371,24 @@ fn initial_in_memory_state_reader() -> InMemoryStateReader { HashMap::from([ ( felt_to_hash(&TEST_ERC20_CONTRACT_CLASS_HASH), - ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&TEST_ACCOUNT_CONTRACT_CLASS_HASH), - ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&TEST_CLASS_HASH), - ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + )), ), ]), HashMap::new(), - HashMap::new(), ) } @@ -524,10 +528,6 @@ fn test_create_account_tx_test_state() { &state.contract_classes(), &expected_initial_state.contract_classes() ); - assert_eq!( - &state.casm_contract_classes(), - &expected_initial_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_initial_state.state_reader.address_to_class_hash @@ -542,21 +542,21 @@ fn test_create_account_tx_test_state() { ); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17 @@ -874,10 +874,6 @@ fn test_declare_tx() { &state.contract_classes(), &expected_initial_state.contract_classes() ); - assert_eq!( - &state.casm_contract_classes(), - &expected_initial_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_initial_state.state_reader.address_to_class_hash @@ -892,21 +888,21 @@ fn test_declare_tx() { ); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17 @@ -962,10 +958,6 @@ fn test_declarev2_tx() { &state.contract_classes(), &expected_initial_state.contract_classes() ); - assert_eq!( - &state.casm_contract_classes(), - &expected_initial_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_initial_state.state_reader.address_to_class_hash @@ -980,21 +972,21 @@ fn test_declarev2_tx() { ); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17 @@ -1308,10 +1300,6 @@ fn test_invoke_tx_state() { &state.contract_classes(), &expected_initial_state.contract_classes() ); - assert_eq!( - &state.casm_contract_classes(), - &expected_initial_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_initial_state.state_reader.address_to_class_hash @@ -1326,21 +1314,21 @@ fn test_invoke_tx_state() { ); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17 @@ -1362,10 +1350,6 @@ fn test_invoke_tx_state() { let expected_final_state = expected_state_after_tx(result.actual_fee); assert_eq!(&state.cache(), &expected_final_state.cache()); - assert_eq!( - &state.casm_contract_classes(), - &expected_final_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_final_state.state_reader.address_to_class_hash @@ -1389,10 +1373,6 @@ fn test_invoke_with_declarev2_tx() { &state.contract_classes(), &expected_initial_state.contract_classes() ); - assert_eq!( - &state.casm_contract_classes(), - &expected_initial_state.casm_contract_classes() - ); assert_eq!( &state.state_reader.address_to_class_hash, &expected_initial_state.state_reader.address_to_class_hash @@ -1407,21 +1387,21 @@ fn test_invoke_with_declarev2_tx() { ); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16 ])); assert!(&state .state_reader - .class_hash_to_contract_class + .class_hash_to_compiled_class .contains_key(&[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17 @@ -1488,19 +1468,11 @@ fn test_deploy_account() { assert_eq!(&state.cache(), &state_before.cache()); assert_eq!(&state.contract_classes(), &state_before.contract_classes()); - assert_eq!( - &state.casm_contract_classes(), - &state_before.casm_contract_classes() - ); let tx_info = deploy_account_tx .execute(&mut state, &block_context) .unwrap(); - assert_eq!( - state.casm_contract_classes(), - state_after.casm_contract_classes() - ); assert_eq!(state.cache(), state_after.cache()); let expected_validate_call_info = expected_validate_call_info( @@ -1599,22 +1571,26 @@ fn expected_deploy_account_states() -> ( HashMap::from([ ( felt_to_hash(&0x110.into()), - ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(TEST_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&0x111.into()), - ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + )), ), ( felt_to_hash(&0x1010.into()), - ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + )), ), ]), HashMap::new(), - HashMap::new(), )), - Some(ContractClassCache::new()), - Some(HashMap::new()), + HashMap::new(), ); state_before.set_storage_at( &( diff --git a/tests/multi_syscall_test.rs b/tests/multi_syscall_test.rs index 7aa7890d7..9f75e7925 100644 --- a/tests/multi_syscall_test.rs +++ b/tests/multi_syscall_test.rs @@ -1,6 +1,7 @@ use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_vm::felt::Felt252; use num_traits::{Num, Zero}; +use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass; use starknet_in_rust::utils::calculate_sn_keccak; use starknet_in_rust::EntryPointType; use starknet_in_rust::{ @@ -28,7 +29,7 @@ fn test_multiple_syscall() { let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert(class_hash, CompiledClass::Casm(Arc::new(contract_class))); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -38,11 +39,7 @@ fn test_multiple_syscall() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - None, - Some(contract_class_cache.clone()), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache.clone()); // Create an execution entry point let calldata = [].to_vec(); diff --git a/tests/storage.rs b/tests/storage.rs index 19d03359e..f13bc141c 100644 --- a/tests/storage.rs +++ b/tests/storage.rs @@ -1,6 +1,7 @@ use cairo_vm::felt::Felt252; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_traits::Zero; +use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass; use starknet_in_rust::EntryPointType; use starknet_in_rust::{ definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION}, @@ -50,7 +51,10 @@ fn integration_storage_test() { let storage_entry = (address.clone(), [90; 32]); let storage_value = Felt252::new(10902); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -66,7 +70,7 @@ fn integration_storage_test() { //* Create state with previous data //* --------------------------------------- - let mut state = CachedState::new(Arc::new(state_reader), Some(contract_class_cache), None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); //* ------------------------------------ //* Create execution entry point diff --git a/tests/syscalls.rs b/tests/syscalls.rs index 23ee0b2e0..03bfd188f 100644 --- a/tests/syscalls.rs +++ b/tests/syscalls.rs @@ -10,7 +10,6 @@ use cairo_vm::{ }, }; use num_traits::{Num, One, Zero}; -use starknet_in_rust::EntryPointType; use starknet_in_rust::{ definitions::{ block_context::{BlockContext, StarknetChainId}, @@ -20,16 +19,17 @@ use starknet_in_rust::{ execution_entry_point::ExecutionEntryPoint, CallInfo, CallType, L2toL1MessageInfo, OrderedEvent, OrderedL2ToL1Message, TransactionExecutionContext, }, - services::api::contract_classes::{ - compiled_class::CompiledClass, deprecated_contract_class::ContractClass, - }, + services::api::contract_classes::deprecated_contract_class::ContractClass, state::{ - cached_state::{CachedState, ContractClassCache}, + cached_state::CachedState, state_api::{State, StateReader}, }, state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager}, utils::{calculate_sn_keccak, felt_to_hash, Address, ClassHash}, }; +use starknet_in_rust::{ + services::api::contract_classes::compiled_class::CompiledClass, EntryPointType, +}; use std::{ collections::{HashMap, HashSet}, iter::empty, @@ -85,19 +85,23 @@ fn test_contract<'a>( state_reader .address_to_nonce_mut() .insert(contract_address.clone(), nonce); - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut storage_entries = Vec::new(); let contract_class_cache = { - let mut contract_class_cache = ContractClassCache::new(); + let mut contract_class_cache = HashMap::new(); for (class_hash, contract_path, contract_address) in extra_contracts { let contract_class = ContractClass::from_path(contract_path) .expect("Could not load extra contract from JSON"); - contract_class_cache.insert(class_hash, contract_class.clone()); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); if let Some((contract_address, data)) = contract_address { storage_entries.extend( @@ -108,15 +112,16 @@ fn test_contract<'a>( state_reader .address_to_class_hash_mut() .insert(contract_address.clone(), class_hash); - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); } } - Some(contract_class_cache) + contract_class_cache }; - let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache, None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); storage_entries .into_iter() .for_each(|(a, b, c)| state.set_storage_at(&(a, b), c)); @@ -1079,7 +1084,6 @@ fn deploy_cairo1_from_cairo0_with_constructor() { let test_contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); @@ -1087,8 +1091,14 @@ fn deploy_cairo1_from_cairo0_with_constructor() { let nonce = Felt252::zero(); // simulate contract declare - casm_contract_class_cache.insert(test_class_hash, test_contract_class.clone()); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Casm(Arc::new(test_contract_class.clone())), + ); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -1099,11 +1109,7 @@ fn deploy_cairo1_from_cairo0_with_constructor() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt, Felt252::one()].to_vec(); @@ -1180,7 +1186,6 @@ fn deploy_cairo1_from_cairo0_without_constructor() { let test_contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); @@ -1188,8 +1193,14 @@ fn deploy_cairo1_from_cairo0_without_constructor() { let nonce = Felt252::zero(); // simulate contract declare - casm_contract_class_cache.insert(test_class_hash, test_contract_class.clone()); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Casm(Arc::new(test_contract_class.clone())), + ); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -1200,11 +1211,7 @@ fn deploy_cairo1_from_cairo0_without_constructor() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt].to_vec(); @@ -1283,7 +1290,6 @@ fn deploy_cairo1_and_invoke() { let test_contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap(); // Create state reader with class hash data - let mut casm_contract_class_cache = HashMap::new(); let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); @@ -1291,8 +1297,14 @@ fn deploy_cairo1_and_invoke() { let nonce = Felt252::zero(); // simulate contract declare - casm_contract_class_cache.insert(test_class_hash, test_contract_class.clone()); - contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + test_class_hash, + CompiledClass::Casm(Arc::new(test_contract_class.clone())), + ); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader @@ -1303,11 +1315,7 @@ fn deploy_cairo1_and_invoke() { .insert(address.clone(), nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(contract_class_cache), - Some(casm_contract_class_cache), - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // arguments of deploy contract let calldata: Vec<_> = [test_felt_hash, salt].to_vec(); @@ -1408,13 +1416,16 @@ fn send_messages_to_l1_different_contract_calls() { .to_owned(); // Create state reader with class hash data - let mut deprecated_contract_class_cache = HashMap::new(); + let mut contract_class_cache = HashMap::new(); let address = Address(1111.into()); let class_hash: ClassHash = [1; 32]; let nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(class_hash, contract_class); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut state_reader = InMemoryStateReader::default(); state_reader .address_to_class_hash_mut() @@ -1432,7 +1443,10 @@ fn send_messages_to_l1_different_contract_calls() { let send_msg_class_hash: ClassHash = [2; 32]; let send_msg_nonce = Felt252::zero(); - deprecated_contract_class_cache.insert(send_msg_class_hash, send_msg_contract_class); + contract_class_cache.insert( + send_msg_class_hash, + CompiledClass::Deprecated(Arc::new(send_msg_contract_class)), + ); state_reader .address_to_class_hash_mut() .insert(send_msg_address.clone(), send_msg_class_hash); @@ -1441,11 +1455,7 @@ fn send_messages_to_l1_different_contract_calls() { .insert(send_msg_address, send_msg_nonce); // Create state from the state_reader and contract cache. - let mut state = CachedState::new( - Arc::new(state_reader), - Some(deprecated_contract_class_cache), - None, - ); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); // Create an execution entry point let calldata = [25.into(), 50.into(), 75.into()].to_vec(); diff --git a/tests/syscalls_errors.rs b/tests/syscalls_errors.rs index 17d85bb1f..1e9433e93 100644 --- a/tests/syscalls_errors.rs +++ b/tests/syscalls_errors.rs @@ -10,10 +10,7 @@ use starknet_in_rust::{ execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext, }, services::api::contract_classes::deprecated_contract_class::ContractClass, - state::{ - cached_state::{CachedState, ContractClassCache}, - state_api::State, - }, + state::{cached_state::CachedState, state_api::State}, state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager}, utils::{calculate_sn_keccak, Address, ClassHash}, }; @@ -21,6 +18,8 @@ use std::path::Path; use std::sync::Arc; use assert_matches::assert_matches; +use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass; +use std::collections::HashMap; #[allow(clippy::too_many_arguments)] fn test_contract<'a>( @@ -63,19 +62,23 @@ fn test_contract<'a>( state_reader .address_to_nonce_mut() .insert(contract_address.clone(), nonce); - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class)), + ); let mut storage_entries = Vec::new(); let contract_class_cache = { - let mut contract_class_cache = ContractClassCache::new(); + let mut contract_class_cache = HashMap::new(); for (class_hash, contract_path, contract_address) in extra_contracts { let contract_class = ContractClass::from_path(contract_path) .expect("Could not load extra contract from JSON"); - contract_class_cache.insert(class_hash, contract_class.clone()); + contract_class_cache.insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); if let Some((contract_address, data)) = contract_address { storage_entries.extend(data.into_iter().map(|(name, value)| { @@ -89,15 +92,16 @@ fn test_contract<'a>( state_reader .address_to_class_hash_mut() .insert(contract_address.clone(), class_hash); - state_reader - .class_hash_to_contract_class_mut() - .insert(class_hash, contract_class.clone()); + state_reader.class_hash_to_compiled_class_mut().insert( + class_hash, + CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ); } } - Some(contract_class_cache) + contract_class_cache }; - let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache, None); + let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache); storage_entries .into_iter() .for_each(|(a, b, c)| state.set_storage_at(&(a, b), c)); From 63e4bc4511616e60c93c6ce63e16e9192c79abb4 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 23 Aug 2023 22:21:51 +0200 Subject: [PATCH 2/5] Fix formatting and clippy. --- src/execution/execution_entry_point.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/execution/execution_entry_point.rs b/src/execution/execution_entry_point.rs index 1b6d33870..b16b500a9 100644 --- a/src/execution/execution_entry_point.rs +++ b/src/execution/execution_entry_point.rs @@ -128,10 +128,8 @@ impl ExecutionEntryPoint { }) } CompiledClass::Casm(contract_class) => { - let mut tmp_state = CachedState::new( - state.state_reader.clone(), - state.contract_classes.clone(), - ); + let mut tmp_state = + CachedState::new(state.state_reader.clone(), state.contract_classes.clone()); tmp_state.cache = state.cache.clone(); match self._execute( From 3efec30ca5008318f544a43ad6bbc50c03f6d269 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 24 Aug 2023 12:09:24 +0200 Subject: [PATCH 3/5] Remove unused code. --- src/state/cached_state.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/state/cached_state.rs b/src/state/cached_state.rs index 350877bd6..74451d8f9 100644 --- a/src/state/cached_state.rs +++ b/src/state/cached_state.rs @@ -65,14 +65,6 @@ impl CachedState { self.contract_classes = contract_classes; Ok(()) } - - // /// Returns the casm classes. - // #[allow(dead_code)] - // pub(crate) fn get_casm_classes(&mut self) -> Result<&CasmClassCache, StateError> { - // self.casm_contract_classes - // .as_ref() - // .ok_or(StateError::MissingCasmClassCache) - // } } impl StateReader for CachedState { From a4afabb9d009e77da492e95d4d67aece6435aa15 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 24 Aug 2023 12:41:27 +0200 Subject: [PATCH 4/5] Unify contract classes in the state traits too. --- bench/internals.rs | 19 +++++++-- cli/src/main.rs | 9 ++++- src/lib.rs | 20 ++++++++-- src/state/cached_state.rs | 44 ++++++--------------- src/state/state_api.rs | 13 +------ src/syscalls/deprecated_syscall_handler.rs | 11 +++++- src/testing/erc20.rs | 10 ++++- src/testing/state.rs | 12 ++++-- src/transaction/declare.rs | 7 +++- src/transaction/declare_v2.rs | 7 +++- src/transaction/deploy.rs | 25 +++++------- src/transaction/deploy_account.rs | 8 +++- src/transaction/invoke_function.rs | 45 +++++++++++++++++----- src/transaction/l1_handler.rs | 9 ++++- tests/complex_contracts/utils.rs | 14 +++++-- tests/deploy_account.rs | 15 +++++--- tests/internals.rs | 10 +++-- 17 files changed, 177 insertions(+), 101 deletions(-) diff --git a/bench/internals.rs b/bench/internals.rs index ea6906013..430dc5446 100644 --- a/bench/internals.rs +++ b/bench/internals.rs @@ -11,7 +11,9 @@ use starknet_in_rust::{ constants::{TRANSACTION_VERSION, VALIDATE_ENTRY_POINT_SELECTOR}, }, hash_utils::calculate_contract_address, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::in_memory_state_reader::InMemoryStateReader, state::{cached_state::CachedState, state_api::State}, transaction::{declare::Declare, Deploy, DeployAccount, InvokeFunction}, @@ -64,7 +66,10 @@ fn deploy_account() { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = &Default::default(); @@ -132,7 +137,10 @@ fn deploy() { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = &Default::default(); @@ -167,7 +175,10 @@ fn invoke() { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = &Default::default(); diff --git a/cli/src/main.rs b/cli/src/main.rs index bc9e0d6bc..1248123dd 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -23,7 +23,9 @@ use starknet_in_rust::{ hash_utils::calculate_contract_address, parser_errors::ParserError, serde_structs::read_abi, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::{cached_state::CachedState, state_api::State}, state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager}, transaction::{error::TransactionError, InvokeFunction}, @@ -110,7 +112,10 @@ fn declare_parser( let contract_class = ContractClass::from_path(&args.contract).map_err(ContractAddressError::Program)?; let class_hash = compute_deprecated_class_hash(&contract_class)?; - cached_state.set_contract_class(&felt_to_hash(&class_hash), &contract_class)?; + cached_state.set_contract_class( + &felt_to_hash(&class_hash), + &CompiledClass::Deprecated(Arc::new(contract_class.clone())), + )?; let tx_hash = calculate_declare_transaction_hash( &contract_class, diff --git a/src/lib.rs b/src/lib.rs index 42677df79..1b9a441e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -684,7 +684,10 @@ mod test { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = &Default::default(); @@ -760,7 +763,10 @@ mod test { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = Default::default(); @@ -821,7 +827,10 @@ mod test { let mut state = CachedState::new(state_reader, HashMap::new()); state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .set_contract_class( + &CLASS_HASH_BYTES, + &CompiledClass::Deprecated(Arc::new(CONTRACT_CLASS.clone())), + ) .unwrap(); let block_context = &Default::default(); @@ -945,7 +954,10 @@ mod test { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let mut block_context = BlockContext::default(); diff --git a/src/state/cached_state.rs b/src/state/cached_state.rs index 74451d8f9..29f863b30 100644 --- a/src/state/cached_state.rs +++ b/src/state/cached_state.rs @@ -4,13 +4,10 @@ use super::{ }; use crate::{ core::errors::state_errors::StateError, - services::api::contract_classes::{ - compiled_class::CompiledClass, deprecated_contract_class::ContractClass, - }, + services::api::contract_classes::compiled_class::CompiledClass, state::StateDiff, utils::{subtract_mappings, to_cache_state_storage_mapping, Address, ClassHash}, }; -use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_vm::felt::Felt252; use getset::{Getters, MutGetters}; use num_traits::Zero; @@ -167,8 +164,6 @@ impl StateReader for CachedState { } // II: FETCHING FROM STATE_READER - // TODO: Should this modify the cache? - // TODO: Related: Isn't this exact method available in `impl State for CachedState`? self.state_reader.get_contract_class(class_hash) } } @@ -178,14 +173,10 @@ impl State for CachedState { fn set_contract_class( &mut self, class_hash: &ClassHash, - contract_class: &ContractClass, + contract_class: &CompiledClass, ) -> Result<(), StateError> { - // TODO: Could this method's signature use `CompiledClass` instead of `ContractClass`? Or - // even better, could the entire trait use `Arc`? - self.contract_classes.insert( - *class_hash, - CompiledClass::Deprecated(Arc::new(contract_class.clone())), - ); + self.contract_classes + .insert(*class_hash, contract_class.clone()); Ok(()) } @@ -249,20 +240,6 @@ impl State for CachedState { Ok(()) } - fn set_compiled_class( - &mut self, - compiled_class_hash: &Felt252, - casm_class: CasmContractClass, - ) -> Result<(), StateError> { - let compiled_class_hash = compiled_class_hash.to_be_bytes(); - self.contract_classes.insert( - compiled_class_hash, - CompiledClass::Casm(Arc::new(casm_class)), - ); - - Ok(()) - } - fn set_compiled_class_hash( &mut self, class_hash: &Felt252, @@ -423,13 +400,13 @@ impl State for CachedState { CompiledClass::Casm(ref casm_class) => { // We call this method instead of state_reader's in order to update the cache's class_hash_initial_values map let compiled_class_hash = self.get_compiled_class_hash(class_hash)?; - self.set_compiled_class( - &Felt252::from_bytes_be(&compiled_class_hash), - casm_class.as_ref().clone(), + self.set_contract_class( + &compiled_class_hash, + &CompiledClass::Casm(casm_class.clone()), )?; } CompiledClass::Deprecated(ref contract) => { - self.set_contract_class(class_hash, &contract.clone())? + self.set_contract_class(class_hash, &CompiledClass::Deprecated(contract.clone()))? } } Ok(contract) @@ -440,7 +417,10 @@ impl State for CachedState { mod tests { use super::*; - use crate::state::in_memory_state_reader::InMemoryStateReader; + use crate::{ + services::api::contract_classes::deprecated_contract_class::ContractClass, + state::in_memory_state_reader::InMemoryStateReader, + }; use num_traits::One; diff --git a/src/state/state_api.rs b/src/state/state_api.rs index 27ffa8599..885eee4be 100644 --- a/src/state/state_api.rs +++ b/src/state/state_api.rs @@ -1,13 +1,10 @@ use super::state_cache::StorageEntry; use crate::{ core::errors::state_errors::StateError, - services::api::contract_classes::{ - compiled_class::CompiledClass, deprecated_contract_class::ContractClass, - }, + services::api::contract_classes::compiled_class::CompiledClass, state::StateDiff, utils::{Address, ClassHash, CompiledClassHash}, }; -use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_vm::felt::Felt252; pub trait StateReader { @@ -30,7 +27,7 @@ pub trait State { fn set_contract_class( &mut self, class_hash: &ClassHash, - contract_class: &ContractClass, + contract_class: &CompiledClass, ) -> Result<(), StateError>; fn deploy_contract( @@ -49,12 +46,6 @@ pub trait State { class_hash: ClassHash, ) -> Result<(), StateError>; - fn set_compiled_class( - &mut self, - compiled_class_hash: &Felt252, - casm_class: CasmContractClass, - ) -> Result<(), StateError>; - fn set_compiled_class_hash( &mut self, class_hash: &Felt252, diff --git a/src/syscalls/deprecated_syscall_handler.rs b/src/syscalls/deprecated_syscall_handler.rs index fb9b38c1a..7ee047b76 100644 --- a/src/syscalls/deprecated_syscall_handler.rs +++ b/src/syscalls/deprecated_syscall_handler.rs @@ -227,6 +227,7 @@ mod tests { use std::sync::Arc; use super::*; + use crate::services::api::contract_classes::compiled_class::CompiledClass; use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; use crate::{ add_segments, allocate_selector, any_box, @@ -1034,7 +1035,10 @@ mod tests { .syscall_handler .starknet_storage_state .state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); // Execute Deploy hint @@ -1133,7 +1137,10 @@ mod tests { .syscall_handler .starknet_storage_state .state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); // Execute Deploy hint diff --git a/src/testing/erc20.rs b/src/testing/erc20.rs index 717097e53..97dbf7cb8 100644 --- a/src/testing/erc20.rs +++ b/src/testing/erc20.rs @@ -142,7 +142,10 @@ fn test_erc20_cairo2() { serde_json::from_slice(program_data_account).unwrap(); state - .set_compiled_class(&felt_str!("1"), contract_class_account) + .set_contract_class( + &felt_str!("1").to_be_bytes(), + &CompiledClass::Casm(Arc::new(contract_class_account)), + ) .unwrap(); let contract_address_salt = @@ -181,7 +184,10 @@ fn test_erc20_cairo2() { serde_json::from_slice(program_data_account).unwrap(); state - .set_compiled_class(&felt_str!("1"), contract_class_account) + .set_contract_class( + &felt_str!("1").to_be_bytes(), + &CompiledClass::Casm(Arc::new(contract_class_account)), + ) .unwrap(); let contract_address_salt = felt_str!("123123123123123"); diff --git a/src/testing/state.rs b/src/testing/state.rs index a16bf66fe..8158786ce 100644 --- a/src/testing/state.rs +++ b/src/testing/state.rs @@ -1,5 +1,6 @@ use super::{state_error::StarknetStateError, type_utils::ExecutionInfo}; use crate::execution::execution_entry_point::ExecutionResult; +use crate::services::api::contract_classes::compiled_class::CompiledClass; use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; use crate::{ definitions::{block_context::BlockContext, constants::TRANSACTION_VERSION}, @@ -201,8 +202,10 @@ impl StarknetState { let contract_hash = deploy.contract_hash; let mut tx = Transaction::Deploy(deploy); - self.state - .set_contract_class(&contract_hash, &contract_class)?; + self.state.set_contract_class( + &contract_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + )?; let tx_execution_info = self.execute_tx(&mut tx, remaining_gas)?; Ok((contract_address, tx_execution_info)) @@ -472,7 +475,10 @@ mod tests { starknet_state .state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); // -------------------------------------------- diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index b7bce7833..ce93bc4b1 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -29,6 +29,8 @@ use num_traits::Zero; use super::fee::charge_fee; use super::{verify_version, Transaction}; +use crate::services::api::contract_classes::compiled_class::CompiledClass; +use std::sync::Arc; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// Represents an internal transaction in the StarkNet network that is a declaration of a Cairo @@ -281,7 +283,10 @@ impl Declare { self.skip_fee_transfer, )?; - state.set_contract_class(&self.class_hash, &self.contract_class)?; + state.set_contract_class( + &self.class_hash, + &CompiledClass::Deprecated(Arc::new(self.contract_class.clone())), + )?; tx_exec_info.set_fee_info(actual_fee, fee_transfer_info); diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 406e4b50c..577c0738b 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -5,6 +5,7 @@ use crate::definitions::constants::QUERY_VERSION_BASE; use crate::execution::execution_entry_point::ExecutionResult; use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; +use crate::services::api::contract_classes::compiled_class::CompiledClass; use crate::state::cached_state::CachedState; use crate::{ core::transaction_hash::calculate_declare_v2_transaction_hash, @@ -26,6 +27,7 @@ use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass; use cairo_vm::felt::Felt252; use num_traits::Zero; +use std::sync::Arc; /// Represents a declare transaction in the starknet network. /// Declare creates a blueprint of a contract class that is used to deploy instances of the contract @@ -372,7 +374,10 @@ impl DeclareV2 { )); } state.set_compiled_class_hash(&self.sierra_class_hash, &self.compiled_class_hash)?; - state.set_compiled_class(&self.compiled_class_hash, casm_class)?; + state.set_contract_class( + &self.compiled_class_hash.to_be_bytes(), + &CompiledClass::Casm(Arc::new(casm_class)), + )?; Ok(()) } diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index c6132445c..b65e8559e 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -151,18 +151,7 @@ impl Deploy { state: &mut CachedState, block_context: &BlockContext, ) -> Result { - match self.contract_class.clone() { - CompiledClass::Casm(contract_class) => { - state.set_compiled_class( - &Felt252::from_bytes_be(&self.contract_hash), - contract_class.as_ref().clone(), - )?; - } - CompiledClass::Deprecated(contract_class) => { - state.set_contract_class(&self.contract_hash, &contract_class)?; - } - } - + state.set_contract_class(&self.contract_hash, &self.contract_class)?; state.deploy_contract(self.contract_address.clone(), self.contract_hash)?; if self.constructor_entry_points_empty(self.contract_class.clone())? { @@ -172,10 +161,10 @@ impl Deploy { self.invoke_constructor(state, block_context) } } + /// Executes the contract without constructor /// ## Parameters /// - state: A state that implements the [`State`] and [`StateReader`] traits. - pub fn handle_empty_constructor( &self, state: &mut S, @@ -386,7 +375,10 @@ mod tests { let class_hash_bytes = class_hash.to_be_bytes(); state - .set_contract_class(&class_hash_bytes, &contract_class) + .set_contract_class( + &class_hash_bytes, + &CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ) .unwrap(); let internal_deploy = @@ -413,7 +405,10 @@ mod tests { class_hash_bytes.copy_from_slice(&class_hash.to_bytes_be()); state - .set_contract_class(&class_hash_bytes, &contract_class) + .set_contract_class( + &class_hash_bytes, + &CompiledClass::Deprecated(Arc::new(contract_class.clone())), + ) .unwrap(); let internal_deploy = Deploy::new( diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 1a048d450..eccb34117 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -465,7 +465,9 @@ mod tests { .unwrap(); let class_hash = internal_deploy.class_hash(); - state.set_contract_class(class_hash, &contract).unwrap(); + state + .set_contract_class(class_hash, &CompiledClass::Deprecated(Arc::new(contract))) + .unwrap(); internal_deploy.execute(&mut state, &block_context).unwrap(); assert_matches!( internal_deploy_error @@ -501,7 +503,9 @@ mod tests { .unwrap(); let class_hash = internal_deploy.class_hash(); - state.set_contract_class(class_hash, &contract).unwrap(); + state + .set_contract_class(class_hash, &CompiledClass::Deprecated(Arc::new(contract))) + .unwrap(); internal_deploy.execute(&mut state, &block_context).unwrap(); } } diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index 33a5d1b3f..ef707f510 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -458,7 +458,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let result = internal_invoke_function @@ -527,7 +530,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let result = internal_invoke_function @@ -592,7 +598,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let expected_error = @@ -651,7 +660,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let result = internal_invoke_function @@ -716,7 +728,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let expected_error = @@ -775,7 +790,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let block_context = BlockContext::default(); @@ -832,7 +850,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let mut block_context = BlockContext::default(); @@ -890,7 +911,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); internal_invoke_function @@ -953,7 +977,10 @@ mod tests { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let expected_error = diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 7cb1766d9..4f84c910d 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -211,7 +211,9 @@ mod test { sync::Arc, }; - use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; + use crate::services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::EntryPointType, + }; use cairo_vm::{ felt::{felt_str, Felt252}, vm::runners::cairo_runner::ExecutionResources, @@ -272,7 +274,10 @@ mod test { state.set_contract_classes(HashMap::new()).unwrap(); state - .set_contract_class(&class_hash, &contract_class) + .set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let mut block_context = BlockContext::default(); diff --git a/tests/complex_contracts/utils.rs b/tests/complex_contracts/utils.rs index 82c0c5b94..1deeba647 100644 --- a/tests/complex_contracts/utils.rs +++ b/tests/complex_contracts/utils.rs @@ -12,14 +12,19 @@ use starknet_in_rust::{ execution_entry_point::{ExecutionEntryPoint, ExecutionResult}, CallInfo, CallType, TransactionExecutionContext, }, - services::api::contract_classes::deprecated_contract_class::ContractClass, + services::api::contract_classes::{ + compiled_class::CompiledClass, deprecated_contract_class::ContractClass, + }, state::{cached_state::CachedState, state_api::State}, state::{in_memory_state_reader::InMemoryStateReader, ExecutionResourcesManager}, transaction::{error::TransactionError, Deploy}, utils::{calculate_sn_keccak, Address}, }; use starknet_in_rust::{ContractEntryPoint, EntryPointType}; -use std::collections::{HashMap, HashSet}; +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, +}; pub struct CallConfig<'a> { pub state: &'a mut CachedState, @@ -156,7 +161,10 @@ pub fn deploy( )?, }; let class_hash = internal_deploy.class_hash(); - state.set_contract_class(&class_hash, &contract_class)?; + state.set_contract_class( + &class_hash, + &CompiledClass::Deprecated(Arc::new(contract_class)), + )?; let tx_execution_info = internal_deploy.apply(state, block_context)?; diff --git a/tests/deploy_account.rs b/tests/deploy_account.rs index 58790be6a..6da3256af 100644 --- a/tests/deploy_account.rs +++ b/tests/deploy_account.rs @@ -4,7 +4,6 @@ use cairo_vm::{ }; use lazy_static::lazy_static; use num_traits::Zero; -use starknet_in_rust::EntryPointType; use starknet_in_rust::{ core::contract_address::compute_deprecated_class_hash, definitions::{ @@ -20,6 +19,9 @@ use starknet_in_rust::{ utils::Address, CasmContractClass, }; +use starknet_in_rust::{ + services::api::contract_classes::compiled_class::CompiledClass, EntryPointType, +}; use std::{ collections::{HashMap, HashSet}, sync::Arc, @@ -43,7 +45,10 @@ fn internal_deploy_account() { let class_hash_bytes = class_hash.to_be_bytes(); state - .set_contract_class(&class_hash_bytes, &contract_class) + .set_contract_class( + &class_hash_bytes, + &CompiledClass::Deprecated(Arc::new(contract_class)), + ) .unwrap(); let contract_address_salt = @@ -123,9 +128,9 @@ fn internal_deploy_account_cairo1() { let contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap(); state - .set_compiled_class( - &TEST_ACCOUNT_COMPILED_CONTRACT_CLASS_HASH.clone(), - contract_class, + .set_contract_class( + &TEST_ACCOUNT_COMPILED_CONTRACT_CLASS_HASH.to_be_bytes(), + &CompiledClass::Casm(Arc::new(contract_class)), ) .unwrap(); diff --git a/tests/internals.rs b/tests/internals.rs index 335c0cf62..94f845e36 100644 --- a/tests/internals.rs +++ b/tests/internals.rs @@ -1688,13 +1688,17 @@ fn expected_deploy_account_states() -> ( state_after .set_contract_class( &felt_to_hash(&0x1010.into()), - &ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + &CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ERC20_CONTRACT_PATH).unwrap(), + )), ) .unwrap(); state_after .set_contract_class( &felt_to_hash(&0x111.into()), - &ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + &CompiledClass::Deprecated(Arc::new( + ContractClass::from_path(ACCOUNT_CONTRACT_PATH).unwrap(), + )), ) .unwrap(); @@ -2010,7 +2014,7 @@ fn test_library_call_with_declare_v2() { .insert(address.clone(), nonce); state - .set_compiled_class(&Felt252::from_bytes_be(&class_hash), contract_class) + .set_contract_class(&class_hash, &CompiledClass::Casm(Arc::new(contract_class))) .unwrap(); let create_execute_extrypoint = |selector: &BigUint, From 6cad73788f2c759b1d18476eb65b10060552048b Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Wed, 30 Aug 2023 13:02:52 +0200 Subject: [PATCH 5/5] Restore type alias. --- src/state/cached_state.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/state/cached_state.rs b/src/state/cached_state.rs index 29f863b30..f451d5e97 100644 --- a/src/state/cached_state.rs +++ b/src/state/cached_state.rs @@ -16,6 +16,8 @@ use std::{ sync::Arc, }; +pub type ContractClassCache = HashMap; + pub const UNINITIALIZED_CLASS_HASH: &ClassHash = &[0u8; 32]; /// Represents a cached state of contract classes with optional caches. @@ -25,12 +27,12 @@ pub struct CachedState { #[getset(get = "pub", get_mut = "pub")] pub(crate) cache: StateCache, #[get = "pub"] - pub(crate) contract_classes: HashMap, + pub(crate) contract_classes: ContractClassCache, } impl CachedState { /// Constructor, creates a new cached state. - pub fn new(state_reader: Arc, contract_classes: HashMap) -> Self { + pub fn new(state_reader: Arc, contract_classes: ContractClassCache) -> Self { Self { cache: StateCache::default(), state_reader, @@ -42,7 +44,7 @@ impl CachedState { pub fn new_for_testing( state_reader: Arc, cache: StateCache, - contract_classes: HashMap, + contract_classes: ContractClassCache, ) -> Self { Self { cache, @@ -54,7 +56,7 @@ impl CachedState { /// Sets the contract classes cache. pub fn set_contract_classes( &mut self, - contract_classes: HashMap, + contract_classes: ContractClassCache, ) -> Result<(), StateError> { if !self.contract_classes.is_empty() { return Err(StateError::AssignedContractClassCache);