Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(starknet_state_sync): add unit tests for state sync read methods #2886

Merged
merged 1 commit into from
Dec 30, 2024

Conversation

noamsp-starkware
Copy link
Contributor

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Contributor Author

noamsp-starkware commented Dec 22, 2024

@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_read_methods_unit_tests branch from 34af6cb to d80a214 Compare December 22, 2024 18:50
@starkware-libs starkware-libs deleted a comment from graphite-app bot Dec 22, 2024
@noamsp-starkware noamsp-starkware marked this pull request as ready for review December 22, 2024 18:55
Copy link
Contributor

@ShahakShama ShahakShama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4 of 5 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 8 unresolved discussions (waiting on @noamsp-starkware)


crates/starknet_state_sync/src/test.rs line 23 at r2 (raw file):

use crate::StateSync;

fn get_mock_state_sync_and_storage_writer() -> (StateSync, StorageWriter) {

Why is this mock? It's the real thing just without the new_block_sender
I'd rename this function to setup


crates/starknet_state_sync/src/test.rs line 40 at r2 (raw file):

        .append_header(header.block_header_without_hash.block_number, &header)
        .unwrap()
        .append_state_diff(header.block_header_without_hash.block_number, ThinStateDiff::default())

Put a non default ThinStateDiff. You can use get_test_instance. It makes for a more interesting test


crates/starknet_state_sync/src/test.rs line 49 at r2 (raw file):

    // Verify that the block that was written is returned correctly.
    let value =
        state_sync.get_block(header.block_header_without_hash.block_number).unwrap().unwrap();

I prefer using the external API: state_sync.handle_request(StateSyncRequest::GetBlock(...))
Same across the entire PR


crates/starknet_state_sync/src/test.rs line 56 at r2 (raw file):

    // Verify we get Ok(None) when trying to call get_block with an unknown block number.
    let result = state_sync

I usually prefer doing separate test cases in separate tests. This way you put these comments in the test title
In most cases here you don't need any setup for the special case. You can just use an empty storage with any block number to test this case

Same across this PR


crates/starknet_state_sync/src/test.rs line 68 at r2 (raw file):

    let mut rng = get_rng();
    let expected_address_u64 = rng.next_u64();
    let expected_address = ContractAddress::from(expected_address_u64);

expected_* is usually used for the expected return value. address is an argument and not a return value. You can just call it address

Same across this PR


crates/starknet_state_sync/src/test.rs line 94 at r2 (raw file):

    // number.
    let result = state_sync.get_storage_at(
        header.block_header_without_hash.block_number.unchecked_next(),

Consider keeping this in a var called wrong_block_number or non_existing_block_number or other_block_number and use it here and in the assert


crates/starknet_state_sync/src/test.rs line 123 at r2 (raw file):

    let expected_address_u64 = rng.next_u64();
    let expected_address = ContractAddress::from(expected_address_u64);
    let expected_value = Nonce::get_test_instance(&mut rng);

value -> nonce


crates/starknet_state_sync/src/test.rs line 172 at r2 (raw file):

    let expected_address_u64 = rng.next_u64();
    let expected_address = ContractAddress::from(expected_address_u64);
    let expected_value = ClassHash::get_test_instance(&mut rng);

value -> class_hash


crates/starknet_state_sync/src/test.rs line 215 at r2 (raw file):

#[tokio::test]
async fn test_get_compiled_class_deprecated() {

You can avoid updating this test according to my comments since it will be erased soon

@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_read_methods_unit_tests branch from d80a214 to 514b33a Compare December 23, 2024 18:14
Copy link
Contributor Author

@noamsp-starkware noamsp-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 4 of 5 files reviewed, 7 unresolved discussions (waiting on @ShahakShama)


crates/starknet_state_sync/src/test.rs line 23 at r2 (raw file):

Previously, ShahakShama wrote…

Why is this mock? It's the real thing just without the new_block_sender
I'd rename this function to setup

Missed this, its from previous trials when writing the file. fixing


crates/starknet_state_sync/src/test.rs line 40 at r2 (raw file):

Previously, ShahakShama wrote…

Put a non default ThinStateDiff. You can use get_test_instance. It makes for a more interesting test

Done.


crates/starknet_state_sync/src/test.rs line 49 at r2 (raw file):

Previously, ShahakShama wrote…

I prefer using the external API: state_sync.handle_request(StateSyncRequest::GetBlock(...))
Same across the entire PR

Done.


crates/starknet_state_sync/src/test.rs line 56 at r2 (raw file):

Previously, ShahakShama wrote…

I usually prefer doing separate test cases in separate tests. This way you put these comments in the test title
In most cases here you don't need any setup for the special case. You can just use an empty storage with any block number to test this case

Same across this PR

Done.


crates/starknet_state_sync/src/test.rs line 68 at r2 (raw file):

Previously, ShahakShama wrote…

expected_* is usually used for the expected return value. address is an argument and not a return value. You can just call it address

Same across this PR

Done.


crates/starknet_state_sync/src/test.rs line 123 at r2 (raw file):

Previously, ShahakShama wrote…

value -> nonce

Done.


crates/starknet_state_sync/src/test.rs line 172 at r2 (raw file):

Previously, ShahakShama wrote…

value -> class_hash

Done.

Copy link
Contributor

@ShahakShama ShahakShama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed (commit messages unreviewed), 7 unresolved discussions (waiting on @noamsp-starkware)


crates/starknet_state_sync/src/test.rs line 65 at r3 (raw file):

        }
        StateSyncResponse::GetBlock(Ok(None)) => {
            panic!("No block returned");

No need to differentiate this case if you accept my suggestions below.
Same across the entire PR


crates/starknet_state_sync/src/test.rs line 68 at r3 (raw file):

        }
        _ => {
            panic!("Expected StateSyncResponse::GetBlock::Ok");

display the response you actually got inside the panic message.
Same across the entire PR


crates/starknet_state_sync/src/test.rs line 68 at r3 (raw file):

        }
        _ => {
            panic!("Expected StateSyncResponse::GetBlock::Ok");

If you apply the 2 suggestions above, change StateSyncResponse::GetBlock::Ok to StateSyncResponse::GetBlock::Ok(Some(_))
Same across the entire PR


crates/starknet_state_sync/src/test.rs line 252 at r3 (raw file):

async fn test_block_not_found() {
    let (mut state_sync, _) = setup();
    let wrong_block_number = BlockNumber(0);

consider non_existing_block_number


crates/starknet_state_sync/src/test.rs line 325 at r3 (raw file):

    let address = ContractAddress::from(1u64);
    let mut diff = ThinStateDiff::default();
    diff.storage_diffs.insert(address, IndexMap::from([(Default::default(), Default::default())]));

Shouldn't you do the same for nonces?


crates/starknet_state_sync/src/test.rs line 325 at r3 (raw file):

    let address = ContractAddress::from(1u64);
    let mut diff = ThinStateDiff::default();
    diff.storage_diffs.insert(address, IndexMap::from([(Default::default(), Default::default())]));

Add a comment that you're creating a corrupted state diff (storage diffs for a contract that wasn't deployed) and you're checking that even if the state diff is corrupted the function will work as expected


crates/starknet_state_sync/src/test.rs line 48 at r2 (raw file):

    // Verify that the block that was written is returned correctly.
    let value =

use let else instead of match for a more compact syntax
Same across the entire PR

@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_read_methods_unit_tests branch from 514b33a to ae10b18 Compare December 24, 2024 11:27
Copy link
Contributor Author

@noamsp-starkware noamsp-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 4 of 5 files reviewed, 6 unresolved discussions (waiting on @ShahakShama)


crates/starknet_state_sync/src/test.rs line 48 at r2 (raw file):

Previously, ShahakShama wrote…

use let else instead of match for a more compact syntax
Same across the entire PR

Done.


crates/starknet_state_sync/src/test.rs line 65 at r3 (raw file):

Previously, ShahakShama wrote…

No need to differentiate this case if you accept my suggestions below.
Same across the entire PR

Done.


crates/starknet_state_sync/src/test.rs line 68 at r3 (raw file):

Previously, ShahakShama wrote…

display the response you actually got inside the panic message.
Same across the entire PR

Done.


crates/starknet_state_sync/src/test.rs line 68 at r3 (raw file):

Previously, ShahakShama wrote…

If you apply the 2 suggestions above, change StateSyncResponse::GetBlock::Ok to StateSyncResponse::GetBlock::Ok(Some(_))
Same across the entire PR

Done.


crates/starknet_state_sync/src/test.rs line 325 at r3 (raw file):

Previously, ShahakShama wrote…

Shouldn't you do the same for nonces?

Ill add it just for consistency, but to be honest, we don't really need this since we are just looking for an address that doesn't exist (therefore empty state_diff also does the job)


crates/starknet_state_sync/src/test.rs line 325 at r3 (raw file):

Previously, ShahakShama wrote…

Add a comment that you're creating a corrupted state diff (storage diffs for a contract that wasn't deployed) and you're checking that even if the state diff is corrupted the function will work as expected

Done.

Copy link
Contributor

@ShahakShama ShahakShama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @noamsp-starkware)


crates/starknet_state_sync/src/test.rs line 327 at r4 (raw file):

        .unwrap();

    // Check that get_storage_at verifies the contract was not deployed and therefore returns

Add to the end of the sentence ", even though the state diff is corrupt and contains this storage"


crates/starknet_state_sync/src/test.rs line 342 at r4 (raw file):

    assert_eq!(get_storage_at_result, Err(StateSyncError::ContractNotFound(address)));

    // Check that calling the functions below with a non existing adress returns contract not found

Put the same comment you did in get_storage_at plus my suggestion

@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_read_methods_unit_tests branch from ae10b18 to f34a894 Compare December 25, 2024 14:52
@noamsp-starkware noamsp-starkware changed the base branch from main to noam.s/state_sync_verify_contract_deployed December 25, 2024 14:52
Copy link
Contributor Author

@noamsp-starkware noamsp-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 5 files reviewed, 2 unresolved discussions (waiting on @ShahakShama)


crates/starknet_state_sync/src/test.rs line 327 at r4 (raw file):

Previously, ShahakShama wrote…

Add to the end of the sentence ", even though the state diff is corrupt and contains this storage"

Done.


crates/starknet_state_sync/src/test.rs line 342 at r4 (raw file):

Previously, ShahakShama wrote…

Put the same comment you did in get_storage_at plus my suggestion

Added this to the comment above, thought it looked better

Copy link

Benchmark movements:
tree_computation_flow performance improved 😺
tree_computation_flow time: [33.958 ms 34.007 ms 34.062 ms]
change: [-4.5849% -3.0229% -1.6483%] (p = 0.00 < 0.05)
Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
8 (8.00%) high mild
1 (1.00%) high severe

Copy link

Benchmark movements:
tree_computation_flow performance improved 😺
tree_computation_flow time: [33.900 ms 33.931 ms 33.969 ms]
change: [-3.9186% -2.3701% -1.0434%] (p = 0.00 < 0.05)
Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
3 (3.00%) high mild
6 (6.00%) high severe

@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_verify_contract_deployed branch from c2267ac to 367bc61 Compare December 26, 2024 11:52
@noamsp-starkware noamsp-starkware force-pushed the noam.s/state_sync_read_methods_unit_tests branch from 630baf2 to aee782d Compare December 26, 2024 11:52
Base automatically changed from noam.s/state_sync_verify_contract_deployed to main December 26, 2024 13:14
Copy link
Contributor

@eitanm-starkware eitanm-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 3 of 3 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @ShahakShama)

@noamsp-starkware noamsp-starkware removed the request for review from ShahakShama December 30, 2024 12:53
Copy link
Contributor Author

@noamsp-starkware noamsp-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dismissed @ShahakShama from 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @noamsp-starkware)

Copy link
Contributor

@ShahakShama ShahakShama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @noamsp-starkware)

@noamsp-starkware noamsp-starkware added this pull request to the merge queue Dec 30, 2024
Merged via the queue into main with commit e86a723 Dec 30, 2024
20 checks passed
@noamsp-starkware noamsp-starkware deleted the noam.s/state_sync_read_methods_unit_tests branch December 31, 2024 09:09
@github-actions github-actions bot locked and limited conversation to collaborators Jan 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants