From 00d4787e827bb96508454592932f8829f33d0e4a Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Wed, 28 Jun 2023 23:44:31 +0100 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 881ce0cfe877459acaad947415308072e03142b5 Mon Sep 17 00:00:00 2001 From: Godspower Eze Date: Thu, 3 Aug 2023 15:54:02 +0100 Subject: [PATCH 8/8] feat: moved ethers_helpers tests to its own test file --- crates/beerus-core/src/ethers_helper.rs | 123 --------------------- crates/beerus-core/tests/ethers_helper.rs | 129 ++++++++++++++++++++++ 2 files changed, 129 insertions(+), 123 deletions(-) create mode 100644 crates/beerus-core/tests/ethers_helper.rs diff --git a/crates/beerus-core/src/ethers_helper.rs b/crates/beerus-core/src/ethers_helper.rs index b8123060..4adead7b 100644 --- a/crates/beerus-core/src/ethers_helper.rs +++ b/crates/beerus-core/src/ethers_helper.rs @@ -99,126 +99,3 @@ pub fn block_tag_eq(a: &BlockTag, b: &BlockTag) -> bool { _ => false, } } - -#[cfg(test)] -mod tests { - use super::block_tag_eq; - use ethers::types::Bytes; - use helios::types::BlockTag; - use std::str::FromStr; - - #[test] - fn test_encode_function_data() { - let abi: ethers::abi::Abi = serde_json::from_str( - r#" - [ - { - "inputs": [ - { - "internalType": "uint256", - "name": "x", - "type": "uint256" - } - ], - "name": "foo", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - } - ] - "#, - ) - .unwrap(); - let args = 42u64; - let function_name = "foo"; - let encoded = super::encode_function_data(args, abi, function_name).unwrap(); - assert_eq!( - encoded, - Bytes::from_str( - "0x2fbebd38000000000000000000000000000000000000000000000000000000000000002a" - ) - .unwrap() - ); - } - - #[test] - fn test_u256_to_bytes32_slice() { - let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into(); - let bytes = super::u256_to_bytes32_slice(value); - assert_eq!( - bytes, - [ - 73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241, - 76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35 - ] - ); - } - - #[test] - fn test_u256_to_bytes32_type() { - let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into(); - let token = super::u256_to_bytes32_type(value); - assert_eq!( - token, - ethers::abi::Token::FixedBytes(vec![ - 73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241, - 76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35 - ]) - ); - } - - #[test] - fn test_block_string_to_block_tag_type() { - // Testing for Number type - // Given - let block = "123".to_string(); - - // When - let result = super::block_string_to_block_tag_type(&block).unwrap(); - - // Then - let expected_result = BlockTag::Number(123); - let equal = block_tag_eq(&result, &expected_result); - assert_eq!(equal, true); - - // Testing for Latest type - // Given - let block = "latest".to_string(); - - // When - let result = super::block_string_to_block_tag_type(&block).unwrap(); - - // Then - let expected_result = BlockTag::Latest; - let equal = block_tag_eq(&result, &expected_result); - assert_eq!(equal, true); - - // Testing for Finalized type - // Given - let block = "finalized".to_string(); - - // When - let result = super::block_string_to_block_tag_type(&block).unwrap(); - - // Then - let expected_result = BlockTag::Finalized; - let equal = block_tag_eq(&result, &expected_result); - assert_eq!(equal, true); - } - - #[test] - fn test_invalid_block_should_return_error() { - // Testing for invalid type - // Given - let block = "0x123".to_string(); - - // When - let result = super::block_string_to_block_tag_type(&block); - - // Then - match result { - Err(e) => assert_eq!("Invalid BlockTag", e.to_string()), - Ok(_) => panic!("Expected error, got ok"), - } - } -} diff --git a/crates/beerus-core/tests/ethers_helper.rs b/crates/beerus-core/tests/ethers_helper.rs new file mode 100644 index 00000000..8c5b5beb --- /dev/null +++ b/crates/beerus-core/tests/ethers_helper.rs @@ -0,0 +1,129 @@ +#![cfg(not(target_arch = "wasm32"))] + +use core::str::FromStr; + +mod tests { + + use super::*; + use beerus_core::ethers_helper::{ + block_string_to_block_tag_type, block_tag_eq, encode_function_data, u256_to_bytes32_slice, + u256_to_bytes32_type, + }; + use ethers::{abi::Abi, types::Bytes}; + use helios::types::BlockTag; + + #[test] + fn test_encode_function_data() { + let abi: Abi = serde_json::from_str( + r#" + [ + { + "inputs": [ + { + "internalType": "uint256", + "name": "x", + "type": "uint256" + } + ], + "name": "foo", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } + ] + "#, + ) + .unwrap(); + let args = 42u64; + let function_name = "foo"; + let encoded = encode_function_data(args, abi, function_name).unwrap(); + assert_eq!( + encoded, + Bytes::from_str( + "0x2fbebd38000000000000000000000000000000000000000000000000000000000000002a" + ) + .unwrap() + ); + } + + #[test] + fn test_u256_to_bytes32_slice() { + let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into(); + let bytes = u256_to_bytes32_slice(value); + assert_eq!( + bytes, + [ + 73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241, + 76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35 + ] + ); + } + + #[test] + fn test_u256_to_bytes32_type() { + let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into(); + let token = u256_to_bytes32_type(value); + assert_eq!( + token, + ethers::abi::Token::FixedBytes(vec![ + 73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241, + 76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35 + ]) + ); + } + + #[test] + fn test_block_string_to_block_tag_type() { + // Testing for Number type + // Given + let block = "123".to_string(); + + // When + let result = block_string_to_block_tag_type(&block).unwrap(); + + // Then + let expected_result = BlockTag::Number(123); + let equal = block_tag_eq(&result, &expected_result); + assert_eq!(equal, true); + + // Testing for Latest type + // Given + let block = "latest".to_string(); + + // When + let result = block_string_to_block_tag_type(&block).unwrap(); + + // Then + let expected_result = BlockTag::Latest; + let equal = block_tag_eq(&result, &expected_result); + assert_eq!(equal, true); + + // Testing for Finalized type + // Given + let block = "finalized".to_string(); + + // When + let result = block_string_to_block_tag_type(&block).unwrap(); + + // Then + let expected_result = BlockTag::Finalized; + let equal = block_tag_eq(&result, &expected_result); + assert_eq!(equal, true); + } + + #[test] + fn test_invalid_block_should_return_error() { + // Testing for invalid type + // Given + let block = "0x123".to_string(); + + // When + let result = block_string_to_block_tag_type(&block); + + // Then + match result { + Err(e) => assert_eq!("Invalid BlockTag", e.to_string()), + Ok(_) => panic!("Expected error, got ok"), + } + } +}