From 6b4725f0d8eb0a7739cfece2b25cb3b829ab8df3 Mon Sep 17 00:00:00 2001 From: Godspower Eze <61994334+Godspower-Eze@users.noreply.github.com> Date: Thu, 29 Jun 2023 23:20:43 +0100 Subject: [PATCH] test: added two test cases for get_storage_at (#500) --- 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(