diff --git a/crates/rpc/tests/rpc.rs b/crates/rpc/tests/rpc.rs index 1107f32d7..b6047c36d 100644 --- a/crates/rpc/tests/rpc.rs +++ b/crates/rpc/tests/rpc.rs @@ -22,16 +22,30 @@ struct TestContext { extracted_value: T, } +/// Creates a `TestContext` with the latest block. async fn latest_block_context() -> TestContext<()> { context(|_block| Some(())).await } +/// Creates a `TestContext` with the latest block with at least 10 transactions. +async fn latest_10_tx_context() -> TestContext<()> { + context( + |block| { + if block.transactions.len() >= 10 { + Some(()) + } else { + None + } + }, + ) + .await +} /// Instantiate a client and look for a block matching the `extractor`, going from latest /// to older. -/// +/// /// # Parameters -/// +/// /// * `extractor`: a closure returning `Some(T)` in the case of a match. The value returned /// by the extractor will end up in the `TestContext`'s `extracted_value` field. async fn context Option, T>( @@ -52,13 +66,6 @@ async fn context Option, T>( } }; - // TODO Make it a criterion. - assert!( - block.transactions.len() >= 10, - "This test requires blocks to have at least N transactions, got {}", - block.transactions.len() - ); - if let Some(extracted_value) = extractor(&block) { return TestContext { client, @@ -195,7 +202,7 @@ async fn test_get_block_transaction_count() { #[tokio::test] async fn test_get_block_with_tx_hashes() { let TestContext { client, block, block_id, extracted_value: () } = - latest_block_context().await; + latest_10_tx_context().await; let BlockWithTxHashes { status, @@ -260,19 +267,20 @@ async fn test_get_class() { #[tokio::test] async fn test_get_class_at() { - let TestContext { client, block: _, block_id, extracted_value } = - context(|block| { - block.transactions.iter().find_map( - |transaction| match transaction { - Transaction::DeployAccount( - DeployAccountTransaction::V3(deploy), - ) => Some(deploy.transaction_hash), - _ => None, - }, - ) + let TestContext { + client, + block: _, + block_id, + extracted_value: deploy_tx_hash, + } = context(|block| { + block.transactions.iter().find_map(|transaction| match transaction { + Transaction::DeployAccount(DeployAccountTransaction::V3( + deploy, + )) => Some(deploy.transaction_hash), + _ => None, }) - .await; - let deploy_tx_hash = extracted_value; + }) + .await; let receipt = match client .get_transaction_receipt(deploy_tx_hash) @@ -293,19 +301,20 @@ async fn test_get_class_at() { #[tokio::test] async fn test_get_class_hash_at() { - let TestContext { client, block: _, block_id, extracted_value } = - context(|block| { - block.transactions.iter().find_map( - |transaction| match transaction { - Transaction::DeployAccount( - DeployAccountTransaction::V3(deploy), - ) => Some(deploy.transaction_hash), - _ => None, - }, - ) + let TestContext { + client, + block: _, + block_id, + extracted_value: deploy_tx_hash, + } = context(|block| { + block.transactions.iter().find_map(|transaction| match transaction { + Transaction::DeployAccount(DeployAccountTransaction::V3( + deploy, + )) => Some(deploy.transaction_hash), + _ => None, }) - .await; - let deploy_tx_hash = extracted_value; + }) + .await; let receipt = match client .get_transaction_receipt(deploy_tx_hash) @@ -326,23 +335,26 @@ async fn test_get_class_hash_at() { #[tokio::test] async fn test_get_nonce() { - let TestContext { client, block: _, block_id, extracted_value } = - context(|block| { - // Browse the transaction in reverse order to make sure we got the latest nonce. - block.transactions.iter().rev().find_map(|transaction| { - match transaction { - Transaction::Invoke(InvokeTransaction::V1(invoke)) => { - Some((invoke.nonce, invoke.sender_address)) - } - Transaction::Invoke(InvokeTransaction::V3(invoke)) => { - Some((invoke.nonce, invoke.sender_address)) - } - _ => None, + let TestContext { + client, + block: _, + block_id, + extracted_value: (original_nonce, sender), + } = context(|block| { + // Browse the transaction in reverse order to make sure we got the latest nonce. + block.transactions.iter().rev().find_map( + |transaction| match transaction { + Transaction::Invoke(InvokeTransaction::V1(invoke)) => { + Some((invoke.nonce, invoke.sender_address)) } - }) - }) - .await; - let (original_nonce, sender) = extracted_value; + Transaction::Invoke(InvokeTransaction::V3(invoke)) => { + Some((invoke.nonce, invoke.sender_address)) + } + _ => None, + }, + ) + }) + .await; let original_nonce = truncate_felt_to_u128(&original_nonce); let incremented_nonce = @@ -355,7 +367,7 @@ async fn test_get_nonce() { #[tokio::test] async fn test_get_transaction_by_block_id_and_index() { let TestContext { client, block, block_id, extracted_value: () } = - latest_block_context().await; + latest_10_tx_context().await; async fn check_transaction( client: &JsonRpcClient, @@ -385,7 +397,7 @@ async fn test_get_transaction_by_block_id_and_index() { #[tokio::test] async fn test_get_transaction_by_hash() { let TestContext { client, block, block_id: _, extracted_value: () } = - latest_block_context().await; + latest_10_tx_context().await; async fn check_transaction( client: &JsonRpcClient, @@ -420,7 +432,7 @@ async fn test_get_transaction_by_hash() { #[tokio::test] async fn test_get_transaction_status() { let TestContext { client, block, block_id: _, extracted_value: () } = - latest_block_context().await; + latest_10_tx_context().await; async fn check_transaction( client: &JsonRpcClient, @@ -472,7 +484,5 @@ async fn test_get_transaction_status() { */ fn truncate_felt_to_u128(felt: &FieldElement) -> u128 { - u128::from_be_bytes( - felt.to_bytes_be().split_at(16).1.try_into().unwrap(), - ) -} \ No newline at end of file + u128::from_be_bytes(felt.to_bytes_be().split_at(16).1.try_into().unwrap()) +}