diff --git a/crates/rpc/tests/rpc.rs b/crates/rpc/tests/rpc.rs index 3f87a253f..a75623ba8 100644 --- a/crates/rpc/tests/rpc.rs +++ b/crates/rpc/tests/rpc.rs @@ -85,7 +85,7 @@ async fn context Option, T>( let block = match client .get_block_with_txs(BlockId::Number(block_number)) .await - .expect("Failed to retrieve the latest block") + .expect("Block retrieval failed") { MaybePendingBlockWithTxs::Block(block) => block, MaybePendingBlockWithTxs::PendingBlock(_) => { @@ -106,8 +106,9 @@ async fn context Option, T>( panic!("No suitable block found") } +/// An extension trait providing extra methods to the Starknet client. pub trait L2ClientExt { - // TODO 550 Doc + /// Retrieve the list of all the events of a single block. fn get_block_events( &self, block_id: BlockId, @@ -196,168 +197,6 @@ async fn test_chain_id() { client.chain_id().await.expect("Failed to retrieve the chain ID"); } -// TODO -#[ignore = "draft"] -#[tokio::test] -async fn test_estimate_fee() { - 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::V3(invoke)) => { - Some(invoke.clone()) - } - // TODO Make the match exhaustive to make dep upgrades more reliable. - _ => None, - } - }) - }) - .await; - let invoke = extracted_value; - - // TODO Criterion - // TODO unwrap. - assert_eq!( - client - .get_transaction_receipt(invoke.transaction_hash) - .await - .unwrap() - .execution_result() - .status(), - TransactionExecutionStatus::Succeeded - ); - - let incremented_nonce = { - // TODO Ugly conversion - let mut incremented_nonce = u128::from_be_bytes( - invoke.nonce.to_bytes_be().split_at(16).1.try_into().unwrap(), - ); - incremented_nonce += 1; - - FieldElement::from_byte_slice_be(&incremented_nonce.to_be_bytes()) - .unwrap() - }; - - let tx_to_estimate = BroadcastedInvokeTransactionV3 { - sender_address: invoke.sender_address, - signature: invoke.signature, - nonce: incremented_nonce, - is_query: true, - resource_bounds: invoke.resource_bounds, - tip: invoke.tip, - paymaster_data: invoke.paymaster_data, - account_deployment_data: invoke.account_deployment_data, - nonce_data_availability_mode: invoke.nonce_data_availability_mode, - fee_data_availability_mode: invoke.fee_data_availability_mode, - calldata: invoke.calldata, - }; - - let estimation = client - .estimate_fee( - [BroadcastedTransaction::Invoke( - starknet::core::types::BroadcastedInvokeTransaction::V3( - tx_to_estimate.clone(), - ), - )], - &[SimulationFlagForEstimateFee::SkipValidate], - block_id, - ) - .await - .expect("Fee estimation failed"); - - assert_eq!(estimation.len(), 1); - - let single_estimation = client - .estimate_fee_single( - BroadcastedTransaction::Invoke( - starknet::core::types::BroadcastedInvokeTransaction::V3( - tx_to_estimate, - ), - ), - &[SimulationFlagForEstimateFee::SkipValidate], - block_id, - ) - .await - .expect("Fee estimation failed"); - - assert_eq!(estimation[0], single_estimation); -} - -#[ignore = "tested with test_estimate_fee"] -#[test] -fn test_estimate_fee_single() {} - -// TODO -#[ignore = "Incomplete, doesn't work yet"] -#[tokio::test] -async fn test_call() { - 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.clone()), - // TODO Make the match exhaustive to make dep upgrades more reliable. - _ => None, - } - }) - }) - .await; - let deploy = extracted_value; - - let class = client - .get_class(block_id, deploy.class_hash) - .await - .expect("getClass failed"); - - let sierra = match class { - starknet::core::types::ContractClass::Sierra(class) => class, - // TODO - starknet::core::types::ContractClass::Legacy(_) => panic!(), - }; - - let address = { - let receipt = client - .get_transaction_receipt(deploy.transaction_hash) - .await - .expect("an existing receipt"); - - // TODO Check the execution & finality status? - match receipt { - MaybePendingTransactionReceipt::Receipt( - TransactionReceipt::DeployAccount(receipt), - ) => receipt.contract_address, - // TODO - _ => panic!(), - } - }; - - let entrypoint = sierra.entry_points_by_type.external.first().unwrap(); - - // TODO Remove - println!("{}", sierra.abi); - // let abi: Vec = serde_json::from_str(&sierra.abi).unwrap(); - - /* TODO - The problem here is that we need to find a function we can call and valid parameters for that function. - We could rely on the Argent & Braavos account contracts, maybe call `getBalance`. I just don't know how - to do that yet :) - */ - client - .call( - FunctionCall { - contract_address: address, - entry_point_selector: entrypoint.selector, - calldata: vec![], - }, - block_id, - ) - .await - .unwrap(); -} - #[tokio::test] async fn test_get_block_transaction_count() { let TestContext { client, block, block_id, extracted_value: () } = @@ -508,10 +347,6 @@ async fn test_get_class_hash_at() { .expect("getClass failed"); } -#[ignore = "tested with test_get_transaction_receipt"] -#[test] -fn test_get_events() {} - #[tokio::test] async fn test_get_nonce() { let TestContext { client, block: _, block_id, extracted_value } = @@ -547,48 +382,6 @@ async fn test_get_nonce() { assert_eq!(original_nonce + 1, incremented_nonce); } -#[ignore = "this is still a draft"] -#[tokio::test] -async fn test_get_storage_at() { - 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::DeployAccount( - DeployAccountTransaction::V3(deploy), - ) => Some(deploy.clone()), - // TODO Make the match exhaustive to make dep upgrades more reliable. - _ => None, - } - }) - }) - .await; - let deploy = extracted_value; - - let receipt = client - .get_transaction_receipt(deploy.transaction_hash) - .await - .expect("get_transaction_receipt failed"); - - let deploy_receipt = match receipt { - MaybePendingTransactionReceipt::Receipt( - TransactionReceipt::DeployAccount(deploy_receipt), - ) => deploy_receipt, - receipt => panic!("Unexpected receipt type: {:?}", receipt), - }; - - // TODO Event array size assertions - client - .get_storage_at( - deploy_receipt.contract_address, - deploy_receipt.events[0].keys[0], - block_id, - ) - .await - .expect("getStorageAt failed"); -} - #[tokio::test] async fn test_get_transaction_by_block_id_and_index() { let TestContext { client, block, block_id, extracted_value: () } = @@ -654,95 +447,6 @@ async fn test_get_transaction_by_hash() { .await; } -// TODO -#[ignore = "draft"] -#[tokio::test] -async fn test_get_transaction_receipt() { - let TestContext { client, block, block_id: _, extracted_value: () } = - latest_block_context().await; - - async fn check_transaction( - client: &JsonRpcClient, - block: &BlockWithTxs, - expected: &Transaction, - ) { - let receipt = client.get_transaction_receipt(expected.transaction_hash()).await.expect("Failed to retrieve a specific transaction by its block ID and index"); - - let receipt = match receipt { - MaybePendingTransactionReceipt::Receipt(receipt) => receipt, - MaybePendingTransactionReceipt::PendingReceipt(_) => { - // TODO Make it a criteria? - panic!("Pending receipt") - } - }; - - // TODO Check the execution & finality status. If the tx was reverted, no event was emitted and - // we can't go any further with the assertions. - - let events = match receipt { - starknet::core::types::TransactionReceipt::Invoke(receipt) => { - receipt.events - } - starknet::core::types::TransactionReceipt::L1Handler(receipt) => { - receipt.events - } - starknet::core::types::TransactionReceipt::Declare(receipt) => { - receipt.events - } - starknet::core::types::TransactionReceipt::Deploy(receipt) => { - receipt.events - } - starknet::core::types::TransactionReceipt::DeployAccount( - receipt, - ) => receipt.events, - }; - - let block_id = BlockId::Number(block.block_number); - - let expected_events = client - .get_block_events(block_id) - .await - .expect("Failed to retrieve the events"); - - assert!(expected_events.len() >= events.len(), "getEvents should have returned at least as many events. GetEvents returned {} while we initially had {}", expected_events.len(), events.len()); - - for actual in &events { - // Find a matching event from the expected events vec. - // We can't invert that relationship because getEvents might have returned more than we need here. - if !expected_events.iter().any(|expected| { - actual.data == expected.data - && actual.from_address == expected.from_address - && actual.keys == expected.keys - }) { - panic!("No match found"); - } - } - - // TODO transaction status comparison - // TODO execution result comparison - } - - for transaction_index in 0..10 { - check_transaction( - &client, - &block, - &block.transactions[transaction_index], - ) - .await; - } - - check_transaction( - &client, - &block, - block - .transactions - .last() - .as_ref() - .expect("We need a last transaction here"), - ) - .await; -} - #[tokio::test] async fn test_get_transaction_status() { let TestContext { client, block, block_id: _, extracted_value: () } = @@ -752,7 +456,6 @@ async fn test_get_transaction_status() { client: &JsonRpcClient, transaction_hash: FieldElement, ) { - // TODO No further assertion to make? let _status = client .get_transaction_status(transaction_hash) .await @@ -779,16 +482,20 @@ async fn test_get_transaction_status() { .await; } -// TODO starknet_call -// TODO starknet_getBalance - -// TODO starknet_getProof -// TODO starknet_getStateRoot - -// TODO starknet_getStateUpdate -// TODO starknet_getStorateAt - -// starknet_getTransactionStatus -// TODO starknet_syncing - -// TODO Look for other methods, I don't think the list is exhaustive. +/* TODO + Add more test scenarios to cover the following methods: + + starknet_call + starknet_estimateFee + starknet_estimateFeeSingle + starknet_getBalance + starknet_getEvents + starknet_getProof + starknet_getStateRoot + starknet_getStateUpdate + starknet_getStorateAt + starknet_getTransactionReceipt + starknet_getTransactionStatus + starknet_syncing + pathfinder_getProof + */