Skip to content

Commit

Permalink
chore: Implement latest_10_tx_context for the tests that require it
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-l committed Apr 8, 2024
1 parent ea59ff9 commit 00b7ad7
Showing 1 changed file with 67 additions and 57 deletions.
124 changes: 67 additions & 57 deletions crates/rpc/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,30 @@ struct TestContext<T> {
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<F: Fn(&BlockWithTxs) -> Option<T>, T>(
Expand All @@ -52,13 +66,6 @@ async fn context<F: Fn(&BlockWithTxs) -> Option<T>, 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 =
Expand All @@ -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<HttpTransport>,
Expand Down Expand Up @@ -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<HttpTransport>,
Expand Down Expand Up @@ -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<HttpTransport>,
Expand Down Expand Up @@ -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(),
)
}
u128::from_be_bytes(felt.to_bytes_be().split_at(16).1.try_into().unwrap())
}

0 comments on commit 00b7ad7

Please sign in to comment.