diff --git a/src/lib.rs b/src/lib.rs index 778cbf56e..fb6e25304 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -933,11 +933,7 @@ mod test { #[test] fn test_deploy_and_invoke_simulation() { let state_reader = Arc::new(InMemoryStateReader::default()); - let mut state = CachedState::new(state_reader, Some(Default::default()), None); - - state - .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) - .unwrap(); + let state = CachedState::new(state_reader, Some(Default::default()), None); let block_context = &Default::default(); diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index 2ada7dae7..52112ead4 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -1,5 +1,7 @@ use crate::execution::execution_entry_point::ExecutionResult; -use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; +use crate::services::api::contract_classes::deprecated_contract_class::{ + ContractClass, EntryPointType, +}; use crate::state::cached_state::CachedState; use crate::syscalls::syscall_handler_errors::SyscallHandlerError; use crate::{ @@ -17,10 +19,7 @@ use crate::{ }, hash_utils::calculate_contract_address, services::api::{ - contract_class_errors::ContractClassError, - contract_classes::{ - compiled_class::CompiledClass, deprecated_contract_class::ContractClass, - }, + contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass, }, state::state_api::{State, StateReader}, state::ExecutionResourcesManager, @@ -40,6 +39,7 @@ pub struct Deploy { pub contract_address: Address, pub contract_address_salt: Felt252, pub contract_hash: ClassHash, + pub contract_class: CompiledClass, pub constructor_calldata: Vec, pub tx_type: TransactionType, pub skip_validate: bool, @@ -80,6 +80,7 @@ impl Deploy { contract_address, contract_address_salt, contract_hash, + contract_class: CompiledClass::Deprecated(Box::new(contract_class)), constructor_calldata, tx_type: TransactionType::Deploy, skip_validate: false, @@ -113,6 +114,7 @@ impl Deploy { contract_address_salt, contract_hash, constructor_calldata, + contract_class: CompiledClass::Deprecated(Box::new(contract_class)), tx_type: TransactionType::Deploy, skip_validate: false, skip_execute: false, @@ -147,11 +149,21 @@ 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, + )?; + } + CompiledClass::Deprecated(contract_class) => { + state.set_contract_class(&self.contract_hash, &contract_class)?; + } + } + state.deploy_contract(self.contract_address.clone(), self.contract_hash)?; - let class_hash: ClassHash = self.contract_hash; - let contract_class = state.get_contract_class(&class_hash)?; - if self.constructor_entry_points_empty(contract_class)? { + if self.constructor_entry_points_empty(self.contract_class.clone())? { // Contract has no constructors Ok(self.handle_empty_constructor(state)?) } else { @@ -323,13 +335,9 @@ mod tests { //transform class_hash to [u8; 32] let class_hash_bytes = class_hash.to_be_bytes(); - state - .set_contract_class(&class_hash_bytes, &contract_class) - .unwrap(); - let internal_deploy = Deploy::new( 0.into(), - contract_class, + contract_class.clone(), vec![10.into()], 0.into(), 0.into(), @@ -340,6 +348,11 @@ mod tests { let _result = internal_deploy.apply(&mut state, &block_context).unwrap(); + assert_eq!( + state.get_contract_class(&class_hash_bytes).unwrap(), + CompiledClass::Deprecated(Box::new(contract_class)) + ); + assert_eq!( state .get_class_hash_at(&internal_deploy.contract_address) diff --git a/src/transaction/error.rs b/src/transaction/error.rs index 4dd439599..21c4ad38d 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -28,6 +28,8 @@ pub enum TransactionError { InvalidMaxFee, #[error("The nonce field in Declare transactions of version 0 must be 0.")] InvalidNonce, + #[error("Couldn't convert from {0} to {1}")] + Conversion(String, String), #[error("The signature field in Declare transactions of version 0 must be an empty list.")] InvalidSignature, #[error("An InvokeFunction transaction (version != 0) must have a nonce.")] diff --git a/tests/internals.rs b/tests/internals.rs index 60cbb9872..98a9a1526 100644 --- a/tests/internals.rs +++ b/tests/internals.rs @@ -21,6 +21,7 @@ use starknet_in_rust::definitions::constants::{ }; use starknet_in_rust::execution::execution_entry_point::ExecutionEntryPoint; use starknet_in_rust::execution::TransactionExecutionContext; +use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledClass; use starknet_in_rust::services::api::contract_classes::deprecated_contract_class::ContractClass; use starknet_in_rust::state::ExecutionResourcesManager; use starknet_in_rust::transaction::fee::calculate_tx_fee; @@ -727,6 +728,14 @@ fn declarev2_tx() -> DeclareV2 { } fn deploy_fib_syscall() -> Deploy { + #[cfg(not(feature = "cairo_1_tests"))] + let program_data = include_bytes!("../starknet_programs/cairo2/fibonacci.sierra"); + #[cfg(feature = "cairo_1_tests")] + let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.sierra"); + let sierra_contract_class: SierraContractClass = serde_json::from_slice(program_data).unwrap(); + let casm_class = CasmContractClass::from_contract_class(sierra_contract_class, true).unwrap(); + let contract_class = CompiledClass::Casm(Box::new(casm_class)); + let contract_hash; #[cfg(not(feature = "cairo_1_tests"))] { @@ -742,6 +751,7 @@ fn deploy_fib_syscall() -> Deploy { contract_address: TEST_FIB_CONTRACT_ADDRESS.clone(), contract_address_salt: 0.into(), contract_hash, + contract_class, constructor_calldata: Vec::new(), tx_type: TransactionType::Deploy, skip_execute: false,