Skip to content

Commit

Permalink
refacto consts
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro committed Jun 17, 2024
2 parents 74e51d1 + a0462ab commit 180e443
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 37 deletions.
12 changes: 6 additions & 6 deletions unit_tests/src/constants/sepolia/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,31 @@ 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.
///
/// 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
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/tests/test_get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
let deoxys = &clients[mainnet::network::DEOXYS];
let pathfinder = &clients[mainnet::network::PATHFINDER];

let block_number = BlockId::Number(193);
let block_number = BlockId::Number(100000);

let response_deoxys = deoxys
.get_block_with_tx_hashes(block_number)
Expand Down
203 changes: 193 additions & 10 deletions unit_tests/tests/test_get_block_with_txs.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,86 @@
#![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},
Provider,
};
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
}

#[rstest]
#[tokio::test]
async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -62,9 +129,34 @@ async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTrans
.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()
);
}
}
}


async fn work_with_block(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
Expand All @@ -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]
Expand Down Expand Up @@ -115,7 +231,31 @@ async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTra
.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]
Expand All @@ -130,7 +270,9 @@ async fn work_with_block_100_000(
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_hash(
clients: HashMap<String, JsonRpcClient<HttpTransport>>,
clients

: HashMap<String, JsonRpcClient<HttpTransport>>,
) {
let deoxys = &clients[mainnet::network::DEOXYS];
let pathfinder = &clients[mainnet::network::PATHFINDER];
Expand All @@ -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
Expand Down Expand Up @@ -210,7 +376,24 @@ 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 All @@ -235,4 +418,4 @@ async fn work_loop(deoxys: JsonRpcClient<HttpTransport>, pathfinder: JsonRpcClie
}
}
assert_eq!(diff, false);
}
}
6 changes: 3 additions & 3 deletions unit_tests/tests/test_get_class_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
let response_deoxys = deoxys
.get_class_at(
BlockId::Hash(FieldElement::ZERO),
FieldElement::from_hex_be(CONTRACT_ACCOUNT).unwrap(),
FieldElement::from_hex_be(CONTRACT_ACCOUNT_CAIRO_ZERO).unwrap(),
)
.await;

Expand Down Expand Up @@ -148,15 +148,15 @@ async fn work_contract_v1(
let response_deoxys = deoxys
.get_class_at(
block_number,
FieldElement::from_hex_be(CONTRACT_ACCOUNT).unwrap(),
FieldElement::from_hex_be(CONTRACT_ACCOUNT_CAIRO_ZERO).unwrap(),
)
.await
.expect("Error waiting for response from Deoxys client");

let response_pathfinder = pathfinder
.get_class_at(
block_number,
FieldElement::from_hex_be(CONTRACT_ACCOUNT).unwrap(),
FieldElement::from_hex_be(CONTRACT_ACCOUNT_CAIRO_ZERO).unwrap(),
)
.await
.expect("Error waiting for response from Pathfinder client");
Expand Down
Loading

0 comments on commit 180e443

Please sign in to comment.