diff --git a/unit_tests/Cargo.toml b/unit_tests/Cargo.toml index 036972d..32ca01c 100644 --- a/unit_tests/Cargo.toml +++ b/unit_tests/Cargo.toml @@ -20,6 +20,7 @@ macro_utils = { path = "../macro_utils/" } rand = "0.8.5" serde_json = "1.0" once_cell = "1.8.0" +base64 = "0.13.0" colored = "2.0" [dev-dependencies] diff --git a/unit_tests/src/constants.rs b/unit_tests/src/constants.rs index 61dc376..9a65982 100644 --- a/unit_tests/src/constants.rs +++ b/unit_tests/src/constants.rs @@ -142,7 +142,8 @@ pub const TRANSACTION_REVERTED: &str = pub const ACCOUNT_CONTRACT: &str = ""; pub const TEST_CONTRACT_ADDRESS: &str = ""; pub const CAIRO_1_ACCOUNT_CONTRACT_CLASS_HASH: &str = ""; -pub const TEST_CONTRACT_CLASS_HASH: &str = "0x01a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003"; +pub const TEST_CONTRACT_CLASS_HASH_V0: &str = "0x036e5b6081df2174189fb83800d2a09132286dcd1004ad960a0c8d69364e6e9a"; +pub const TEST_CONTRACT_CLASS_HASH_V1: &str = "0x01a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003"; /// /// Value to be used as a payload for a message in the `estimate_message_fee` test. diff --git a/unit_tests/tests/test_get_class.rs b/unit_tests/tests/test_get_class.rs index 52bd498..7fb4ec6 100644 --- a/unit_tests/tests/test_get_class.rs +++ b/unit_tests/tests/test_get_class.rs @@ -1,48 +1,43 @@ #![feature(assert_matches)] mod common; +use anyhow::{anyhow, Result}; +use base64::decode; use common::*; -use starknet_core::types::{BlockId, FieldElement, StarknetError}; -use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; +use starknet_core::types::{BlockId, ContractClass, FieldElement, StarknetError}; +use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider, ProviderError}; use std::collections::HashMap; #[rstest] #[tokio::test] -#[ignore = "Fix failing unwrap due to empty constant"] -async fn fail_non_existing_block(clients: HashMap>) { +async fn fail_non_existing_block(clients: HashMap>) -> Result<()> { let deoxys = &clients[DEOXYS]; let test_contract_class_hash = - FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH).expect("Invalid Contract Address"); - - let response_deoxys = deoxys - .get_class(BlockId::Number(1), test_contract_class_hash) - .await; - - assert!( - response_deoxys.is_err(), - "Expected an error, but got a result" - ); - - if let Err(error) = response_deoxys { - let is_correct_error = checking_error_format(&error, StarknetError::BlockNotFound); - - assert!( - is_correct_error, - "Expected BlockNotFound error, but got a different error" - ); + FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH_V0).map_err(|e| anyhow!("Invalid Contract Class Hash: {}", e))?; + let block_id = BlockId::Number(800000); + + match deoxys.get_class(block_id, test_contract_class_hash).await { + Err(e) => { + if checking_error_format(&e, StarknetError::BlockNotFound) { + eprintln!("Error: Block not found for block ID {:?}", block_id); + Ok(()) + } else { + panic!("Unexpected error: {:?}", e); + } + }, + Ok(_) => panic!("Unexpected success: Class was found when it shouldn't be."), } } #[rstest] #[tokio::test] -#[ignore = "Fix failing unwrap due to empty constant"] async fn fail_non_existing_class_hash(clients: HashMap>) { let deoxys = &clients[DEOXYS]; let unknown_contract_class_hash = - FieldElement::from_hex_be("0x4269DEADBEEF").expect("Invalid Contract classh hash"); + FieldElement::from_hex_be("0x4269DEADBEEF").expect("Invalid Contract class hash"); let response_deoxys = deoxys .get_class(BlockId::Number(0), unknown_contract_class_hash) @@ -58,57 +53,66 @@ async fn fail_non_existing_class_hash(clients: HashMap>, ) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; - //TODO: Check this contract class hash to ensure test is valid, must be CAIRO_0 let test_contract_class_hash = - FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH).expect("Invalid Contract Class Hash"); + FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH_V0).expect("Invalid Contract Class Hash"); let deoxys_class = deoxys - .get_class(BlockId::Number(0), test_contract_class_hash) + .get_class(BlockId::Number(50000), test_contract_class_hash) .await .unwrap(); let pathfinder_class = pathfinder - .get_class(BlockId::Number(0), test_contract_class_hash) + .get_class(BlockId::Number(50000), test_contract_class_hash) .await .unwrap(); - assert_eq!(deoxys_class, pathfinder_class); + if let (ContractClass::Legacy(deoxys_legacy), ContractClass::Legacy(pathfinder_legacy)) = (deoxys_class, pathfinder_class) { + assert_eq!(deoxys_legacy.entry_points_by_type, pathfinder_legacy.entry_points_by_type); + assert_eq!(deoxys_legacy.abi, pathfinder_legacy.abi); + + let deoxys_program = decode(&deoxys_legacy.program).expect("Failed to decode base64 program"); + let pathfinder_program = decode(&pathfinder_legacy.program).expect("Failed to decode base64 program"); + + assert_eq!(deoxys_program, pathfinder_program); + } else { + panic!("Contract classes are not of the Legacy variant"); + } } #[rstest] #[tokio::test] -#[ignore = "Fix failing unwrap due to empty constant"] async fn work_ok_retrieving_class_for_contract_version_1( clients: HashMap>, ) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; - //TODO: Check this contract class hash to ensure test is valid, must be CAIRO_1 - let test_contract_class_hash = FieldElement::from_hex_be(CAIRO_1_ACCOUNT_CONTRACT_CLASS_HASH) + let test_contract_class_hash = FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH_V1) .expect("Invalid Contract Class Hash"); let deoxys_class = deoxys - .get_class(BlockId::Number(0), test_contract_class_hash) + .get_class(BlockId::Number(250000), test_contract_class_hash) .await .unwrap(); let pathfinder_class = pathfinder - .get_class(BlockId::Number(0), test_contract_class_hash) + .get_class(BlockId::Number(250000), test_contract_class_hash) .await .unwrap();