Skip to content

Commit

Permalink
Merge pull request #62 from KasarLabs/fix/get_class
Browse files Browse the repository at this point in the history
Fix/get class
  • Loading branch information
antiyro authored Jun 17, 2024
2 parents 899468c + 2ee9c4c commit a2e0a92
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
1 change: 1 addition & 0 deletions unit_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion unit_tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 40 additions & 36 deletions unit_tests/tests/test_get_class.rs
Original file line number Diff line number Diff line change
@@ -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<String, JsonRpcClient<HttpTransport>>) {
async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) -> 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<String, JsonRpcClient<HttpTransport>>) {
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)
Expand All @@ -58,57 +53,66 @@ async fn fail_non_existing_class_hash(clients: HashMap<String, JsonRpcClient<Htt

assert!(
is_correct_error,
"Expected ClassHashNotFound error, but got a different error"
"Expected ClassHashNotFound error, but got a different error: {:?}",
error
);
} else {
panic!("Unexpected success: Class hash was found when it shouldn't be.");
}
}

#[rstest]
#[tokio::test]
#[ignore = "Fix failing unwrap due to empty constant"]
async fn work_ok_retrieving_class_for_contract_version_0(
clients: HashMap<String, JsonRpcClient<HttpTransport>>,
) {
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<String, JsonRpcClient<HttpTransport>>,
) {
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();

Expand Down

0 comments on commit a2e0a92

Please sign in to comment.