diff --git a/Cargo.lock b/Cargo.lock index af017f8..40495b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1227,6 +1227,12 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.152" @@ -1269,6 +1275,7 @@ name = "macro_utils" version = "0.1.0" dependencies = [ "anyhow", + "lazy_static", "quote", "serde", "serde_json", diff --git a/macro/src/lib.rs b/macro/src/lib.rs index e8ed70f..fc87d23 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -1,4 +1,4 @@ -use macro_utils::{extract_expr_to_str, extract_expr_to_u64, get_rpc_data, RpcData}; +use macro_utils::{extract_expr_to_str, extract_expr_to_u64, RpcData, RPC_DATA}; use proc_macro::TokenStream; use quote::{quote, ToTokens}; use syn::{ @@ -46,14 +46,18 @@ impl Parse for MacroDataRequire { match arg.path.get_ident() { Some(ident) => match ident.to_string().as_str() { "block_min" => { - parsed_params.block_min = extract_expr_to_u64(arg.value).unwrap_or(0); + parsed_params.block_min = match extract_expr_to_str(&arg.value) { + Ok(s) if s == "latest" => RPC_DATA.latest_chain_block, + Ok(_) => 0, + Err(_) => extract_expr_to_u64(&arg.value).unwrap_or(0), + }; } "block_max" => { parsed_params.block_max = - extract_expr_to_u64(arg.value).unwrap_or(u64::MAX); + extract_expr_to_u64(&arg.value).unwrap_or(u64::MAX); } "spec_version" => { - parsed_params.spec_version = match extract_expr_to_str(arg.value) { + parsed_params.spec_version = match extract_expr_to_str(&arg.value) { Ok(s) => Some(s), Err(_) => None, } @@ -87,7 +91,7 @@ impl MacroDataRequire { #[proc_macro_attribute] pub fn require(args: TokenStream, item: TokenStream) -> TokenStream { - let block_data = get_rpc_data(); + let block_data = RPC_DATA.clone(); let macro_data = parse_macro_input!(args as MacroDataRequire); if macro_data.should_ignore(block_data) { diff --git a/macro_utils/Cargo.toml b/macro_utils/Cargo.toml index 2f9a0f7..72af1f9 100644 --- a/macro_utils/Cargo.toml +++ b/macro_utils/Cargo.toml @@ -16,3 +16,4 @@ url = "2.5.0" syn = "2.0.48" quote = "1.0.35" tokio = { version = "1", features = ["full"] } +lazy_static = "1.4.0" diff --git a/macro_utils/src/lib.rs b/macro_utils/src/lib.rs index 6011160..9d445a5 100644 --- a/macro_utils/src/lib.rs +++ b/macro_utils/src/lib.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use lazy_static::lazy_static; use serde::Deserialize; use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; use std::{fs::File, io::Read}; @@ -26,31 +27,42 @@ impl TestConfig { } } +#[derive(Clone, Debug)] pub struct RpcData { + pub latest_chain_block: u64, pub block_number: u64, pub spec_version: String, } -pub fn get_rpc_data() -> RpcData { +lazy_static! { + pub static ref RPC_DATA: RpcData = get_rpc_data(); +} + +fn get_rpc_data() -> RpcData { let config = TestConfig::new("./secret.json").expect("'./secret.json' must contain correct node urls"); let deoxys = JsonRpcClient::new(HttpTransport::new( Url::parse(&config.deoxys).expect("Error parsing Deoxys node url"), )); + let pathfinder = JsonRpcClient::new(HttpTransport::new( + Url::parse(&config.pathfinder).expect("Error parsing Pathfinder node url"), + )); + let rt = runtime::Runtime::new().unwrap(); rt.block_on(async { RpcData { + latest_chain_block: pathfinder.block_number().await.unwrap(), block_number: deoxys.block_number().await.unwrap(), spec_version: deoxys.spec_version().await.unwrap(), } }) } -pub fn extract_expr_to_str(expr: Expr) -> anyhow::Result { +pub fn extract_expr_to_str(expr: &Expr) -> anyhow::Result { match expr { - Expr::Lit(expr_lit) => match expr_lit.lit { + Expr::Lit(expr_lit) => match &expr_lit.lit { Lit::Str(lit_str) => anyhow::Ok(lit_str.value()), _ => Err(anyhow!("Not a string literal")), }, @@ -58,9 +70,9 @@ pub fn extract_expr_to_str(expr: Expr) -> anyhow::Result { } } -pub fn extract_expr_to_u64(expr: Expr) -> anyhow::Result { +pub fn extract_expr_to_u64(expr: &Expr) -> anyhow::Result { match expr { - Expr::Lit(expr_lit) => match expr_lit.lit { + Expr::Lit(expr_lit) => match &expr_lit.lit { Lit::Int(lit_int) => match lit_int.base10_parse::() { Ok(n) => anyhow::Ok(n), Err(_) => Err(anyhow!("Failed to convert literal")), diff --git a/unit_tests/tests/test_block_hash_and_number.rs b/unit_tests/tests/test_block_hash_and_number.rs index 3cbcc0f..6336c00 100644 --- a/unit_tests/tests/test_block_hash_and_number.rs +++ b/unit_tests/tests/test_block_hash_and_number.rs @@ -15,7 +15,7 @@ use starknet_providers::{ /// purpose: get block hash and number on latest block. /// success case: retrieves correct block hash and number. /// -#[require(spec_version = "0.5.1")] +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] async fn work_latest_block(clients: HashMap>) { diff --git a/unit_tests/tests/test_block_number.rs b/unit_tests/tests/test_block_number.rs index f545ffb..493e9f5 100644 --- a/unit_tests/tests/test_block_number.rs +++ b/unit_tests/tests/test_block_number.rs @@ -14,7 +14,7 @@ use starknet_providers::{ /// purpose: call blockNumber on latest block. /// success case: must return valid non-zero block number. /// -#[require(spec_version = "0.5.1")] +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] async fn work_existing_block(clients: HashMap>) { diff --git a/unit_tests/tests/test_call.rs b/unit_tests/tests/test_call.rs index d0059f7..a80ba79 100644 --- a/unit_tests/tests/test_call.rs +++ b/unit_tests/tests/test_call.rs @@ -79,7 +79,7 @@ async fn fail_non_existing_contract(clients: HashMap>) { @@ -145,7 +145,7 @@ async fn fail_missing_contract_call_data(clients: HashMap>) { @@ -175,7 +175,7 @@ async fn fail_invalid_contract_call_data(clients: HashMap>) { @@ -205,7 +205,7 @@ async fn fail_too_many_call_data(clients: HashMap>) { @@ -248,7 +248,7 @@ async fn work_correct_call(clients: HashMap /// purpose: function request `balanceOf` to StarkGate ETH bridge contract /// success case: must return non-zero balance /// -#[require(spec_version = "0.5.1")] +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] async fn work_correct_call_with_args(clients: HashMap>) { @@ -291,7 +291,7 @@ async fn work_correct_call_with_args(clients: HashMap>) { diff --git a/unit_tests/tests/test_estimate_fee.rs b/unit_tests/tests/test_estimate_fee.rs index 3fac579..27d1b44 100644 --- a/unit_tests/tests/test_estimate_fee.rs +++ b/unit_tests/tests/test_estimate_fee.rs @@ -33,6 +33,7 @@ async fn fail_non_existing_block(clients: HashMap>) { @@ -52,7 +52,7 @@ async fn work_with_latest_block(clients: HashMap>) { @@ -74,7 +74,7 @@ async fn work_with_block_one_num(clients: HashMap>) { @@ -101,7 +101,7 @@ async fn work_with_block_one_hash(clients: HashMap>) { @@ -82,7 +82,7 @@ async fn work_existing_block(clients: HashMap>) { @@ -52,7 +52,7 @@ async fn work_with_latest_block(clients: HashMap>) { @@ -74,7 +74,7 @@ async fn work_with_block_one_num(clients: HashMap>) { @@ -101,7 +101,7 @@ async fn work_with_block_one_hash(clients: HashMap>) { @@ -67,7 +67,7 @@ async fn fail_non_existing_contract(clients: HashMap>) { @@ -68,7 +68,7 @@ async fn fail_non_existing_contract(clients: HashMap>) { @@ -99,9 +99,10 @@ async fn work_block_latest(clients: HashMap /// purpose: call getClassHashAt on pending block. /// success case: retrieve valid class hash. /// -#[require(spec_version = "0.5.1")] +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] +#[ignore = "Pending fails some times when called on the cusp of being accepted, need virtual sequencer"] async fn work_block_pending(clients: HashMap>) { let deoxys = &clients[DEOXYS]; let pathfinder = &clients[PATHFINDER]; diff --git a/unit_tests/tests/test_get_nonce.rs b/unit_tests/tests/test_get_nonce.rs index 8261d38..9bec601 100644 --- a/unit_tests/tests/test_get_nonce.rs +++ b/unit_tests/tests/test_get_nonce.rs @@ -86,7 +86,7 @@ async fn fail_non_existing_contract(clients: HashMap>) { @@ -109,7 +109,7 @@ async fn work_erc721_contract(clients: HashMap>) { @@ -132,7 +132,7 @@ async fn work_erc20_contract(clients: HashMap>) { @@ -165,7 +165,7 @@ async fn work_account_contract(clients: HashMap>) { diff --git a/unit_tests/tests/test_get_state_update.rs b/unit_tests/tests/test_get_state_update.rs index 2d939bc..8a5bbfd 100644 --- a/unit_tests/tests/test_get_state_update.rs +++ b/unit_tests/tests/test_get_state_update.rs @@ -33,7 +33,7 @@ async fn fail_non_existing_block(clients: HashMap>) { @@ -71,7 +71,7 @@ async fn fail_non_existing_contract(clients: HashMap>) { @@ -95,7 +95,7 @@ async fn fail_invalid_storage_key(clients: HashMap>) { diff --git a/unit_tests/tests/test_get_transaction_by_block_id_and_index.rs b/unit_tests/tests/test_get_transaction_by_block_id_and_index.rs index b31493d..9b35b5e 100644 --- a/unit_tests/tests/test_get_transaction_by_block_id_and_index.rs +++ b/unit_tests/tests/test_get_transaction_by_block_id_and_index.rs @@ -61,7 +61,7 @@ async fn fail_non_existent_block_index(clients: HashMap>) { @@ -101,7 +101,7 @@ async fn work_deploy_invoke(clients: HashMap>) { @@ -141,7 +141,7 @@ async fn work_deploy_l1_handler(clients: HashMap>) { @@ -181,7 +181,7 @@ async fn work_deploy_declare(clients: HashMap>) { diff --git a/unit_tests/tests/test_get_transaction_status.rs b/unit_tests/tests/test_get_transaction_status.rs index 11f1671..a11ff37 100644 --- a/unit_tests/tests/test_get_transaction_status.rs +++ b/unit_tests/tests/test_get_transaction_status.rs @@ -40,7 +40,7 @@ async fn fail_invalid_transaction(clients: HashMap>) { @@ -67,7 +67,7 @@ async fn work_transaction_accepted_on_l1(clients: HashMap>) { @@ -109,7 +109,7 @@ async fn work_transaction_accepted_on_l2(clients: HashMap>) { diff --git a/unit_tests/tests/test_simulate_transaction.rs b/unit_tests/tests/test_simulate_transaction.rs index 6c0f416..fa84d85 100644 --- a/unit_tests/tests/test_simulate_transaction.rs +++ b/unit_tests/tests/test_simulate_transaction.rs @@ -94,6 +94,7 @@ async fn fail_if_one_txn_cannot_be_executed( ); } +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] async fn works_ok_on_no_validate(clients: HashMap>) { @@ -146,6 +147,7 @@ async fn works_ok_on_no_validate(clients: HashMap) { let response_deoxys = deoxys.spec_version().await.expect(ERR_DEOXYS); log::info!("Deoxys RPC spec: {}", response_deoxys); - assert_eq!(response_deoxys, RPC_SPEC); + assert_eq!(response_deoxys, SPEC_0_5_1); } diff --git a/unit_tests/tests/test_syncing.rs b/unit_tests/tests/test_syncing.rs index 2be00d8..24f3e59 100644 --- a/unit_tests/tests/test_syncing.rs +++ b/unit_tests/tests/test_syncing.rs @@ -11,7 +11,7 @@ use std::collections::HashMap; /// purpose: returns starknet sync status /// success case: sync status matches between providers (NOT DETERMINISTIC) /// -#[require(spec_version = "0.5.1")] +#[require(block_min = "latest", spec_version = "0.5.1")] #[rstest] #[tokio::test] async fn syncing(clients: HashMap>) {