Skip to content

Commit

Permalink
test: added two test cases for get_storage_at (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godspower-Eze committed Jun 29, 2023
1 parent 0f3ed93 commit 6b4725f
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions crates/beerus-core/tests/beerus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 6b4725f

Please sign in to comment.