Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : 🆙 Updating Ditto to choose block_number while testing #53

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion run_Ditto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

test_files=($(basename -a $(find unit_tests/tests -type f -name "*.rs" ! -name "common.rs")))

cat art.txt
cat ditto.txt

echo "\n"
echo "\033[31mSelect a test file to run:\033[0m"
Expand All @@ -22,6 +22,17 @@ fi
((choice--))

if [[ $choice -ge 0 && $choice -lt ${#test_files[@]} ]]; then

read -p "Enter the maximum block number (or 0 for the latest): " max_block

if grep -q "pub const MAX_BLOCK" unit_tests/src/constants.rs; then
sed -i "" "/pub const MAX_BLOCK/c\\
pub const MAX_BLOCK: u64 = $max_block;" unit_tests/src/constants.rs
else
echo "pub const MAX_BLOCK: u64 = $max_block;" >> unit_tests/src/constants.rs
fi


cargo test --test "${test_files[$choice]%.*}"
else
echo "Invalid selection."
Expand Down
4 changes: 3 additions & 1 deletion unit_tests/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Constants used throughout unit tests
// Max block number used for testing
pub const MAX_BLOCK: u64 = 5000;

pub const DEOXYS: &str = "deoxys";
pub const PATHFINDER: &str = "pathfinder";
Expand Down Expand Up @@ -154,3 +155,4 @@ pub const ERR_PATHFINDER: &str = "Error waiting for response from Pathfinder cli

pub const SPEC_0_5_1: &str = "0.5.1";
pub const SPEC_0_6_0: &str = "0.6.0";
pub const SPEC_0_7_0: &str = "0.7.0";
10 changes: 10 additions & 0 deletions unit_tests/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use starknet_core::types::BlockId;
use starknet_core::types::StarknetError;
use starknet_providers::ProviderError;
/* Common imports used throughout all unit tests */
Expand Down Expand Up @@ -34,5 +35,14 @@ pub fn checking_error_format(response: &ProviderError, expected_error: StarknetE
}
}

pub fn get_max_block_value() -> BlockId {
let max_block = BlockId::Number((MAX_BLOCK));
if MAX_BLOCK == 0 {
BlockId::Tag(starknet_core::types::BlockTag::Latest)
} else {
max_block
}
}

// TODO : Maybe create a function for each executions call that retrieves
// responses from the 3 differents full nodes and compare releveant fields
1 change: 1 addition & 0 deletions unit_tests/tests/test_block_hash_and_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use starknet_providers::{
///
#[rstest]
#[tokio::test]
#[ignore = "Slash this ignore when Deoxys node is fully synced"]
async fn work_latest_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
Expand Down
1 change: 1 addition & 0 deletions unit_tests/tests/test_block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use starknet_providers::{
///
#[rstest]
#[tokio::test]
#[ignore = "Slash this ignore when Deoxys node is fully synced"]
async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
Expand Down
23 changes: 16 additions & 7 deletions unit_tests/tests/test_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,13 @@ async fn fail_too_many_call_data(clients: HashMap<String, JsonRpcClient<HttpTran
entry_point_selector: get_selector_from_name("name").unwrap(),
calldata: vec![FieldElement::ZERO],
},
BlockId::Tag(BlockTag::Latest),
BlockId::Number(MAX_BLOCK),
)
.await
.err();

println!("{:?}", response_deoxys);

assert!(
response_deoxys.is_some(),
"Expected an error, but got a result"
Expand Down Expand Up @@ -302,7 +304,7 @@ async fn work_correct_call(clients: HashMap<String, JsonRpcClient<HttpTransport>
entry_point_selector: get_selector_from_name("name").unwrap(),
calldata: vec![],
},
BlockId::Tag(BlockTag::Latest),
BlockId::Number(MAX_BLOCK),
)
.await
.expect("Error waiting for response from Deoxys node");
Expand All @@ -314,13 +316,16 @@ async fn work_correct_call(clients: HashMap<String, JsonRpcClient<HttpTransport>
entry_point_selector: get_selector_from_name("name").unwrap(),
calldata: vec![],
},
BlockId::Tag(BlockTag::Latest),
BlockId::Number(MAX_BLOCK),
)
.await
.expect("Error waiting for response from Pathfinder node");

let response_expected = short_string!("Ether");

println!("{:?}", response_deoxys);
println!("{:?}", response_pathfinder);

assert_eq!(response_deoxys, vec![response_expected]);
assert_eq!(response_deoxys, response_pathfinder);
}
Expand All @@ -337,14 +342,16 @@ async fn work_correct_call_with_args(clients: HashMap<String, JsonRpcClient<Http
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = get_max_block_value();

let response_deoxys = deoxys
.call(
FunctionCall {
contract_address: FieldElement::from_hex_be(STARKGATE_ETH_BRIDGE_ADDR).unwrap(),
entry_point_selector: get_selector_from_name("balanceOf").unwrap(),
calldata: vec![FieldElement::from_hex_be(CONTRACT_ADDR).unwrap()],
},
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.expect("Error waiting for response from Deoxys node");
Expand All @@ -356,7 +363,7 @@ async fn work_correct_call_with_args(clients: HashMap<String, JsonRpcClient<Http
entry_point_selector: get_selector_from_name("balanceOf").unwrap(),
calldata: vec![FieldElement::from_hex_be(CONTRACT_ADDR).unwrap()],
},
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.expect("Error waiting for response from Pathfinder node");
Expand All @@ -379,6 +386,8 @@ async fn work_with_multiple_args(clients: HashMap<String, JsonRpcClient<HttpTran
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = get_max_block_value();

let response_deoxys = deoxys
.call(
FunctionCall {
Expand All @@ -389,7 +398,7 @@ async fn work_with_multiple_args(clients: HashMap<String, JsonRpcClient<HttpTran
FieldElement::from_hex_be(STARKGATE_USDC).unwrap(),
],
},
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.expect("Error waiting for response from Deoxys node");
Expand All @@ -404,7 +413,7 @@ async fn work_with_multiple_args(clients: HashMap<String, JsonRpcClient<HttpTran
FieldElement::from_hex_be(STARKGATE_USDC).unwrap(),
],
},
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.expect("Error waiting for response from Deoxys node");
Expand Down
6 changes: 4 additions & 2 deletions unit_tests/tests/test_estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ async fn works_ok(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = get_max_block_value();

let ok_deoxys_invoke = OkTransactionFactory::build(Some(FieldElement::ZERO));
let ok_deoxys_invoke_1 = OkTransactionFactory::build(Some(FieldElement::ONE));
let ok_deoxys_invoke_2 = OkTransactionFactory::build(Some(FieldElement::TWO));
Expand All @@ -89,7 +91,7 @@ async fn works_ok(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
let deoxys_estimates = deoxys
.estimate_fee(
&vec![ok_deoxys_invoke, ok_deoxys_invoke_1, ok_deoxys_invoke_2],
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.unwrap();
Expand All @@ -101,7 +103,7 @@ async fn works_ok(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
ok_pathfinder_invoke_1,
ok_pathfinder_invoke_2,
],
BlockId::Tag(BlockTag::Latest),
block_number,
)
.await
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions unit_tests/tests/test_estimate_message_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ async fn estimate_message_fee_works_ok(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
let block_number = get_max_block_value();

let contract_address = FieldElement::from_hex_be(
"0x073314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82",
);
Expand All @@ -187,12 +189,12 @@ async fn estimate_message_fee_works_ok(
);

let deoxys_message_fee = deoxys
.estimate_message_fee(message_fee_params.clone(), BlockId::Tag(BlockTag::Latest))
.estimate_message_fee(message_fee_params.clone(), block_number)
.await
.unwrap();

let pathfinder_message_fee = pathfinder
.estimate_message_fee(message_fee_params, BlockId::Tag(BlockTag::Latest))
.estimate_message_fee(message_fee_params, block_number)
.await
.unwrap();

Expand Down
4 changes: 4 additions & 0 deletions unit_tests/tests/test_get_block_transaction_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ async fn work_with_block_100_000_hash(clients: HashMap<String, JsonRpcClient<Htt
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

if MAX_BLOCK < 100_000 {
return;
}

let block_hash = BlockId::Hash(
FieldElement::from_hex_be(
"0x4f45f870c79f7656c5d7c3c2c28ca0c2fe7206f22f56ac2183f81de521ab340",
Expand Down
18 changes: 15 additions & 3 deletions unit_tests/tests/test_get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_number = get_max_block_value();

let response_deoxys = deoxys
.get_block_with_tx_hashes(BlockId::Tag(BlockTag::Latest))
.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(BlockId::Tag(BlockTag::Latest))
.get_block_with_tx_hashes(block_number)
.await
.expect("Error waiting for response from Deoxys node");

Expand Down Expand Up @@ -126,7 +128,7 @@ async fn work_with_block(
pathfinder: JsonRpcClient<HttpTransport>,
block_number: u64,
) {
let block_number = BlockId::Number(block_number);
let block_number = get_max_block_value();

let response_deoxys = deoxys
.get_block_with_tx_hashes(block_number)
Expand Down Expand Up @@ -158,6 +160,9 @@ async fn work_with_block_3800(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 3800 {
return;
}
work_with_block(deoxys, pathfinder, 3000).await;
}

Expand All @@ -168,6 +173,9 @@ async fn work_with_block_5066(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 5066 {
return;
}
work_with_block(deoxys, pathfinder, 5066).await;
}

Expand All @@ -178,6 +186,9 @@ async fn work_with_block_1500(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 1500 {
return;
}
work_with_block(deoxys, pathfinder, 1500).await;
}

Expand Down Expand Up @@ -237,6 +248,7 @@ async fn work_loop(deoxys: JsonRpcClient<HttpTransport>, pathfinder: JsonRpcClie
/// This test may crash because if 2 clients doesnt exactly have the same computation time, the trace will be different
#[rstest]
#[tokio::test]
#[ignore = "Slash this ignore when Deoxys node is fully synced, but it may not works at all bc of computation"]
async fn work_ok_with_pending_block(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
Expand Down
30 changes: 23 additions & 7 deletions unit_tests/tests/test_get_block_with_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,21 @@ async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTrans
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

let block_tag = BlockId::Tag(BlockTag::Latest);
let block_number = get_max_block_value();
if block_number != BlockId::Tag(BlockTag::Latest) {
return;
}

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

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

//println!("✅ {:?}", response_deoxys);
//println!("✅ {:?}", response_pathfinder);
assert_eq!(response_deoxys, response_pathfinder);
}

Expand All @@ -69,7 +70,7 @@ async fn work_with_block(
pathfinder: JsonRpcClient<HttpTransport>,
block_number: u64,
) {
let block_number = BlockId::Number(block_number);
let block_number = get_max_block_value();

let response_deoxys = deoxys
.get_block_with_txs(block_number)
Expand Down Expand Up @@ -125,6 +126,9 @@ async fn work_with_block_100_000(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 100_000 {
return;
}
work_with_block(deoxys, pathfinder, 100_000).await;
}

Expand All @@ -133,6 +137,9 @@ async fn work_with_block_100_000(
async fn work_with_block_one_hundred_thousand_hash(
clients: HashMap<String, JsonRpcClient<HttpTransport>>,
) {
if MAX_BLOCK < 100_000 {
return;
}
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];

Expand Down Expand Up @@ -163,16 +170,22 @@ async fn work_with_block_3800(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 3800 {
return;
}
work_with_block(deoxys, pathfinder, 3800).await;
}

/// block 50066 is one of the biggest blocks in the mainnet
/// block 5066 is one of the biggest blocks in the mainnet
#[rstest]
#[tokio::test]
async fn work_with_block_5066(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 5066 {
return;
}
work_with_block(deoxys, pathfinder, 5066).await;
}
/// block 1466-2242 mismatch block_hash
Expand All @@ -182,6 +195,9 @@ async fn work_with_block_1500(
deoxys: JsonRpcClient<HttpTransport>,
pathfinder: JsonRpcClient<HttpTransport>,
) {
if MAX_BLOCK < 1500 {
return;
}
work_with_block(deoxys, pathfinder, 1500).await;
}

Expand Down
Loading
Loading