From 00d4787e827bb96508454592932f8829f33d0e4a Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Wed, 28 Jun 2023 23:44:31 +0100 Subject: [PATCH 01/12] test: added two test cases for get_storage_at --- crates/beerus-core/tests/beerus.rs | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 96ba4c1b..9eff0db0 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -1665,6 +1665,103 @@ mod tests { assert_eq!(res, expected_result); } + /// Test that starknet storage value is returned when the Starknet light client returns a value(second scenario) + #[tokio::test] + async fn given_normal_conditions_with_second_scenario_when_starknet_get_storage_at_should_work() + { + // Mock config, ethereum light client and starknet light client. + let (config, mut ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let expected_result = FieldElement::from_hex_be("298305742194").unwrap(); + + let test_block_with_tx_hashes = BlockWithTxHashes { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number: 10, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let test_block = MaybePendingBlockWithTxHashes::Block(test_block_with_tx_hashes); + + // Set the expected return value for the StarkNet light client mock. + starknet_lightclient_mock + .expect_get_storage_at() + .times(1) + .return_once(move |_address, _key, _block_nb| Ok(expected_result)); + + starknet_lightclient_mock + .expect_get_block_with_tx_hashes() + .times(1) + .return_once(move |_block_id| Ok(test_block)); + + ethereum_lightclient_mock + .expect_starknet_last_proven_block() + .return_once(move || Ok(U256::from(10))); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let address = FieldElement::from_hex_be( + "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", + ) + .unwrap(); + + let key = selector!("ERC20_name"); + let block_id = BlockId::Hash(FieldElement::from_hex_be("0").unwrap()); + + // Perform the test call. + let res = beerus + .starknet_get_storage_at(address, key, &block_id) + .await + .unwrap(); + + assert_eq!(res, expected_result); + } + + /// Test that an error is return when getting storage at an unproven block + #[tokio::test] + async fn given_unproven_blockid_when_starknet_get_storage_at_should_fail_with_blockid_not_proven_err( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, mut ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + ethereum_lightclient_mock + .expect_starknet_last_proven_block() + .return_once(move || Ok(U256::from(10))); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let address = FieldElement::from_hex_be( + "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", + ) + .unwrap(); + + let key = selector!("ERC20_name"); + let block_id = BlockId::Number(11); + + // Perform the test call. + let res = beerus + .starknet_get_storage_at(address, key, &block_id) + .await; + + assert!(res.is_err()); + let expected_result = "BlockId is not proven yet"; + assert_eq!(res.unwrap_err().to_string(), expected_result.to_string()); + } + /// Test that starknet get_storage_at return an error when the StarkNet Light client returns an error. #[tokio::test] async fn given_starknet_lightclient_returns_error_when_starknet_get_storage_at_should_fail_with_same_error( From b7a5fbdc01bc77343c4305cb53b22e1139b06158 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Sat, 1 Jul 2023 22:58:40 +0100 Subject: [PATCH 02/12] test: added test for starknet_estimate_fee --- crates/beerus-core/tests/beerus.rs | 88 +++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 9eff0db0..204e09c5 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -27,7 +27,8 @@ mod tests { BlockHashAndNumber, BlockId, BlockStatus, BlockTag as StarknetBlockTag, BlockWithTxHashes, BlockWithTxs, BroadcastedDeclareTransaction, BroadcastedDeclareTransactionV1, BroadcastedDeployTransaction, - BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV0, ContractClass, + BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV0, + BroadcastedInvokeTransactionV1, BroadcastedTransaction, ContractClass, DeclareTransactionResult, DeployTransactionResult, EventFilter, FeeEstimate, InvokeTransaction, InvokeTransactionReceipt, InvokeTransactionResult, InvokeTransactionV0, LegacyContractClass, LegacyContractEntryPoint, @@ -1629,6 +1630,89 @@ mod tests { assert_eq!(res.unwrap_err().to_string(), expected_error); } + /// Test that starknet estimated fee is returned when the Starknet light client returns a value. + #[tokio::test] + async fn given_normal_conditions_when_starknet_estimate_fee_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let expected_result = FeeEstimate { + gas_consumed: 0, + gas_price: 0, + overall_fee: 0, + }; + + // Set the expected return value for the Ethereum light client mock. + starknet_lightclient_mock + .expect_estimate_fee() + .times(1) + .return_once(|_request, _block_id| Ok(expected_result)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let request = BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction::V1( + BroadcastedInvokeTransactionV1 { + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0").unwrap(), + calldata: Vec::::new(), + }, + )); + let block_id = BlockId::Number(10); + + // Perform the test estimate. + let res = beerus.starknet_estimate_fee(request, &block_id).await; + + // // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet estimate_fee return an error when the StarkNet Light client returns an error + #[tokio::test] + async fn given_starknet_lightclient_returns_error_when_starknet_estimate_fee_should_fail_with_same_error( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + // Set the expected return value for the Starknet light client mock. + let expected_error = "Wrong url"; + + starknet_lightclient_mock + .expect_estimate_fee() + .return_once(move |_block_nb, _address| Err(eyre!(expected_error))); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let request = BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction::V1( + BroadcastedInvokeTransactionV1 { + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0").unwrap(), + calldata: Vec::::new(), + }, + )); + let block_id = BlockId::Number(10); + + // Perform the test estimate. + let res = beerus.starknet_estimate_fee(request, &block_id).await; + + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error); + } + /// Test that starknet storage value is returned when the Starknet light client returns a value. #[tokio::test] async fn given_normal_conditions_when_starknet_get_storage_at_should_work() { @@ -1837,7 +1921,7 @@ mod tests { assert_eq!(res, expected_result); } - /// Test that starknet get_nonce. + /// Test that starknet get_nonce return an error when the StarkNet Light client returns an error #[tokio::test] async fn given_starknet_lightclient_returns_error_when_starknet_get_nonce_should_fail_with_same_error( ) { From da967f0eb3f0c12d24638ab8009ff5547bc4f38d Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Sun, 2 Jul 2023 00:20:40 +0100 Subject: [PATCH 03/12] test: added test for starknet get_block_with_txs --- crates/beerus-core/tests/beerus.rs | 175 +++++++++++++++++++++++++++-- 1 file changed, 168 insertions(+), 7 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 204e09c5..7c619d98 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -591,7 +591,7 @@ mod tests { /// It tests the `get_transaction_count` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_tx_count_then_error_is_propagated() + async fn given_ethereum_lightclient_returns_error_when_query_tx_count_then_error_is_propagated() { // Given // Mock config, ethereum light client and starknet light client. @@ -672,7 +672,7 @@ mod tests { /// It tests the `get_block_transaction_count_by_number` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_tx_count_by_block_number_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_tx_count_by_block_number_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -853,7 +853,7 @@ mod tests { /// It tests the `get_block_transaction_count_by_hash` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_tx_count_by_block_hash_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_tx_count_by_block_hash_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -941,7 +941,7 @@ mod tests { /// It tests the `query_transaction_by_hash` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_transaction_by_hash_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_transaction_by_hash_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -1027,7 +1027,7 @@ mod tests { /// It tests the `gas_price` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_gas_price_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_gas_price_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -1112,7 +1112,7 @@ mod tests { /// Test the `estimate_gas` method when the Ethereum light client returns an error. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_estimate_gas_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_estimate_gas_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -1307,7 +1307,7 @@ mod tests { /// It tests the `priority_fee` method of the Beerus light client. /// It tests the error handling of the `start` method of the Beerus light client. #[tokio::test] - async fn giver_ethereum_lightclient_returns_error_when_query_priority_fee_then_error_is_propagated( + async fn given_ethereum_lightclient_returns_error_when_query_priority_fee_then_error_is_propagated( ) { // Given // Mock config, ethereum light client and starknet light client. @@ -2137,6 +2137,167 @@ mod tests { assert_eq!(result.unwrap_err().to_string(), expected_error); } + /// Test that starknet block is returned when a block number is given and the Starknet light client returns a value. + #[tokio::test] + async fn given_block_number_when_starknet_get_block_with_txs_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let test_block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number: 10, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + + // // Set the expected return value for the Starknet light client mock. + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_result)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(10); + + let res = beerus.get_block_with_txs(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet block is returned when a block tag is given and the Starknet light client returns a value. + #[tokio::test] + async fn given_block_tag_when_starknet_get_block_with_txs_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let test_block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number: 10, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + + // // Set the expected return value for the Starknet light client mock. + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_result)); + + starknet_lightclient_mock + .expect_block_number() + .times(1) + .return_once(|| Ok(10)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Tag(StarknetBlockTag::Latest); + + let res = beerus.get_block_with_txs(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet block is returned when a block hash is given and the Starknet light client returns a value. + #[tokio::test] + async fn given_block_hash_when_starknet_get_block_with_txs_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let test_block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number: 10, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + + // // Set the expected return value for the Starknet light client mock. + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_result)); + + starknet_lightclient_mock + .expect_block_number() + .times(1) + .return_once(|| Ok(10)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Hash(FieldElement::from_hex_be("0").unwrap()); + + let res = beerus.get_block_with_txs(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + // Test that starknet block return an error when the StarkNet Light client returns an error + #[tokio::test] + async fn given_starknet_lightclient_returns_error_when_starknet_get_block_with_txs_should_fail_with_same_error( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + // Set the expected return value for the Starknet light client mock. + let expected_error = "Wrong url"; + + starknet_lightclient_mock + .expect_get_block_with_txs() + .return_once(move |_block_id| Err(eyre!(expected_error))); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(10); + + // // Perform the get_block_with_txs + let res = beerus.get_block_with_txs(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 3e83926b9653e1e592178cb971c23254eb5863bc Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Mon, 3 Jul 2023 21:28:40 +0100 Subject: [PATCH 04/12] test: added tests for get_block_hash_and_number and get_block_with_txs --- crates/beerus-core/tests/beerus.rs | 112 ++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 7c619d98..00e0f333 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -9,7 +9,7 @@ mod tests { use beerus_core::{ config::Config, lightclient::{ - beerus::{BeerusLightClient, SyncStatus}, + beerus::{BeerusLightClient, NodeData, SyncStatus}, ethereum::helios_lightclient::HeliosLightClient, starknet::{StarkNetLightClient, StarkNetLightClientImpl}, }, @@ -37,7 +37,8 @@ mod tests { Transaction as StarknetTransaction, TransactionReceipt, TransactionStatus, }, }; - use std::str::FromStr; + use std::{collections::BTreeMap, str::FromStr, sync::Arc}; + use tokio::sync::RwLock; #[test] fn when_call_new_then_should_return_beerus_lightclient() { @@ -2177,6 +2178,49 @@ mod tests { assert!(res.is_ok()); } + /// Test that starknet block is returned when a block number is given and the Starknet light client returns a value(second scenerio). + #[tokio::test] + async fn given_block_number_second_scenerio_when_starknet_get_block_with_txs_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let block_number = 10; + + let test_block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let mut test_btree_map: BTreeMap = BTreeMap::new(); + test_btree_map.insert(block_number, test_block_with_tx_hashes); + + let test_node = NodeData { + block_number, + state_root: String::from("0"), + payload: test_btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(test_node)); + + let block_id = BlockId::Number(10); + + let res = beerus.get_block_with_txs(&block_id).await; + // Assert that the result is correct. + assert!(res.is_ok()); + } + /// Test that starknet block is returned when a block tag is given and the Starknet light client returns a value. #[tokio::test] async fn given_block_tag_when_starknet_get_block_with_txs_should_work() { @@ -2298,6 +2342,70 @@ mod tests { assert_eq!(res.unwrap_err().to_string(), expected_error); } + /// Test that starknet block hash and number is returned when the Starknet light client returns a value. + #[tokio::test] + async fn given_normal_condition_starknet_get_block_hash_and_number_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let block_number = 10; + + let test_block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let mut test_btree_map: BTreeMap = BTreeMap::new(); + test_btree_map.insert(block_number, test_block_with_tx_hashes); + + let test_node = NodeData { + block_number, + state_root: String::from("0"), + payload: test_btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + beerus.node = Arc::new(RwLock::new(test_node)); + + let res = beerus.get_block_hash_and_number().await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet block hash and number returns error when block number is not found in payload but Starknet light client returns a value. + #[tokio::test] + async fn given_error_condition_starknet_get_block_hash_and_number_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let res = beerus.get_block_hash_and_number().await; + + let expected_error = "Block not found"; + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 77eba816ccbb4414103a930f8744adff9a5a9584 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Thu, 6 Jul 2023 15:36:52 +0100 Subject: [PATCH 05/12] test: added tests for starknet_get_transaction_receipt --- crates/beerus-core/tests/beerus.rs | 159 +++++++++++++++++++++++++---- 1 file changed, 139 insertions(+), 20 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 00e0f333..541ccd5e 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -2144,7 +2144,7 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let test_block_with_tx_hashes = BlockWithTxs { + let block_with_tx_hashes = BlockWithTxs { status: BlockStatus::AcceptedOnL2, block_hash: FieldElement::from_hex_be("0").unwrap(), parent_hash: FieldElement::from_hex_be("0").unwrap(), @@ -2155,7 +2155,7 @@ mod tests { transactions: Vec::new(), }; - let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); // // Set the expected return value for the Starknet light client mock. starknet_lightclient_mock @@ -2186,7 +2186,7 @@ mod tests { let block_number = 10; - let test_block_with_tx_hashes = BlockWithTxs { + let block_with_tx_hashes = BlockWithTxs { status: BlockStatus::AcceptedOnL2, block_hash: FieldElement::from_hex_be("0").unwrap(), parent_hash: FieldElement::from_hex_be("0").unwrap(), @@ -2197,13 +2197,13 @@ mod tests { transactions: Vec::new(), }; - let mut test_btree_map: BTreeMap = BTreeMap::new(); - test_btree_map.insert(block_number, test_block_with_tx_hashes); + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); - let test_node = NodeData { + let node_data = NodeData { block_number, state_root: String::from("0"), - payload: test_btree_map, + payload: btree_map, }; // Create a new Beerus light client. @@ -2212,7 +2212,7 @@ mod tests { Box::new(ethereum_lightclient_mock), Box::new(starknet_lightclient_mock), ); - beerus.node = Arc::new(RwLock::new(test_node)); + beerus.node = Arc::new(RwLock::new(node_data)); let block_id = BlockId::Number(10); @@ -2227,7 +2227,7 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let test_block_with_tx_hashes = BlockWithTxs { + let block_with_tx_hashes = BlockWithTxs { status: BlockStatus::AcceptedOnL2, block_hash: FieldElement::from_hex_be("0").unwrap(), parent_hash: FieldElement::from_hex_be("0").unwrap(), @@ -2238,7 +2238,7 @@ mod tests { transactions: Vec::new(), }; - let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); // // Set the expected return value for the Starknet light client mock. starknet_lightclient_mock @@ -2272,7 +2272,7 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let test_block_with_tx_hashes = BlockWithTxs { + let block_with_tx_hashes = BlockWithTxs { status: BlockStatus::AcceptedOnL2, block_hash: FieldElement::from_hex_be("0").unwrap(), parent_hash: FieldElement::from_hex_be("0").unwrap(), @@ -2283,7 +2283,7 @@ mod tests { transactions: Vec::new(), }; - let expected_result = MaybePendingBlockWithTxs::Block(test_block_with_tx_hashes); + let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); // // Set the expected return value for the Starknet light client mock. starknet_lightclient_mock @@ -2344,13 +2344,13 @@ mod tests { /// Test that starknet block hash and number is returned when the Starknet light client returns a value. #[tokio::test] - async fn given_normal_condition_starknet_get_block_hash_and_number_should_work() { + async fn given_normal_condition_get_block_hash_and_number_should_work() { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let block_number = 10; - let test_block_with_tx_hashes = BlockWithTxs { + let block_with_tx_hashes = BlockWithTxs { status: BlockStatus::AcceptedOnL2, block_hash: FieldElement::from_hex_be("0").unwrap(), parent_hash: FieldElement::from_hex_be("0").unwrap(), @@ -2361,13 +2361,13 @@ mod tests { transactions: Vec::new(), }; - let mut test_btree_map: BTreeMap = BTreeMap::new(); - test_btree_map.insert(block_number, test_block_with_tx_hashes); + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); - let test_node = NodeData { + let node_data = NodeData { block_number, state_root: String::from("0"), - payload: test_btree_map, + payload: btree_map, }; // Create a new Beerus light client. @@ -2377,7 +2377,7 @@ mod tests { Box::new(starknet_lightclient_mock), ); - beerus.node = Arc::new(RwLock::new(test_node)); + beerus.node = Arc::new(RwLock::new(node_data)); let res = beerus.get_block_hash_and_number().await; @@ -2387,7 +2387,7 @@ mod tests { /// Test that starknet block hash and number returns error when block number is not found in payload but Starknet light client returns a value. #[tokio::test] - async fn given_error_condition_starknet_get_block_hash_and_number_should_work() { + async fn given_error_condition_when_get_block_hash_and_number_should_work() { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); @@ -2406,6 +2406,125 @@ mod tests { assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); } + /// Test that starknet gets transaction receipt Starknet light client and Ethereum light mock returns a value. + #[tokio::test] + async fn given_normal_condition_starknet_get_transaction_receipt_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, mut ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + + let tx_receipt = TransactionReceipt::Invoke(InvokeTransactionReceipt { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + actual_fee: FieldElement::from_hex_be("10").unwrap(), + status: TransactionStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0x5678").unwrap(), + block_number: 10, + messages_sent: Vec::<_>::new(), + events: Vec::<_>::new(), + }); + + let expected_result = MaybePendingTransactionReceipt::Receipt(tx_receipt); + + ethereum_lightclient_mock + .expect_starknet_state_root() + .times(1) + .return_once(|| Ok(U256::from("1"))); + + starknet_lightclient_mock + .expect_get_transaction_receipt() + .times(1) + .return_once(|_tx_hash| Ok(expected_result)); + + let block_number = 10; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("1"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + beerus.node = Arc::new(RwLock::new(node_data)); + + let res = beerus.starknet_get_transaction_receipt(tx_hash).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets transaction receipt returns error when state root on node data is not equal to ethereum light client state root. + #[tokio::test] + async fn given_error_condition_when_starknet_get_transaction_receipt_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, mut ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + ethereum_lightclient_mock + .expect_starknet_state_root() + .times(1) + .return_once(|| Ok(U256::from("0x1234"))); + + let block_number = 10; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions: Vec::new(), + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + beerus.node = Arc::new(RwLock::new(node_data)); + + let tx_hash = String::from("0x1234"); + + let res = beerus.starknet_get_transaction_receipt(tx_hash).await; + + let expected_error = "State root mismatch"; + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 0273c854b9bcc51dd7adbd83a543cbb4fd2ac183 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Thu, 6 Jul 2023 16:12:18 +0100 Subject: [PATCH 06/12] test: added test for get_transaction_by_hash --- crates/beerus-core/tests/beerus.rs | 48 ++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 541ccd5e..891bfb3c 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -31,10 +31,11 @@ mod tests { BroadcastedInvokeTransactionV1, BroadcastedTransaction, ContractClass, DeclareTransactionResult, DeployTransactionResult, EventFilter, FeeEstimate, InvokeTransaction, InvokeTransactionReceipt, InvokeTransactionResult, - InvokeTransactionV0, LegacyContractClass, LegacyContractEntryPoint, - LegacyEntryPointsByType, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, - MaybePendingTransactionReceipt, StateDiff, StateUpdate, SyncStatusType, - Transaction as StarknetTransaction, TransactionReceipt, TransactionStatus, + InvokeTransactionV0, InvokeTransactionV1, LegacyContractClass, + LegacyContractEntryPoint, LegacyEntryPointsByType, MaybePendingBlockWithTxHashes, + MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, StateDiff, StateUpdate, + SyncStatusType, Transaction as StarknetTransaction, TransactionReceipt, + TransactionStatus, }, }; use std::{collections::BTreeMap, str::FromStr, sync::Arc}; @@ -2406,7 +2407,7 @@ mod tests { assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); } - /// Test that starknet gets transaction receipt Starknet light client and Ethereum light mock returns a value. + /// Test that starknet gets transaction receipt when Starknet light client and Ethereum light mock returns a value. #[tokio::test] async fn given_normal_condition_starknet_get_transaction_receipt_should_work() { // Mock config, ethereum light client and starknet light client. @@ -2525,6 +2526,43 @@ mod tests { assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); } + /// Test that starknet gets transaction hash when Starknet light client returns a value. + #[tokio::test] + async fn given_normal_condition_get_transaction_by_hash_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + + let expected_result = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + + starknet_lightclient_mock + .expect_get_transaction_by_hash() + .times(1) + .return_once(|_tx_hash| Ok(expected_result)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let res = beerus.get_transaction_by_hash(tx_hash).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From b049942427a6cc34284239bae04e5774dd535654 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Fri, 28 Jul 2023 21:07:59 +0100 Subject: [PATCH 07/12] fix: fixed failing tests --- crates/beerus-core/tests/beerus.rs | 31 +++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 45a3c1f1..46dd2b3b 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -1705,11 +1705,14 @@ mod tests { let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); // Set the expected return value for the Starknet light client mock. - let expected_error = "Wrong url"; + let expected_error = JsonRpcError { + code: 0, + message: "Wrong Url".to_string(), + }; starknet_lightclient_mock .expect_estimate_fee() - .return_once(move |_block_nb, _address| Err(eyre!(expected_error))); + .return_once(move |_block_nb, _address| Err(expected_error)); // Create a new Beerus light client. let beerus = BeerusLightClient::new( @@ -1734,7 +1737,6 @@ mod tests { // Assert that the result is correct. assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), expected_error); } /// Test that starknet storage value is returned when the Starknet light client returns a value. @@ -1865,8 +1867,11 @@ mod tests { .starknet_get_storage_at(address, key, &block_id) .await; + let expected_result = JsonRpcError { + code: 520, + message: "BlockId is not proven yet".to_string(), + }; assert!(res.is_err()); - let expected_result = "BlockId is not proven yet"; assert_eq!(res.unwrap_err().to_string(), expected_result.to_string()); } @@ -2369,11 +2374,14 @@ mod tests { let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); // Set the expected return value for the Starknet light client mock. - let expected_error = "Wrong url"; + let expected_error = JsonRpcError { + code: 0, + message: "Wrong Url".to_string(), + }; starknet_lightclient_mock .expect_get_block_with_txs() - .return_once(move |_block_id| Err(eyre!(expected_error))); + .return_once(move |_block_id| Err(expected_error)); // Create a new Beerus light client. let beerus = BeerusLightClient::new( @@ -2389,7 +2397,6 @@ mod tests { // Assert that the result is correct. assert!(res.is_err()); - assert_eq!(res.unwrap_err().to_string(), expected_error); } /// Test that starknet block hash and number is returned when the Starknet light client returns a value. @@ -2450,7 +2457,10 @@ mod tests { let res = beerus.get_block_hash_and_number().await; - let expected_error = "Block not found"; + let expected_error = JsonRpcError { + code: 24, + message: "Block not found".to_string(), + }; // Assert that the result is correct. assert!(res.is_err()); assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); @@ -2569,7 +2579,10 @@ mod tests { let res = beerus.starknet_get_transaction_receipt(tx_hash).await; - let expected_error = "State root mismatch"; + let expected_error = JsonRpcError { + code: 520, + message: "State root mismatch".to_string(), + }; // Assert that the result is correct. assert!(res.is_err()); assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); From 41b3a25bbc59b91e415c6ac4821344b4d6e814ec Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Mon, 14 Aug 2023 14:50:28 +0100 Subject: [PATCH 08/12] feat: added tests for get_transaction_by_block_and_index --- crates/beerus-core/tests/beerus.rs | 121 ++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 3 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index c14fbc52..6abcb065 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -20,7 +20,7 @@ mod tests { use eyre::eyre; use helios::types::{BlockTag, CallOpts, ExecutionBlock, Transactions}; - use starknet::providers::jsonrpc::JsonRpcError; + use starknet::providers::jsonrpc::{models::PendingBlockWithTxs, JsonRpcError}; use starknet::{ core::types::FieldElement, macros::selector, @@ -2631,12 +2631,12 @@ mod tests { calldata: Vec::::new(), }; - let expected_result = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let expected_transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); starknet_lightclient_mock .expect_get_transaction_by_hash() .times(1) - .return_once(|_tx_hash| Ok(expected_result)); + .return_once(|_tx_hash| Ok(expected_transaction)); // Create a new Beerus light client. let beerus = BeerusLightClient::new_from_clients( @@ -2651,6 +2651,121 @@ mod tests { assert!(res.is_ok()); } + /// Test that starknet gets transaction by block and index when Starknet light client returns a value and `MaybePendingBlockWithTxs::Block` is returned + #[tokio::test] + async fn given_normal_condition_and_block_get_transaction_by_block_and_index_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let status = BlockStatus::Pending; + let block_hash = FieldElement::from_dec_str("01").unwrap(); + let parent_hash = FieldElement::from_dec_str("01").unwrap(); + let block_number = 0; + let new_root = FieldElement::from_dec_str("01").unwrap(); + let timestamp: u64 = 0; + let sequencer_address = FieldElement::from_dec_str("01").unwrap(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block = BlockWithTxs { + status, + block_hash, + parent_hash, + block_number, + new_root, + timestamp, + sequencer_address, + transactions, + }; + + let expected_block_with_txs = MaybePendingBlockWithTxs::Block(block); + + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_block_with_txs)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(1); + let index = 0; + let res = beerus + .get_transaction_by_block_and_index(&block_id, index) + .await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets transaction by block and index when Starknet light client returns a value and `MaybePendingBlockWithTxs::PendingBlock` is returned + #[tokio::test] + async fn given_normal_condition_and_pending_block_get_transaction_by_block_and_index_should_work( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let parent_hash = FieldElement::from_dec_str("01").unwrap(); + let timestamp: u64 = 0; + let sequencer_address = FieldElement::from_dec_str("01").unwrap(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let pending_block = PendingBlockWithTxs { + timestamp, + sequencer_address, + transactions, + parent_hash, + }; + + let expected_block_with_txs = MaybePendingBlockWithTxs::PendingBlock(pending_block); + + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_block_with_txs)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(1); + let index = 0; + let res = beerus + .get_transaction_by_block_and_index(&block_id, index) + .await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 031f8cf86b7c05dc11ea26874c8e5027bc925e90 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Mon, 14 Aug 2023 14:59:28 +0100 Subject: [PATCH 09/12] feat: added tests for get_transaction_count --- crates/beerus-core/tests/beerus.rs | 114 ++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index 6abcb065..f288f1cf 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -2651,7 +2651,7 @@ mod tests { assert!(res.is_ok()); } - /// Test that starknet gets transaction by block and index when Starknet light client returns a value and `MaybePendingBlockWithTxs::Block` is returned + /// Test that starknet gets transaction by block and index when Starknet light client returns a value and when `MaybePendingBlockWithTxs::Block` is returned #[tokio::test] async fn given_normal_condition_and_block_get_transaction_by_block_and_index_should_work() { // Mock config, ethereum light client and starknet light client. @@ -2712,7 +2712,7 @@ mod tests { assert!(res.is_ok()); } - /// Test that starknet gets transaction by block and index when Starknet light client returns a value and `MaybePendingBlockWithTxs::PendingBlock` is returned + /// Test that starknet gets transaction by block and index when Starknet light client returns a value and when `MaybePendingBlockWithTxs::PendingBlock` is returned #[tokio::test] async fn given_normal_condition_and_pending_block_get_transaction_by_block_and_index_should_work( ) { @@ -2766,6 +2766,116 @@ mod tests { assert!(res.is_ok()); } + /// Test that starknet gets transaction count when Starknet light client returns a value and when `MaybePendingBlockWithTxs::Block` is returned + #[tokio::test] + async fn given_normal_condition_and_block_get_transaction_count_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let status = BlockStatus::Pending; + let block_hash = FieldElement::from_dec_str("01").unwrap(); + let parent_hash = FieldElement::from_dec_str("01").unwrap(); + let block_number = 0; + let new_root = FieldElement::from_dec_str("01").unwrap(); + let timestamp: u64 = 0; + let sequencer_address = FieldElement::from_dec_str("01").unwrap(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block = BlockWithTxs { + status, + block_hash, + parent_hash, + block_number, + new_root, + timestamp, + sequencer_address, + transactions, + }; + + let expected_block_with_txs = MaybePendingBlockWithTxs::Block(block); + + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_block_with_txs)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(1); + let res = beerus.get_block_transaction_count(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + assert_eq!(res.unwrap(), 1); + } + + /// Test that starknet gets transaction by block and index when Starknet light client returns a value and when `MaybePendingBlockWithTxs::PendingBlock` is returned + #[tokio::test] + async fn given_normal_condition_and_pending_block_get_transaction_count_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); + + let parent_hash = FieldElement::from_dec_str("01").unwrap(); + let timestamp: u64 = 0; + let sequencer_address = FieldElement::from_dec_str("01").unwrap(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let pending_block = PendingBlockWithTxs { + timestamp, + sequencer_address, + transactions, + parent_hash, + }; + + let expected_block_with_txs = MaybePendingBlockWithTxs::PendingBlock(pending_block); + + starknet_lightclient_mock + .expect_get_block_with_txs() + .times(1) + .return_once(|_block_id| Ok(expected_block_with_txs)); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_id = BlockId::Number(1); + let res = beerus.get_block_transaction_count(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + assert_eq!(res.unwrap(), 1); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 82c8e347fc5adacd99ff4158640bf359cb48d1ab Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Mon, 14 Aug 2023 15:02:28 +0100 Subject: [PATCH 10/12] fix: fixed typo --- crates/beerus-core/tests/beerus.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index f288f1cf..ed672ea6 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -2766,9 +2766,9 @@ mod tests { assert!(res.is_ok()); } - /// Test that starknet gets transaction count when Starknet light client returns a value and when `MaybePendingBlockWithTxs::Block` is returned + /// Test that starknet gets block transaction count when Starknet light client returns a value and when `MaybePendingBlockWithTxs::Block` is returned #[tokio::test] - async fn given_normal_condition_and_block_get_transaction_count_should_work() { + async fn given_normal_condition_and_block_get_block_transaction_count_should_work() { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); @@ -2825,9 +2825,9 @@ mod tests { assert_eq!(res.unwrap(), 1); } - /// Test that starknet gets transaction by block and index when Starknet light client returns a value and when `MaybePendingBlockWithTxs::PendingBlock` is returned + /// Test that starknet gets block transaction count when Starknet light client returns a value and when `MaybePendingBlockWithTxs::PendingBlock` is returned #[tokio::test] - async fn given_normal_condition_and_pending_block_get_transaction_count_should_work() { + async fn given_normal_condition_and_pending_block_get_block_transaction_count_should_work() { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); From 981745757a9f588653a1e297f5aef7404f9167b8 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Tue, 15 Aug 2023 19:34:59 +0100 Subject: [PATCH 11/12] feat: added test for get_block_with_tx_hashes --- crates/beerus-core/tests/beerus.rs | 503 ++++++++++++++++++++++++++++- 1 file changed, 496 insertions(+), 7 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index ed672ea6..d8e25dd5 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -20,7 +20,7 @@ mod tests { use eyre::eyre; use helios::types::{BlockTag, CallOpts, ExecutionBlock, Transactions}; - use starknet::providers::jsonrpc::{models::PendingBlockWithTxs, JsonRpcError}; + use starknet::providers::jsonrpc::JsonRpcError; use starknet::{ core::types::FieldElement, macros::selector, @@ -30,13 +30,15 @@ mod tests { BroadcastedDeclareTransactionV1, BroadcastedDeployTransaction, BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV0, BroadcastedInvokeTransactionV1, BroadcastedTransaction, ContractClass, - DeclareTransactionResult, DeployTransactionResult, EventFilter, FeeEstimate, - InvokeTransaction, InvokeTransactionReceipt, InvokeTransactionResult, - InvokeTransactionV0, InvokeTransactionV1, LegacyContractClass, + DeclareTransaction, DeclareTransactionResult, DeclareTransactionV1, + DeclareTransactionV2, DeployAccountTransaction, DeployTransaction, + DeployTransactionResult, EventFilter, FeeEstimate, InvokeTransaction, + InvokeTransactionReceipt, InvokeTransactionResult, InvokeTransactionV0, + InvokeTransactionV1, L1HandlerTransaction, LegacyContractClass, LegacyContractEntryPoint, LegacyEntryPointsByType, MaybePendingBlockWithTxHashes, - MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, StateDiff, StateUpdate, - SyncStatusType, Transaction as StarknetTransaction, TransactionReceipt, - TransactionStatus, + MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, PendingBlockWithTxs, + StateDiff, StateUpdate, SyncStatusType, Transaction as StarknetTransaction, + TransactionReceipt, TransactionStatus, }, }; use std::{collections::BTreeMap, str::FromStr, sync::Arc}; @@ -2876,6 +2878,493 @@ mod tests { assert_eq!(res.unwrap(), 1); } + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and `block_id` is a number + #[tokio::test] + async fn given_normal_condition_and_block_id_is_number_get_block_with_tx_hashes_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block_number = 10; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Number(10); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and `block_id` is a hash + #[tokio::test] + async fn given_normal_condition_and_block_id_is_hash_get_block_with_tx_hashes_should_work() { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block_number = 1; + let block_hash = FieldElement::from_hex_be("0xff").unwrap(); + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash, + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Hash(block_hash); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and `block_id` is a hash and hash not found + #[tokio::test] + async fn given_normal_condition_and_block_id_is_hash_and_hash_not_found_then_get_block_with_tx_hashes_should_return_error( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_hash = FieldElement::from_hex_be("0xff").unwrap(); + let block_id = BlockId::Hash(block_hash); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + let expected_error = JsonRpcError { + code: 24, + message: format!("Block with hash {} not found in the payload.", block_hash), + }; + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and `block_id` is a latest tag + #[tokio::test] + async fn given_normal_condition_and_block_id_is_latest_tag_then_get_block_with_tx_hashes_should_work( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block_number = 1; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Tag(StarknetBlockTag::Latest); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and `block_id` is a pending tag + #[tokio::test] + async fn given_normal_condition_and_block_id_is_pending_tag_then_get_block_with_tx_hashes_should_work( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block_number = 1; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::Pending, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Tag(StarknetBlockTag::Pending); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value, `block_id` is a pending tag and pending tag not found + #[tokio::test] + async fn given_normal_condition_and_block_id_is_pending_tag_and_pending_tag_not_found_then_get_block_with_tx_hashes_should_return_error( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let tx_hash = String::from("0x1234"); + let invoke_tx_v1 = InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + }; + let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); + let transactions = vec![transaction]; + + let block_number = 1; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash: FieldElement::from_hex_be("0").unwrap(), + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Tag(StarknetBlockTag::Pending); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + let expected_error = JsonRpcError { + code: 24, + message: "Block with pending status not found in the payload.".to_string(), + }; + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value and all transaction are contained in block + #[tokio::test] + async fn given_normal_condition_and_block_contains_all_transaction_types_then_get_block_with_tx_hashes_should_work( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + let hash = String::from("0x1234"); + let transaction_hash = FieldElement::from_hex_be(&hash).unwrap(); + let max_fee = FieldElement::from_hex_be("0").unwrap(); + let signature = Vec::::new(); + let nonce = FieldElement::from_hex_be("0").unwrap(); + let contract_address = FieldElement::from_hex_be(&hash).unwrap(); + let sender_address = FieldElement::from_hex_be(&hash).unwrap(); + let entry_point_selector = FieldElement::from_hex_be(&hash).unwrap(); + let class_hash = FieldElement::from_hex_be(&hash).unwrap(); + let block_hash = FieldElement::from_hex_be(&hash).unwrap(); + let parent_hash = FieldElement::from_hex_be(&hash).unwrap(); + let new_root = FieldElement::from_hex_be(&hash).unwrap(); + let sequencer_address = FieldElement::from_hex_be(&hash).unwrap(); + let compiled_class_hash = FieldElement::from_hex_be(&hash).unwrap(); + let calldata = Vec::::new(); + let contract_address_salt = FieldElement::from_hex_be(&hash).unwrap(); + let constructor_calldata = Vec::::new(); + let version = 1; + let block_number = 1; + + // Invoke Transaction V0 + let invoke_tx_v0 = + StarknetTransaction::Invoke(InvokeTransaction::V0(InvokeTransactionV0 { + transaction_hash, + max_fee, + signature: signature.clone(), + nonce, + contract_address, + entry_point_selector, + calldata: calldata.clone(), + })); + + // Invoke Transaction V1 + let invoke_tx_v1 = + StarknetTransaction::Invoke(InvokeTransaction::V1(InvokeTransactionV1 { + transaction_hash, + max_fee, + signature: signature.clone(), + nonce, + sender_address, + calldata: calldata.clone(), + })); + + // L1 Handler Transaction + let l1_handler_tx = StarknetTransaction::L1Handler(L1HandlerTransaction { + transaction_hash, + version, + nonce: 1, + contract_address, + entry_point_selector, + calldata, + }); + + // Declare Transaction V1 + let declare_v1_tx = + StarknetTransaction::Declare(DeclareTransaction::V1(DeclareTransactionV1 { + transaction_hash, + max_fee, + signature: signature.clone(), + nonce, + class_hash, + sender_address, + })); + + // Declare Transaction V2 + let declare_v2_tx = + StarknetTransaction::Declare(DeclareTransaction::V2(DeclareTransactionV2 { + transaction_hash, + max_fee, + signature: signature.clone(), + nonce, + class_hash, + compiled_class_hash, + sender_address, + })); + + // Deploy Transaction + let deploy_tx = StarknetTransaction::Deploy(DeployTransaction { + transaction_hash, + class_hash, + version, + contract_address_salt, + constructor_calldata: constructor_calldata.clone(), + }); + + // Deploy Account Transaction + let deploy_account_tx = StarknetTransaction::DeployAccount(DeployAccountTransaction { + transaction_hash, + max_fee, + version, + signature, + nonce, + contract_address_salt, + constructor_calldata, + class_hash, + }); + + let transactions = vec![ + invoke_tx_v0, + invoke_tx_v1, + l1_handler_tx, + declare_v1_tx, + declare_v2_tx, + deploy_tx, + deploy_account_tx, + ]; + + let block_with_tx_hashes = BlockWithTxs { + status: BlockStatus::AcceptedOnL2, + block_hash, + parent_hash, + block_number, + new_root, + timestamp: 10, + sequencer_address, + transactions, + }; + + let mut btree_map: BTreeMap = BTreeMap::new(); + btree_map.insert(block_number, block_with_tx_hashes); + + let node_data = NodeData { + block_number, + state_root: String::from("0x5678"), + payload: btree_map, + }; + + // Create a new Beerus light client. + let mut beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + beerus.node = Arc::new(RwLock::new(node_data)); + + let block_id = BlockId::Number(block_number); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + // Assert that the result is correct. + assert!(res.is_ok()); + } + + /// Test that starknet gets block with transaction hashes when Starknet light client returns a value, `block_id` is a number and block number not found + #[tokio::test] + async fn given_normal_condition_and_block_id_is_number_and_number_not_found_then_get_block_with_tx_hashes_should_return_error( + ) { + // Mock config, ethereum light client and starknet light client. + let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + + // Create a new Beerus light client. + let beerus = BeerusLightClient::new_from_clients( + config, + Box::new(ethereum_lightclient_mock), + Box::new(starknet_lightclient_mock), + ); + + let block_number = 1; + let block_id = BlockId::Number(block_number); + let res = beerus.get_block_with_tx_hashes(&block_id).await; + + let expected_error = JsonRpcError { + code: 24, + message: "Error while retrieving block.".to_string(), + }; + // Assert that the result is correct. + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), expected_error.to_string()); + } + /// Test the `block_number` method when everything is fine. /// This test mocks external dependencies. /// It does not test the `block_number` method of the external dependencies. From 7cc8c8df1afa9199242ae5e491fb262278a66055 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Thu, 17 Aug 2023 08:48:31 +0100 Subject: [PATCH 12/12] feat: added mock functions --- crates/beerus-core/tests/beerus.rs | 364 ++++++------------------- crates/beerus-core/tests/common/mod.rs | 48 ++++ 2 files changed, 134 insertions(+), 278 deletions(-) diff --git a/crates/beerus-core/tests/beerus.rs b/crates/beerus-core/tests/beerus.rs index d8e25dd5..703957e0 100644 --- a/crates/beerus-core/tests/beerus.rs +++ b/crates/beerus-core/tests/beerus.rs @@ -1,7 +1,7 @@ #![cfg(not(target_arch = "wasm32"))] pub mod common; -use common::mock_clients; +use common::{mock_block_with_txs, mock_broadcasted_transaction, mock_clients, mock_invoke_tx_v1}; #[cfg(test)] mod tests { @@ -28,17 +28,16 @@ mod tests { BlockHashAndNumber, BlockId, BlockStatus, BlockTag as StarknetBlockTag, BlockWithTxHashes, BlockWithTxs, BroadcastedDeclareTransaction, BroadcastedDeclareTransactionV1, BroadcastedDeployTransaction, - BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV0, - BroadcastedInvokeTransactionV1, BroadcastedTransaction, ContractClass, + BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV0, ContractClass, DeclareTransaction, DeclareTransactionResult, DeclareTransactionV1, DeclareTransactionV2, DeployAccountTransaction, DeployTransaction, DeployTransactionResult, EventFilter, FeeEstimate, InvokeTransaction, InvokeTransactionReceipt, InvokeTransactionResult, InvokeTransactionV0, - InvokeTransactionV1, L1HandlerTransaction, LegacyContractClass, - LegacyContractEntryPoint, LegacyEntryPointsByType, MaybePendingBlockWithTxHashes, - MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, PendingBlockWithTxs, - StateDiff, StateUpdate, SyncStatusType, Transaction as StarknetTransaction, - TransactionReceipt, TransactionStatus, + L1HandlerTransaction, LegacyContractClass, LegacyContractEntryPoint, + LegacyEntryPointsByType, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, + MaybePendingTransactionReceipt, PendingBlockWithTxs, StateDiff, StateUpdate, + SyncStatusType, Transaction as StarknetTransaction, TransactionReceipt, + TransactionStatus, }, }; use std::{collections::BTreeMap, str::FromStr, sync::Arc}; @@ -1707,15 +1706,7 @@ mod tests { Box::new(starknet_lightclient_mock), ); - let request = BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction::V1( - BroadcastedInvokeTransactionV1 { - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0").unwrap(), - calldata: Vec::::new(), - }, - )); + let request = mock_broadcasted_transaction(); let block_id = BlockId::Number(10); // Perform the test estimate. @@ -1749,15 +1740,7 @@ mod tests { Box::new(starknet_lightclient_mock), ); - let request = BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction::V1( - BroadcastedInvokeTransactionV1 { - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0").unwrap(), - calldata: Vec::::new(), - }, - )); + let request = mock_broadcasted_transaction(); let block_id = BlockId::Number(10); // Perform the test estimate. @@ -2227,16 +2210,13 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number: 10, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); + let block_number = 10; + let transactions = Vec::::new(); + + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); @@ -2267,18 +2247,13 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); let block_number = 10; + let transactions = Vec::::new(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -2310,16 +2285,13 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number: 10, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); + let block_number = 10; + let transactions = Vec::::new(); + + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); @@ -2355,16 +2327,13 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, mut starknet_lightclient_mock) = mock_clients(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number: 10, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); + let block_number = 10; + let transactions = Vec::::new(); + + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let expected_result = MaybePendingBlockWithTxs::Block(block_with_tx_hashes); @@ -2433,18 +2402,13 @@ mod tests { // Mock config, ethereum light client and starknet light client. let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); let block_number = 10; + let transactions = Vec::::new(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -2524,18 +2488,13 @@ mod tests { .times(1) .return_once(|_tx_hash| Ok(expected_result)); + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); let block_number = 10; + let transactions = Vec::::new(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -2572,18 +2531,13 @@ mod tests { .times(1) .return_once(|| Ok(U256::from("0x1234"))); + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); let block_number = 10; + let transactions = Vec::::new(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions: Vec::new(), - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -2624,14 +2578,7 @@ mod tests { let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash.clone()); let expected_transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); @@ -2661,36 +2608,15 @@ mod tests { let status = BlockStatus::Pending; let block_hash = FieldElement::from_dec_str("01").unwrap(); - let parent_hash = FieldElement::from_dec_str("01").unwrap(); let block_number = 0; - let new_root = FieldElement::from_dec_str("01").unwrap(); - let timestamp: u64 = 0; - let sequencer_address = FieldElement::from_dec_str("01").unwrap(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; - let block = BlockWithTxs { - status, - block_hash, - parent_hash, - block_number, - new_root, - timestamp, - sequencer_address, - transactions, - }; - - let expected_block_with_txs = MaybePendingBlockWithTxs::Block(block); + let block_with_txs = mock_block_with_txs(transactions, block_number, status, block_hash); + let expected_block_with_txs = MaybePendingBlockWithTxs::Block(block_with_txs); starknet_lightclient_mock .expect_get_block_with_txs() @@ -2726,14 +2652,7 @@ mod tests { let sequencer_address = FieldElement::from_dec_str("01").unwrap(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; @@ -2776,35 +2695,14 @@ mod tests { let status = BlockStatus::Pending; let block_hash = FieldElement::from_dec_str("01").unwrap(); - let parent_hash = FieldElement::from_dec_str("01").unwrap(); let block_number = 0; - let new_root = FieldElement::from_dec_str("01").unwrap(); - let timestamp: u64 = 0; - let sequencer_address = FieldElement::from_dec_str("01").unwrap(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; - let block = BlockWithTxs { - status, - block_hash, - parent_hash, - block_number, - new_root, - timestamp, - sequencer_address, - transactions, - }; - + let block = mock_block_with_txs(transactions, block_number, status, block_hash); let expected_block_with_txs = MaybePendingBlockWithTxs::Block(block); starknet_lightclient_mock @@ -2838,14 +2736,7 @@ mod tests { let sequencer_address = FieldElement::from_dec_str("01").unwrap(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; @@ -2885,29 +2776,16 @@ mod tests { let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; let block_number = 10; + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0x").unwrap(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -2940,30 +2818,16 @@ mod tests { let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; let block_number = 1; let block_hash = FieldElement::from_hex_be("0xff").unwrap(); + let status = BlockStatus::AcceptedOnL2; - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash, - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -3024,29 +2888,16 @@ mod tests { let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; let block_number = 1; + let block_hash = FieldElement::from_hex_be("0x").unwrap(); + let status = BlockStatus::AcceptedOnL2; - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -3080,29 +2931,16 @@ mod tests { let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; let block_number = 1; + let status = BlockStatus::Pending; + let block_hash = FieldElement::from_hex_be("0").unwrap(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::Pending, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -3136,29 +2974,16 @@ mod tests { let (config, ethereum_lightclient_mock, starknet_lightclient_mock) = mock_clients(); let tx_hash = String::from("0x1234"); - let invoke_tx_v1 = InvokeTransactionV1 { - transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), - max_fee: FieldElement::from_hex_be("0").unwrap(), - signature: Vec::::new(), - nonce: FieldElement::from_hex_be("0").unwrap(), - sender_address: FieldElement::from_hex_be("0x").unwrap(), - calldata: Vec::::new(), - }; + let invoke_tx_v1 = mock_invoke_tx_v1(tx_hash); let transaction = StarknetTransaction::Invoke(InvokeTransaction::V1(invoke_tx_v1)); let transactions = vec![transaction]; let block_number = 1; + let status = BlockStatus::AcceptedOnL2; + let block_hash = FieldElement::from_hex_be("0").unwrap(); - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash: FieldElement::from_hex_be("0").unwrap(), - parent_hash: FieldElement::from_hex_be("0").unwrap(), - block_number, - new_root: FieldElement::from_hex_be("0").unwrap(), - timestamp: 10, - sequencer_address: FieldElement::from_hex_be("0").unwrap(), - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); @@ -3206,15 +3031,13 @@ mod tests { let entry_point_selector = FieldElement::from_hex_be(&hash).unwrap(); let class_hash = FieldElement::from_hex_be(&hash).unwrap(); let block_hash = FieldElement::from_hex_be(&hash).unwrap(); - let parent_hash = FieldElement::from_hex_be(&hash).unwrap(); - let new_root = FieldElement::from_hex_be(&hash).unwrap(); - let sequencer_address = FieldElement::from_hex_be(&hash).unwrap(); let compiled_class_hash = FieldElement::from_hex_be(&hash).unwrap(); let calldata = Vec::::new(); let contract_address_salt = FieldElement::from_hex_be(&hash).unwrap(); let constructor_calldata = Vec::::new(); let version = 1; let block_number = 1; + let status = BlockStatus::AcceptedOnL2; // Invoke Transaction V0 let invoke_tx_v0 = @@ -3230,14 +3053,7 @@ mod tests { // Invoke Transaction V1 let invoke_tx_v1 = - StarknetTransaction::Invoke(InvokeTransaction::V1(InvokeTransactionV1 { - transaction_hash, - max_fee, - signature: signature.clone(), - nonce, - sender_address, - calldata: calldata.clone(), - })); + StarknetTransaction::Invoke(InvokeTransaction::V1(mock_invoke_tx_v1(hash))); // L1 Handler Transaction let l1_handler_tx = StarknetTransaction::L1Handler(L1HandlerTransaction { @@ -3303,16 +3119,8 @@ mod tests { deploy_account_tx, ]; - let block_with_tx_hashes = BlockWithTxs { - status: BlockStatus::AcceptedOnL2, - block_hash, - parent_hash, - block_number, - new_root, - timestamp: 10, - sequencer_address, - transactions, - }; + let block_with_tx_hashes = + mock_block_with_txs(transactions, block_number, status, block_hash); let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert(block_number, block_with_tx_hashes); diff --git a/crates/beerus-core/tests/common/mod.rs b/crates/beerus-core/tests/common/mod.rs index 9f37667a..263ac81e 100644 --- a/crates/beerus-core/tests/common/mod.rs +++ b/crates/beerus-core/tests/common/mod.rs @@ -10,6 +10,13 @@ use ethers::types::Address; use httpmock::{prelude::*, Mock}; use serde::{Deserialize, Serialize}; use serde_json::json; +use starknet::{ + core::types::FieldElement, + providers::jsonrpc::models::{ + BlockStatus, BlockWithTxs, BroadcastedInvokeTransaction, BroadcastedInvokeTransactionV1, + BroadcastedTransaction, InvokeTransactionV1, Transaction, + }, +}; use std::fs; use std::net::SocketAddr; use std::path::PathBuf; @@ -23,6 +30,47 @@ pub fn mock_clients() -> (Config, MockEthereumLightClient, MockStarkNetLightClie ) } +pub fn mock_invoke_tx_v1(tx_hash: String) -> InvokeTransactionV1 { + InvokeTransactionV1 { + transaction_hash: FieldElement::from_hex_be(&tx_hash).unwrap(), + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0x").unwrap(), + calldata: Vec::::new(), + } +} + +pub fn mock_broadcasted_transaction() -> BroadcastedTransaction { + BroadcastedTransaction::Invoke(BroadcastedInvokeTransaction::V1( + BroadcastedInvokeTransactionV1 { + max_fee: FieldElement::from_hex_be("0").unwrap(), + signature: Vec::::new(), + nonce: FieldElement::from_hex_be("0").unwrap(), + sender_address: FieldElement::from_hex_be("0").unwrap(), + calldata: Vec::::new(), + }, + )) +} + +pub fn mock_block_with_txs( + transactions: Vec, + block_number: u64, + status: BlockStatus, + block_hash: FieldElement, +) -> BlockWithTxs { + BlockWithTxs { + status, + block_hash, + parent_hash: FieldElement::from_hex_be("0").unwrap(), + block_number, + new_root: FieldElement::from_hex_be("0").unwrap(), + timestamp: 10, + sequencer_address: FieldElement::from_hex_be("0").unwrap(), + transactions, + } +} + pub fn mock_get_contract_storage_proof(server: &MockServer) -> (Mock, GetProofOutput) { let path = "tests/common/data/data.json"; let s = fs::read_to_string(path).unwrap();