Skip to content

Commit

Permalink
cleaned get block with tx hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro committed May 23, 2024
1 parent f7227fe commit 7d84a00
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 8 deletions.
132 changes: 125 additions & 7 deletions unit_tests/tests/test_get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod common;
use common::*;
use colored::*;
use starknet_core::types::MaybePendingBlockWithTxHashes;

use std::collections::HashMap;
Expand All @@ -12,8 +13,66 @@ 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
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::<std::collections::HashSet<_>>() {
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
}

///
/// Unit test for `starknet_get_block_with_tx_hashes`
///
Expand Down Expand Up @@ -59,7 +118,7 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = BlockId::Number(193); //2243
let block_number = BlockId::Number(193);

let response_deoxys = deoxys
.get_block_with_tx_hashes(block_number)
Expand All @@ -68,7 +127,7 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
let response_pathfinder = pathfinder
.get_block_with_tx_hashes(block_number)
.await
.expect("Error waiting for response from Deoxys node");
.expect("Error waiting for Pathfinder node");

let block_deoxys = match response_deoxys {
MaybePendingBlockWithTxHashes::Block(block) => block,
Expand All @@ -83,7 +142,21 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
}
};

assert_eq!(block_deoxys, block_pathfinder);
// Convert the blocks to JSON values
let block_deoxys_json: Value = serde_json::to_value(&block_deoxys).expect("Failed to convert deoxys block to JSON");
let block_pathfinder_json: Value = serde_json::to_value(&block_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());
}
}
}

///
Expand Down Expand Up @@ -134,13 +207,39 @@ async fn work_with_block(
.get_block_with_tx_hashes(block_number)
.await
.expect("Error waiting for response from Deoxys node");

let response_pathfinder = pathfinder
.get_block_with_tx_hashes(block_number)
.await
.expect("Error waiting for response from Pathfinder node");
.expect("Error waiting for Pathfinder node");

assert_eq!(response_deoxys, response_pathfinder);
let block_deoxys = match response_deoxys {
MaybePendingBlockWithTxHashes::Block(block) => block,
MaybePendingBlockWithTxHashes::PendingBlock(_) => {
panic!("Expected block, got pending block")
}
};
let block_pathfinder = match response_pathfinder {
MaybePendingBlockWithTxHashes::Block(block) => block,
MaybePendingBlockWithTxHashes::PendingBlock(_) => {
panic!("Expected block, got pending block")
}
};

// Convert the blocks to JSON values
let block_deoxys_json: Value = serde_json::to_value(&block_deoxys).expect("Failed to convert deoxys block to JSON");
let block_pathfinder_json: Value = serde_json::to_value(&block_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 1
Expand Down Expand Up @@ -209,7 +308,26 @@ async fn work_loop(deoxys: JsonRpcClient<HttpTransport>, 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(())
}
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/tests/test_syncing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn assert_sync_status(a: SyncStatusType, b: SyncStatusType, c: SyncStatusType) {
println!("Mismatch skipped: {}", format!("Node {} is not syncing.", name1).green().bold());
},
_ => {
panic!("Mismatch skipped: {}", "starknet_syncing mismatch detected".red().bold());
panic!("Mismatch detected: {}", "starknet_syncing mismatch".red().bold());
}
}
}
Expand Down

0 comments on commit 7d84a00

Please sign in to comment.