From 7239f32d28bc0af418af5129a57a6e35c1cdff85 Mon Sep 17 00:00:00 2001 From: antiyro Date: Thu, 23 May 2024 17:51:27 +0200 Subject: [PATCH] added block_hash_and_number tests --- .../tests/test_block_hash_and_number.rs | 122 ++++++++++++++++-- ...st_specversion.rs => test_spec_version.rs} | 2 +- 2 files changed, 112 insertions(+), 12 deletions(-) rename unit_tests/tests/{test_specversion.rs => test_spec_version.rs} (92%) diff --git a/unit_tests/tests/test_block_hash_and_number.rs b/unit_tests/tests/test_block_hash_and_number.rs index a3f90c7..cd91544 100644 --- a/unit_tests/tests/test_block_hash_and_number.rs +++ b/unit_tests/tests/test_block_hash_and_number.rs @@ -1,5 +1,6 @@ mod common; use common::*; +use starknet_core::types::BlockHashAndNumber; use std::collections::HashMap; @@ -8,6 +9,8 @@ use starknet_providers::{ Provider, }; +use colored::*; + /// /// Unit test for `starknet_BlockHashAndNumber` /// @@ -16,24 +19,121 @@ use starknet_providers::{ /// #[rstest] #[tokio::test] -#[ignore = "Slash this ignore when Deoxys node is fully synced"] -async fn work_latest_block(clients: HashMap>) { +async fn work_existing_block(clients: HashMap>) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; + let juno = &clients[JUNO]; - let response_deoxys = deoxys + let deoxys_responce = deoxys .block_hash_and_number() .await - .expect("Error waiting for response from Deoxys node"); - let response_pathfinder = pathfinder + .expect("Deoxys : Error while getting the block number"); + let pathfinder_responce = pathfinder .block_hash_and_number() .await - .expect("Error waiting for response from Deoxys node"); - let response_expected = deoxys - .block_number() + .expect("RPC : Error while getting the block number"); + let juno_responce = juno + .block_hash_and_number() .await - .expect("Error waiting for response from Deoxys node"); + .expect("Juno : Error while getting the block number"); + + assert!(deoxys_responce.block_number > 0); + assert!(pathfinder_responce.block_number > 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_hashes(deoxys_responce, pathfinder_responce, juno_responce) { + 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 { + 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 { + 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()); + + if deoxys_block_number != pathfinder_block_number { + println!( + "{} {} != {}", + "Mismatch between Deoxys and Pathfinder:".red(), + deoxys_block_number.to_string().yellow().bold(), + pathfinder_block_number.to_string().yellow().bold() + ); + } + if pathfinder_block_number != juno_block_number { + println!( + "{} {} != {}", + "Mismatch between Pathfinder and Juno:".red(), + pathfinder_block_number.to_string().yellow().bold(), + juno_block_number.to_string().yellow().bold() + ); + } + if juno_block_number != deoxys_block_number { + println!( + "{} {} != {}", + "Mismatch between Juno and Deoxys:".red(), + juno_block_number.to_string().yellow().bold(), + deoxys_block_number.to_string().yellow().bold() + ); + } + + return false; + } else { + 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 { + 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 { + 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()); + + if deoxys_block_hash != pathfinder_block_hash { + println!( + "{} {} != {}", + "Mismatch between Deoxys and Pathfinder:".red(), + format!("0x{:x}", deoxys_block_hash).yellow().bold(), + format!("0x{:x}", pathfinder_block_hash).yellow().bold() + ); + } + if pathfinder_block_hash != juno_block_hash { + println!( + "{} {} != {}", + "Mismatch between Pathfinder and Juno:".red(), + format!("0x{:x}", pathfinder_block_hash).yellow().bold(), + format!("0x{:x}", juno_block_hash).yellow().bold() + ); + } + if juno_block_hash != deoxys_block_hash { + println!( + "{} {} != {}", + "Mismatch between Juno and Deoxys:".red(), + format!("0x{:x}", juno_block_hash).yellow().bold(), + format!("0x{:x}", deoxys_block_hash).yellow().bold() + ); + } + + return false; + } else { + println!("{}", "All nodes have matching block hashes".green().bold()); + return true; + } - assert_eq!(response_deoxys.block_number, response_expected); - assert_eq!(response_deoxys, response_pathfinder); } diff --git a/unit_tests/tests/test_specversion.rs b/unit_tests/tests/test_spec_version.rs similarity index 92% rename from unit_tests/tests/test_specversion.rs rename to unit_tests/tests/test_spec_version.rs index 24007a2..cb07e43 100644 --- a/unit_tests/tests/test_specversion.rs +++ b/unit_tests/tests/test_spec_version.rs @@ -37,5 +37,5 @@ async fn test_spec_version_7_1(clients: HashMap