From 942cfbfa8280bfb80cd67b199361f9f4bcc5a587 Mon Sep 17 00:00:00 2001 From: antiyro Date: Fri, 24 May 2024 09:38:01 +0200 Subject: [PATCH 1/2] cleaned get block with txs --- unit_tests/tests/common.rs | 2 +- .../tests/test_block_hash_and_number.rs | 76 +++++-- unit_tests/tests/test_block_number.rs | 21 +- .../tests/test_get_block_transaction_count.rs | 6 +- .../tests/test_get_block_with_tx_hashes.rs | 79 ++++--- unit_tests/tests/test_get_block_with_txs.rs | 203 +++++++++++++++++- unit_tests/tests/test_spec_version.rs | 12 +- unit_tests/tests/test_syncing.rs | 28 ++- 8 files changed, 356 insertions(+), 71 deletions(-) diff --git a/unit_tests/tests/common.rs b/unit_tests/tests/common.rs index 8c90218..ebed895 100644 --- a/unit_tests/tests/common.rs +++ b/unit_tests/tests/common.rs @@ -50,4 +50,4 @@ pub fn get_block_setting() -> BlockId { Err(_) => BlockId::Number(100000), }, } -} \ No newline at end of file +} diff --git a/unit_tests/tests/test_block_hash_and_number.rs b/unit_tests/tests/test_block_hash_and_number.rs index cd91544..591634d 100644 --- a/unit_tests/tests/test_block_hash_and_number.rs +++ b/unit_tests/tests/test_block_hash_and_number.rs @@ -41,25 +41,55 @@ async fn work_existing_block(clients: HashMap 0); assert!(juno_responce.block_number > 0); - if !check_block_number(deoxys_responce.clone(), pathfinder_responce.clone(), juno_responce.clone()) { - println!("{}", "\nMismatch on Block numbers are skipped since it may not be an error.".green().bold()); + if !check_block_number( + deoxys_responce.clone(), + pathfinder_responce.clone(), + juno_responce.clone(), + ) { + println!( + "{}", + "\nMismatch on Block numbers are skipped since it may not be an error." + .green() + .bold() + ); } - + if !check_block_hashes(deoxys_responce, pathfinder_responce, juno_responce) { - println!("{}", "\nMismatch on Block hashes are skipped since it may not be an error.".green().bold()); + println!( + "{}", + "\nMismatch on Block hashes are skipped since it may not be an error." + .green() + .bold() + ); } } -fn check_block_number(responce_deoxys: BlockHashAndNumber, responce_pathfinder: BlockHashAndNumber, responce_juno: BlockHashAndNumber) -> bool { +fn check_block_number( + responce_deoxys: BlockHashAndNumber, + responce_pathfinder: BlockHashAndNumber, + responce_juno: BlockHashAndNumber, +) -> bool { let deoxys_block_number = responce_deoxys.block_number; let pathfinder_block_number = responce_pathfinder.block_number; let juno_block_number = responce_juno.block_number; - if deoxys_block_number != pathfinder_block_number || pathfinder_block_number != juno_block_number || juno_block_number != deoxys_block_number { + if deoxys_block_number != pathfinder_block_number + || pathfinder_block_number != juno_block_number + || juno_block_number != deoxys_block_number + { println!("{}", "Block number mismatch detected\n".red().bold()); - println!("Deoxys: {}", format!("{}", deoxys_block_number).cyan().bold()); - println!("Pathfinder: {}", format!("{}", pathfinder_block_number).magenta().bold()); - println!("Juno: {}\n", format!("{}", juno_block_number).green().bold()); + println!( + "Deoxys: {}", + format!("{}", deoxys_block_number).cyan().bold() + ); + println!( + "Pathfinder: {}", + format!("{}", pathfinder_block_number).magenta().bold() + ); + println!( + "Juno: {}\n", + format!("{}", juno_block_number).green().bold() + ); if deoxys_block_number != pathfinder_block_number { println!( @@ -91,19 +121,34 @@ fn check_block_number(responce_deoxys: BlockHashAndNumber, responce_pathfinder: println!("{}", "All nodes have matching block numbers".green().bold()); return true; } - } -fn check_block_hashes(responce_deoxys: BlockHashAndNumber, responce_pathfinder: BlockHashAndNumber, responce_juno: BlockHashAndNumber) -> bool { +fn check_block_hashes( + responce_deoxys: BlockHashAndNumber, + responce_pathfinder: BlockHashAndNumber, + responce_juno: BlockHashAndNumber, +) -> bool { let deoxys_block_hash = responce_deoxys.block_hash; let pathfinder_block_hash = responce_pathfinder.block_hash; let juno_block_hash = responce_juno.block_hash; - if deoxys_block_hash != pathfinder_block_hash || pathfinder_block_hash != juno_block_hash || juno_block_hash != deoxys_block_hash { + if deoxys_block_hash != pathfinder_block_hash + || pathfinder_block_hash != juno_block_hash + || juno_block_hash != deoxys_block_hash + { println!("{}", "Block hash mismatch detected\n".red().bold()); - println!("Deoxys: {}", format!("0x{:x}", deoxys_block_hash).cyan().bold()); - println!("Pathfinder: {}", format!("0x{:x}", pathfinder_block_hash).magenta().bold()); - println!("Juno: {}\n", format!("0x{:x}", juno_block_hash).green().bold()); + println!( + "Deoxys: {}", + format!("0x{:x}", deoxys_block_hash).cyan().bold() + ); + println!( + "Pathfinder: {}", + format!("0x{:x}", pathfinder_block_hash).magenta().bold() + ); + println!( + "Juno: {}\n", + format!("0x{:x}", juno_block_hash).green().bold() + ); if deoxys_block_hash != pathfinder_block_hash { println!( @@ -135,5 +180,4 @@ fn check_block_hashes(responce_deoxys: BlockHashAndNumber, responce_pathfinder: println!("{}", "All nodes have matching block hashes".green().bold()); return true; } - } diff --git a/unit_tests/tests/test_block_number.rs b/unit_tests/tests/test_block_number.rs index 54977e0..92f671b 100644 --- a/unit_tests/tests/test_block_number.rs +++ b/unit_tests/tests/test_block_number.rs @@ -3,11 +3,11 @@ use common::*; use std::collections::HashMap; +use colored::*; use starknet_providers::{ jsonrpc::{HttpTransport, JsonRpcClient}, Provider, -}; -use colored::*; // Add this import for colored output +}; // Add this import for colored output /// /// Unit test for `starknet_blockNumber` @@ -41,11 +41,17 @@ async fn work_existing_block(clients: HashMap, pathfinder: JsonRpcClient, - juno: JsonRpcClient + juno: JsonRpcClient, ) { work_with_block(deoxys, pathfinder, juno, 1).await; } @@ -147,7 +147,7 @@ async fn work_with_block_1_hash(clients: HashMap, pathfinder: JsonRpcClient, - juno: JsonRpcClient + juno: JsonRpcClient, ) { work_with_block(deoxys, pathfinder, juno, 1).await; } @@ -157,7 +157,7 @@ async fn work_with_block_5066( async fn work_with_block_100_000( deoxys: JsonRpcClient, pathfinder: JsonRpcClient, - juno: JsonRpcClient + juno: JsonRpcClient, ) { work_with_block(deoxys, pathfinder, juno, 100_000).await; } diff --git a/unit_tests/tests/test_get_block_with_tx_hashes.rs b/unit_tests/tests/test_get_block_with_tx_hashes.rs index f316810..5b40e2b 100644 --- a/unit_tests/tests/test_get_block_with_tx_hashes.rs +++ b/unit_tests/tests/test_get_block_with_tx_hashes.rs @@ -1,19 +1,19 @@ #![feature(assert_matches)] mod common; -use common::*; use colored::*; +use common::*; use starknet_core::types::MaybePendingBlockWithTxHashes; use std::collections::HashMap; use std::sync::Arc; +use serde_json::Value; use starknet_core::types::{BlockId, BlockTag, FieldElement, StarknetError}; use starknet_providers::{ jsonrpc::{HttpTransport, JsonRpcClient}, Provider, }; -use serde_json::Value; use unit_tests::constants::DEOXYS; // Define a recursive function to compare JSON values and print differences @@ -22,7 +22,11 @@ fn compare_json_values(path: &str, value1: &Value, value2: &Value) -> bool { match (value1, value2) { (Value::Object(map1), Value::Object(map2)) => { - for key in map1.keys().chain(map2.keys()).collect::>() { + for key in map1 + .keys() + .chain(map2.keys()) + .collect::>() + { let new_path = format!("{}/{}", path, key); match (map1.get(key), map2.get(key)) { (Some(v1), Some(v2)) => { @@ -30,8 +34,12 @@ fn compare_json_values(path: &str, value1: &Value, value2: &Value) -> bool { exception_found = true; } } - (Some(v1), None) => println!("{}: present in first, absent in second", new_path), - (None, Some(v2)) => println!("{}: absent in first, present in second", new_path), + (Some(v1), None) => { + println!("{}: present in first, absent in second", new_path) + } + (None, Some(v2)) => { + println!("{}: absent in first, present in second", new_path) + } (None, None) => unreachable!(), } } @@ -57,11 +65,14 @@ fn compare_json_values(path: &str, value1: &Value, value2: &Value) -> bool { if value1 != value2 { let exception_paths = [ "/l1_data_gas_price/price_in_fri", - "/l1_data_gas_price/price_in_wei" + "/l1_data_gas_price/price_in_wei", ]; if exception_paths.contains(&path) { - println!("{} - Bypassed as exception", format!("{}: {:?} != {:?}", path, value1, value2).yellow()); + println!( + "{} - Bypassed as exception", + format!("{}: {:?} != {:?}", path, value1, value2).yellow() + ); exception_found = true; } else { println!("{}: {:?} != {:?}", path, value1, value2); @@ -118,7 +129,7 @@ async fn work_existing_block(clients: HashMap, pathfinder: JsonRpcClie // Convert the blocks to JSON values let block_deoxys_json: Value = serde_json::to_value(&response_deoxys) .expect("Failed to convert deoxys block to JSON"); - let block_pathfinder_json: Value = serde_json::to_value(&response_pathfinder) - .expect("Failed to convert pathfinder block to JSON"); + let block_pathfinder_json: Value = + serde_json::to_value(&response_pathfinder) + .expect("Failed to convert pathfinder block to JSON"); // Compare the JSON values and print differences println!("Blocks for block {} do not match. Differences:", block_id); - let exception_found = compare_json_values( - "", - &block_deoxys_json, - &block_pathfinder_json, - ); + let exception_found = + compare_json_values("", &block_deoxys_json, &block_pathfinder_json); if !exception_found { Err(format!("block {}", block_id)) diff --git a/unit_tests/tests/test_get_block_with_txs.rs b/unit_tests/tests/test_get_block_with_txs.rs index 7711835..89f7541 100644 --- a/unit_tests/tests/test_get_block_with_txs.rs +++ b/unit_tests/tests/test_get_block_with_txs.rs @@ -1,12 +1,11 @@ #![feature(assert_matches)] -/// TODO test on a block withouth transactions mod common; +use colored::*; use common::*; +use serde_json::Value; use std::sync::Arc; - use std::collections::HashMap; - use starknet_core::types::{BlockId, BlockTag, FieldElement, StarknetError}; use starknet_providers::{ jsonrpc::{HttpTransport, JsonRpcClient}, @@ -14,6 +13,74 @@ use starknet_providers::{ }; use unit_tests::constants::DEOXYS; +// Define a recursive function to compare JSON values and print differences +fn compare_json_values(path: &str, value1: &Value, value2: &Value) -> bool { + let mut exception_found = false; + + match (value1, value2) { + (Value::Object(map1), Value::Object(map2)) => { + for key in map1 + .keys() + .chain(map2.keys()) + .collect::>() + { + let new_path = format!("{}/{}", path, key); + match (map1.get(key), map2.get(key)) { + (Some(v1), Some(v2)) => { + if compare_json_values(&new_path, v1, v2) { + exception_found = true; + } + } + (Some(v1), None) => { + println!("{}: present in first, absent in second", new_path) + } + (None, Some(v2)) => { + println!("{}: absent in first, present in second", new_path) + } + (None, None) => unreachable!(), + } + } + } + (Value::Array(arr1), Value::Array(arr2)) => { + for (index, (item1, item2)) in arr1.iter().zip(arr2.iter()).enumerate() { + let new_path = format!("{}[{}]", path, index); + if compare_json_values(&new_path, item1, item2) { + exception_found = true; + } + } + if arr1.len() > arr2.len() { + for index in arr2.len()..arr1.len() { + println!("{}[{}]: present in first, absent in second", path, index); + } + } else if arr2.len() > arr1.len() { + for index in arr1.len()..arr2.len() { + println!("{}[{}]: absent in first, present in second", path, index); + } + } + } + _ => { + if value1 != value2 { + let exception_paths = [ + "/l1_data_gas_price/price_in_fri", + "/l1_data_gas_price/price_in_wei", + ]; + + if exception_paths.contains(&path) { + println!( + "{} - Bypassed as exception", + format!("{}: {:?} != {:?}", path, value1, value2).yellow() + ); + exception_found = true; + } else { + println!("{}: {:?} != {:?}", path, value1, value2); + } + } + } + } + + exception_found +} + #[rstest] #[tokio::test] async fn fail_non_existing_block(clients: HashMap>) { @@ -62,9 +129,34 @@ async fn work_with_latest_block(clients: HashMap, pathfinder: JsonRpcClient, @@ -80,7 +172,31 @@ async fn work_with_block( .await .expect("Error waiting for response from Pathfinder node"); - assert_eq!(response_deoxys, response_pathfinder); + // Convert the blocks to JSON values + let block_deoxys_json: Value = + serde_json::to_value(&response_deoxys).expect("Failed to convert deoxys block to JSON"); + let block_pathfinder_json: Value = serde_json::to_value(&response_pathfinder) + .expect("Failed to convert pathfinder block to JSON"); + + // Compare the JSON values and print differences if they don't match + if block_deoxys_json != block_pathfinder_json { + println!( + "{}", + format!("Block does not match differences found\n") + .red() + .bold() + ); + let exception_found = compare_json_values("", &block_deoxys_json, &block_pathfinder_json); + + if !exception_found { + panic!("Blocks do not match"); + } else { + println!( + "\nMismatch skipped: {}", + format!("field exception found").green().bold() + ); + } + } } #[rstest] @@ -115,7 +231,31 @@ async fn work_with_block_one_hash(clients: HashMap>, + clients + +: HashMap>, ) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; @@ -152,7 +294,31 @@ async fn work_with_block_one_hundred_thousand_hash( .await .expect("Error waiting for response from Pathfinder node"); - assert_eq!(response_deoxys, response_pathfinder); + // Convert the blocks to JSON values + let block_deoxys_json: Value = + serde_json::to_value(&response_deoxys).expect("Failed to convert deoxys block to JSON"); + let block_pathfinder_json: Value = serde_json::to_value(&response_pathfinder) + .expect("Failed to convert pathfinder block to JSON"); + + // Compare the JSON values and print differences if they don't match + if block_deoxys_json != block_pathfinder_json { + println!( + "{}", + format!("Block does not match differences found\n") + .red() + .bold() + ); + let exception_found = compare_json_values("", &block_deoxys_json, &block_pathfinder_json); + + if !exception_found { + panic!("Blocks do not match"); + } else { + println!( + "\nMismatch skipped: {}", + format!("field exception found").green().bold() + ); + } + } } /// block 3800 is the first block with starknet_version in the header @@ -210,7 +376,24 @@ async fn work_loop(deoxys: JsonRpcClient, pathfinder: JsonRpcClie match response_pathfinder { Ok(response_pathfinder) => { if response_deoxys != response_pathfinder { - Err(format!("block {}", block_id)) + // Convert the blocks to JSON values + let block_deoxys_json: Value = serde_json::to_value(&response_deoxys) + .expect("Failed to convert deoxys block to JSON"); + let block_pathfinder_json: Value = + serde_json::to_value(&response_pathfinder) + .expect("Failed to convert pathfinder block to JSON"); + + // Compare the JSON values and print differences + println!("Blocks for block {} do not match. Differences:", block_id); + let exception_found = + compare_json_values("", &block_deoxys_json, &block_pathfinder_json); + + if !exception_found { + Err(format!("block {}", block_id)) + } else { + println!("{}", "Test passed with exceptions".green()); + Ok(()) + } } else { Ok(()) } @@ -235,4 +418,4 @@ async fn work_loop(deoxys: JsonRpcClient, pathfinder: JsonRpcClie } } assert_eq!(diff, false); -} +} \ No newline at end of file diff --git a/unit_tests/tests/test_spec_version.rs b/unit_tests/tests/test_spec_version.rs index cb07e43..cf0f603 100644 --- a/unit_tests/tests/test_spec_version.rs +++ b/unit_tests/tests/test_spec_version.rs @@ -3,9 +3,9 @@ mod common; use std::collections::HashMap; +use colored::*; use common::*; use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; -use colored::*; /// /// Unit test for `starknet_specversion` @@ -34,8 +34,14 @@ async fn test_spec_version_7_1(clients: HashMap { - println!("Mismatch skipped: {}", format!("Node {} is not syncing.", name2).green().bold()); - }, + println!( + "Mismatch skipped: {}", + format!("Node {} is not syncing.", name2).green().bold() + ); + } (SyncStatusType::NotSyncing, SyncStatusType::Syncing(_)) => { - println!("Mismatch skipped: {}", format!("Node {} is not syncing.", name1).green().bold()); - }, + println!( + "Mismatch skipped: {}", + format!("Node {} is not syncing.", name1).green().bold() + ); + } _ => { - panic!("Mismatch detected: {}", "starknet_syncing mismatch".red().bold()); + panic!( + "Mismatch detected: {}", + "starknet_syncing mismatch".red().bold() + ); } } } From b432cf9cfb357ed039e6f12e105e96d6aa040d1a Mon Sep 17 00:00:00 2001 From: antiyro Date: Sat, 25 May 2024 10:50:56 +0200 Subject: [PATCH 2/2] cleaned getNonce call --- unit_tests/src/constants.rs | 14 +++++++------- unit_tests/tests/test_get_class.rs | 2 +- unit_tests/tests/test_get_class_at.rs | 6 +++--- unit_tests/tests/test_get_nonce.rs | 18 ++++++------------ .../tests/test_get_transaction_by_hash.rs | 2 +- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/unit_tests/src/constants.rs b/unit_tests/src/constants.rs index 6f03bc2..61dc376 100644 --- a/unit_tests/src/constants.rs +++ b/unit_tests/src/constants.rs @@ -56,7 +56,7 @@ pub const STARKGATE_ETHER: &str = /// Details concerning this contract can be found on [StarkScan](https://starkscan.co/contract/0x07fa9a8eacb89fb6cd0c7fe557e71c42a4b181ba328a9a04958136e6469c4e00) /// pub const CONTRACT_ERC721: &str = - "0x07fa9a8eacb89fb6cd0c7fe557e71c42a4b181ba328a9a04958136e6469c4e00"; + "0x05dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af"; /// /// Random ERC20 Starknet contract address. @@ -64,23 +64,23 @@ pub const CONTRACT_ERC721: &str = /// Details concerning this contract can be found on [StarkScan](https://starkscan.co/contract/0x04a5fdce70877b77f03aea8a29259176f88d5bea9d0ad8c0118f5316425e6ba0) /// pub const CONTRACT_ERC20: &str = - "0x04a5fdce70877b77f03aea8a29259176f88d5bea9d0ad8c0118f5316425e6ba0"; + "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; /// /// Random ACCOUNT Starknet contract address. /// /// Details concerning this contract can be found on [StarkScan](https://starkscan.co/contract/0x05e1eee30e79b4f592f444132526b2e2c7f505698e888c659e5f5def5a458c1a) /// -pub const CONTRACT_ACCOUNT: &str = - "0x05e1eee30e79b4f592f444132526b2e2c7f505698e888c659e5f5def5a458c1a"; +pub const CONTRACT_ACCOUNT_CAIRO_ZERO: &str = + "0x0100104681e789e4211fba898262e1d6f6191bee797ce38a9e07fe8dec743574"; /// /// Random PROXY ACCOUNT Starknet contract address. /// /// Details concerning this contract can be found on [StarkScan](https://starkscan.co/contract/0x05d7f4d55795b56ceb4dd93febe17954c7cfd5e15d7a79cb0cec067a713ac159) /// -pub const CONTRACT_ACCOUNT_PROXY: &str = - "0x05d7f4d55795b56ceb4dd93febe17954c7cfd5e15d7a79cb0cec067a713ac159"; +pub const CONTRACT_ACCOUNT_PROXY_CAIRO_ZERO: &str = + "0x036Ef6410243034cE315eC19E7ABFEB41052f39095EE3D5AFcC20D672b1C725F"; /// /// Random legacy account using Cairo v0 @@ -142,7 +142,7 @@ 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 = ""; +pub const TEST_CONTRACT_CLASS_HASH: &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 0ecc89c..52bd498 100644 --- a/unit_tests/tests/test_get_class.rs +++ b/unit_tests/tests/test_get_class.rs @@ -17,7 +17,7 @@ async fn fail_non_existing_block(clients: HashMap>) { let deoxys = &clients[DEOXYS]; - //This contract was created at Block 500198, so need to be synced to this minimum block - let block_number = get_block_setting(); let response_deoxys = deoxys @@ -156,14 +154,12 @@ async fn work_account_contract(clients: HashMap>) { +async fn work_declare_transaction(clients: HashMap>) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER];