diff --git a/src/lib.rs b/src/lib.rs index cb9bdd7db..bee36a39e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1004,13 +1004,21 @@ mod test { fn test_declare_v2_with_invalid_compiled_class_hash() { let (block_context, mut state) = create_account_tx_test_state().unwrap(); let mut declare_v2 = declarev2_tx(); - declare_v2.compiled_class_hash = Felt252::from(1); + let real_casm_class_hash = declare_v2.compiled_class_hash; + let wrong_casm_class_hash = Felt252::from(1); + declare_v2.compiled_class_hash = wrong_casm_class_hash.clone(); let declare_tx = Transaction::DeclareV2(Box::new(declare_v2)); let err = declare_tx .execute(&mut state, &block_context, INITIAL_GAS_COST) .unwrap_err(); - assert_eq!(err.to_string(), "Invalid compiled class, expected class hash: \"1948962768849191111780391610229754715773924969841143100991524171924131413970\", but received: \"1\"".to_string()); + assert_eq!( + err.to_string(), + format!( + "Invalid compiled class, expected class hash: {}, but received: {}", + real_casm_class_hash, wrong_casm_class_hash + ) + ); } } diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 2cb931b3c..c034fb47f 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -692,4 +692,133 @@ mod tests { assert_eq!(expected_casm_class, casm_class); } + + #[test] + fn create_declare_v2_with_casm_contract_class_none_test() { + // read file to create sierra contract class + let version; + let path; + #[cfg(not(feature = "cairo_1_tests"))] + { + version = Felt252::from(2); + path = PathBuf::from("starknet_programs/cairo2/fibonacci.sierra"); + } + + #[cfg(feature = "cairo_1_tests")] + { + version = Felt252::from(1); + path = PathBuf::from("starknet_programs/cairo1/fibonacci.sierra"); + } + + let file = File::open(path).unwrap(); + let reader = BufReader::new(file); + let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass = + serde_json::from_reader(reader).unwrap(); + let sender_address = Address(1.into()); + let casm_class = + CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap(); + let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap(); + + // create internal declare v2 + + let internal_declare = DeclareV2::new_with_tx_hash( + &sierra_contract_class, + None, + casm_class_hash, + sender_address, + 0, + version, + [1.into()].to_vec(), + Felt252::zero(), + Felt252::one(), + ) + .unwrap(); + + // crate state to store casm contract class + let casm_contract_class_cache = HashMap::new(); + let state_reader = InMemoryStateReader::default(); + let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + + // call compile and store + assert!(internal_declare + .compile_and_store_casm_class(&mut state) + .is_ok()); + + // test we can retreive the data + let expected_casm_class = CasmContractClass::from_contract_class( + internal_declare.sierra_contract_class.clone(), + true, + ) + .unwrap(); + + let casm_class = match state + .get_contract_class(&internal_declare.compiled_class_hash.to_be_bytes()) + .unwrap() + { + CompiledClass::Casm(casm) => *casm, + _ => unreachable!(), + }; + + assert_eq!(expected_casm_class, casm_class); + } + + #[test] + fn create_declare_v2_wrong_casm_class_hash_test() { + // read file to create sierra contract class + let version; + let path; + #[cfg(not(feature = "cairo_1_tests"))] + { + version = Felt252::from(2); + path = PathBuf::from("starknet_programs/cairo2/fibonacci.sierra"); + } + + #[cfg(feature = "cairo_1_tests")] + { + version = Felt252::from(1); + path = PathBuf::from("starknet_programs/cairo1/fibonacci.sierra"); + } + + let file = File::open(path).unwrap(); + let reader = BufReader::new(file); + let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass = + serde_json::from_reader(reader).unwrap(); + let sender_address = Address(1.into()); + let casm_class = + CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap(); + let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap(); + + let sended_class_hash = Felt252::from(5); + // create internal declare v2 + + let internal_declare = DeclareV2::new_with_tx_hash( + &sierra_contract_class, + None, + sended_class_hash.clone(), + sender_address, + 0, + version, + [1.into()].to_vec(), + Felt252::zero(), + Felt252::one(), + ) + .unwrap(); + + // crate state to store casm contract class + let casm_contract_class_cache = HashMap::new(); + let state_reader = InMemoryStateReader::default(); + let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); + + let expected_err = format!( + "Invalid compiled class, expected class hash: {}, but received: {}", + casm_class_hash, sended_class_hash + ); + assert_eq!( + internal_declare + .compile_and_store_casm_class(&mut state) + .unwrap_err() + .to_string(), + expected_err + ); + } } diff --git a/src/transaction/error.rs b/src/transaction/error.rs index 6ed79c64d..e7acd92cc 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -135,6 +135,6 @@ pub enum TransactionError { CallInfoIsNone, #[error("Unsupported version {0:?}")] UnsupportedVersion(String), - #[error("Invalid compiled class, expected class hash: {0:?}, but received: {1:?}")] + #[error("Invalid compiled class, expected class hash: {0}, but received: {1}")] InvalidCompiledClassHash(String, String), }