Skip to content

Commit

Permalink
Merge pull request #40 from jbcaron/main
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
antiyro authored Jan 30, 2024
2 parents 9a61f6c + ea733b2 commit bcd37e7
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 130 deletions.
124 changes: 91 additions & 33 deletions unit_tests/tests/test_get_block_transaction_count.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![feature(assert_matches)]

/// TODO test on a block withouth transactions
/// TODO test on a block without transactions
mod common;
use common::*;

use std::{assert_matches::assert_matches, collections::HashMap};

use starknet_core::types::{BlockId, BlockTag, FieldElement, StarknetError};
use starknet_providers::{
jsonrpc::{HttpTransport, JsonRpcClient},
Provider, ProviderError,
};
use std::sync::Arc;
use std::{assert_matches::assert_matches, collections::HashMap};
use unit_tests::constants::DEOXYS;

#[require(spec_version = "0.5.1")]
Expand Down Expand Up @@ -52,14 +52,12 @@ async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTrans
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = BlockId::Number(1);
async fn work_with_block(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
block_number: u64,
) {
let block_number = BlockId::Number(block_number);

let response_deoxys = deoxys
.get_block_transaction_count(block_number)
Expand All @@ -77,7 +75,17 @@ async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTran
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
async fn work_with_block_1(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 1).await;
}

#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_1_hash(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

Expand All @@ -101,36 +109,31 @@ async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTra
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(block_min = 100_000, spec_version = "0.5.1")]
/// block 50066 is one of the biggest blocks in the mainnet
#[require(block_min = 5066, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_num(
clients: HashMap<String, JsonRpcClient<HttpTransport>>,
async fn work_with_block_5066(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = BlockId::Number(100000);

let response_deoxys = deoxys
.get_block_transaction_count(block_number)
.await
.expect("Error waiting for response from Deoxys node");

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

assert_eq!(response_deoxys, response_pathfinder);
work_with_block(deoxys, pathfinder, 1).await;
}

#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_hash(
clients: HashMap<String, JsonRpcClient<HttpTransport>>,
async fn work_with_block_100_000(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 100_000).await;
}

#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_100_000_hash(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

Expand All @@ -153,3 +156,58 @@ async fn work_with_block_one_hundred_thousand_hash(

assert_eq!(response_deoxys, response_pathfinder);
}

#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
#[ignore = "ignore this test"]
async fn work_loop(deoxys: JsonRpcClient<HttpTransport>, pathfinder: JsonRpcClient<HttpTransport>) {
let arc_deoxys = Arc::new(deoxys);
let arc_pathfinder = Arc::new(pathfinder);
let parallels_queries = 10;
let mut diff = false;

for block_group in (0..=100_000).step_by(parallels_queries) {
let mut set = tokio::task::JoinSet::new();
for offset in 0..parallels_queries {
let block_id = (block_group + offset) as u64;
let block = BlockId::Number(block_id);
let clone_deoxys = Arc::clone(&arc_deoxys);
let clone_pathfinder = Arc::clone(&arc_pathfinder);
set.spawn(async move {
let response_deoxys = clone_deoxys
.get_block_transaction_count(block)
.await
.expect("Error waiting for response from Deoxys node");

let response_pathfinder = clone_pathfinder.get_block_transaction_count(block).await;

match response_pathfinder {
Ok(response_pathfinder) => {
if response_deoxys != response_pathfinder {
Err(format!("block {}", block_id))
} else {
Ok(())
}
}
Err(e) => Err(format!("Error pathfinder: {}", e)),
}
});
}
while let Some(result) = set.join_next().await {
match result {
Ok(result) => match result {
Ok(_) => {}
Err(e) => {
println!("{}", e);
diff = true;
}
},
Err(e) => {
panic!("{}", e);
}
}
}
}
assert_eq!(diff, false);
}
120 changes: 120 additions & 0 deletions unit_tests/tests/test_get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod common;
use common::*;
use starknet_core::types::MaybePendingBlockWithTxHashes;

use std::sync::Arc;
use std::{assert_matches::assert_matches, collections::HashMap};

use starknet_core::types::{BlockId, BlockTag, FieldElement, StarknetError};
Expand Down Expand Up @@ -111,3 +112,122 @@ async fn work_pending_block(clients: HashMap<String, JsonRpcClient<HttpTransport

assert_eq!(block_deoxys, block_pathfinder);
}

async fn work_with_block(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
block_number: u64,
) {
let block_number = BlockId::Number(block_number);

let response_deoxys = deoxys
.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");

assert_eq!(response_deoxys, response_pathfinder);
}

/// block 1
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_1(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 1).await;
}

/// block 3800 is the first block with starknet_version in the header
#[require(block_min = 3800, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_3800(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 1).await;
}

/// block 50066 is one of the biggest blocks in the mainnet
#[require(block_min = 5066, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_5066(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 1).await;
}

/// block 1466-2242 mismatch block_hash
#[require(block_min = 1500, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_1500(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
work_with_block(deoxys, pathfinder, 1500).await;
}

#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
#[ignore = "ignore this test"]
async fn work_loop(deoxys: JsonRpcClient<HttpTransport>, pathfinder: JsonRpcClient<HttpTransport>) {
let arc_deoxys = Arc::new(deoxys);
let arc_pathfinder = Arc::new(pathfinder);
let parallels_queries = 10;
let mut diff = false;

for block_group in (0..=100_000).step_by(parallels_queries) {
let mut set = tokio::task::JoinSet::new();
for offset in 0..parallels_queries {
let block_id = (block_group + offset) as u64;
let block = BlockId::Number(block_id);
let clone_deoxys = Arc::clone(&arc_deoxys);
let clone_pathfinder = Arc::clone(&arc_pathfinder);
set.spawn(async move {
let response_deoxys = clone_deoxys
.get_block_with_tx_hashes(block)
.await
.expect("Error waiting for response from Deoxys node");

let response_pathfinder = clone_pathfinder.get_block_with_tx_hashes(block).await;

match response_pathfinder {
Ok(response_pathfinder) => {
if response_deoxys != response_pathfinder {
Err(format!("block {}", block_id))
} else {
Ok(())
}
}
Err(e) => Err(format!("Error pathfinder: {}", e)),
}
});
}
while let Some(result) = set.join_next().await {
match result {
Ok(result) => match result {
Ok(_) => {}
Err(e) => {
println!("{}", e);
diff = true;
}
},
Err(e) => {
panic!("{}", e);
}
}
}
}
assert_eq!(diff, false);
}
Loading

0 comments on commit bcd37e7

Please sign in to comment.