Skip to content

Commit

Permalink
Merge pull request #36 from jbcaron/main
Browse files Browse the repository at this point in the history
cache improve, block "latest"
  • Loading branch information
antiyro authored Jan 15, 2024
2 parents 1528720 + 874605d commit 9a61f6c
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 53 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions macro_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 17 additions & 5 deletions macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -26,41 +27,52 @@ 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<String> {
pub fn extract_expr_to_str(expr: &Expr) -> anyhow::Result<String> {
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")),
},
_ => Err(anyhow!("Not a literal expression")),
}
}

pub fn extract_expr_to_u64(expr: Expr) -> anyhow::Result<u64> {
pub fn extract_expr_to_u64(expr: &Expr) -> anyhow::Result<u64> {
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::<u64>() {
Ok(n) => anyhow::Ok(n),
Err(_) => Err(anyhow!("Failed to convert literal")),
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/tests/test_block_hash_and_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonRpcClient<HttpTransport>>) {
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/tests/test_block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonRpcClient<HttpTransport>>) {
Expand Down
14 changes: 7 additions & 7 deletions unit_tests/tests/test_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpT
/// purpose: function request `name` to StarkGate ETH bridge contract
/// fail case: invalid field element
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_invalid_contract_entry_point_selector(
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn fail_invalid_contract_entry_point_selector(
/// purpose: function request `balanceOf` to StarkGate ETH bridge contract
/// fail case: missing call data. This is different from solely *invalid* call data, as we will see shortly
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_missing_contract_call_data(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -145,7 +145,7 @@ async fn fail_missing_contract_call_data(clients: HashMap<String, JsonRpcClient<
/// purpose: function request `balanceOf` to StarkGate ETH bridge contract
/// fail case: invalid call data. This does not cause an error upon calling the contract but returns felt 0x0
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_invalid_contract_call_data(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn fail_invalid_contract_call_data(clients: HashMap<String, JsonRpcClient<
/// purpose: function request `name` to StarkGate ETH bridge contract
/// fail case: too many arguments in call data
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_too_many_call_data(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -205,7 +205,7 @@ async fn fail_too_many_call_data(clients: HashMap<String, JsonRpcClient<HttpTran
/// purpose: function request `name` to StarkGate ETH bridge contract
/// success case: should return 'Ether'
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_correct_call(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -248,7 +248,7 @@ async fn work_correct_call(clients: HashMap<String, JsonRpcClient<HttpTransport>
/// 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<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -291,7 +291,7 @@ async fn work_correct_call_with_args(clients: HashMap<String, JsonRpcClient<Http
/// purpose: function request `sort_tokens` to JediSwap exchange, with multiple arguments.
/// success case: must return array of 2 non-zero values.
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_multiple_args(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down
2 changes: 2 additions & 0 deletions unit_tests/tests/test_estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
);
}

#[require(block_min = "latest")]
#[rstest]
#[tokio::test]
#[ignore = "Fix failing unwrap due to empty constant"]
Expand Down Expand Up @@ -61,6 +62,7 @@ async fn fail_if_one_txn_cannot_be_executed(
assert_eq!(result_deoxys, result_pathfinder);
}

#[require(block_min = "latest")]
#[rstest]
#[tokio::test]
#[ignore = "Fix failing unwrap due to empty constant"]
Expand Down
10 changes: 5 additions & 5 deletions unit_tests/tests/test_get_block_transaction_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -52,7 +52,7 @@ async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTrans
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -74,7 +74,7 @@ async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTran
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -101,7 +101,7 @@ async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTra
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_num(
Expand All @@ -125,7 +125,7 @@ async fn work_with_block_one_hundred_thousand_num(
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_hash(
Expand Down
4 changes: 2 additions & 2 deletions unit_tests/tests/test_get_block_with_tx_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
/// purpose: call getBlockWithTxHashes on latest validated block.
/// success case: retrieves valid block.
///
#[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<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -82,7 +82,7 @@ async fn work_existing_block(clients: HashMap<String, JsonRpcClient<HttpTranspor
///
/// Note that this can fail at the last moments of a block being validated!!!
///
#[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"]
Expand Down
10 changes: 5 additions & 5 deletions unit_tests/tests/test_get_block_with_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -52,7 +52,7 @@ async fn work_with_latest_block(clients: HashMap<String, JsonRpcClient<HttpTrans
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -74,7 +74,7 @@ async fn work_with_block_one_num(clients: HashMap<String, JsonRpcClient<HttpTran
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 1, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -101,7 +101,7 @@ async fn work_with_block_one_hash(clients: HashMap<String, JsonRpcClient<HttpTra
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_num(
Expand All @@ -125,7 +125,7 @@ async fn work_with_block_one_hundred_thousand_num(
assert_eq!(response_deoxys, response_pathfinder);
}

#[require(spec_version = "0.5.1")]
#[require(block_min = 100_000, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_with_block_one_hundred_thousand_hash(
Expand Down
4 changes: 2 additions & 2 deletions unit_tests/tests/test_get_class_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
/// purpose: gets contract class for inexistent contract.
/// fail case: invalid contract address.
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand All @@ -67,7 +67,7 @@ async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpT
/// purpose: gets legacy contract and extracts it's data.
/// success case: should retrieve contract and decompress it to a valid json string.
///
#[require(spec_version = "0.5.1")]
#[require(block_min = 2891, spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn work_contract_v0(
Expand Down
7 changes: 4 additions & 3 deletions unit_tests/tests/test_get_class_hash_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn fail_non_existing_block(clients: HashMap<String, JsonRpcClient<HttpTran
/// purpose: call getClassHashAt on non-existent contract.
/// fail case: invalid contract hash.
///
#[require(spec_version = "0.5.1")]
#[require(block_min = "latest", spec_version = "0.5.1")]
#[rstest]
#[tokio::test]
async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -68,7 +68,7 @@ async fn fail_non_existing_contract(clients: HashMap<String, JsonRpcClient<HttpT
/// purpose: call getClassHashAt on latest 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]
async fn work_block_latest(clients: HashMap<String, JsonRpcClient<HttpTransport>>) {
Expand Down Expand Up @@ -99,9 +99,10 @@ async fn work_block_latest(clients: HashMap<String, JsonRpcClient<HttpTransport>
/// 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<String, JsonRpcClient<HttpTransport>>) {
let deoxys = &clients[DEOXYS];
let pathfinder = &clients[PATHFINDER];
Expand Down
Loading

0 comments on commit 9a61f6c

Please sign in to comment.