-
Notifications
You must be signed in to change notification settings - Fork 65
committer: generate random state diff test util. #8738
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
committer: generate random state diff test util. #8738
Conversation
Benchmark movements: No major performance changes detected. |
87f9844
to
e0f551e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 5 of 5 files at r1, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @yoavGrs)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 15 at r1 (raw file):
pub mod state_diff_generator_test; pub(crate) const GENERATED_CONTRACT_ADDRESS: u32 = 500_u32;
Suggestion:
CONTRACT_ADDRESS:
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 49 at r1 (raw file):
random_bytes[0] &= mask; hex::encode(random_bytes)
I didn't undetsrand the mask computation. I understand it tries to be sort of a general computation for the first byte depending on how much bits we want in total (an assuming only the first byte should be masked). Can't you just write 7 or 0b111
? :)
In any case, I see we have Felt::random
in the CLI crate. Maybe we should move it.
Code quote:
fn generate_random_hex_251_bits(rng: &mut StdRng) -> String {
const N_BYTES: usize = 32; // 251 bits / 8 bits per byte
let mut random_bytes = [0u8; N_BYTES];
rng.fill(&mut random_bytes);
// Mask the extra bits if n_bits is not a multiple of 8
let n_bits = 251;
let mask = (1u8 << (n_bits % 8)) - 1;
random_bytes[0] &= mask;
hex::encode(random_bytes)
crates/starknet_committer/src/block_committer/state_diff_generator_test.rs
line 20 at r1 (raw file):
state_diff.storage_updates.get(&ContractAddress::from(GENERATED_CONTRACT_ADDRESS)).unwrap(); assert_eq!(contract.len(), N_STORAGE_UPDATES); }
Can you make a loop of some iterations (100 if it's fast) and assert that the squashed state diff is of len of approx 100*1000? To see that they are random on the entire domain.
Code quote:
let contract =
state_diff.storage_updates.get(&ContractAddress::from(GENERATED_CONTRACT_ADDRESS)).unwrap();
assert_eq!(contract.len(), N_STORAGE_UPDATES);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @yoavGrs)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 20 at r1 (raw file):
pub fn generate_random_state_diff(rng: &mut StdRng) -> StateDiff { let mut storage_updates = HashMap::new(); let mut contract_updates = HashMap::new();
Suggestion:
let mut contract_updates = HashMap::with_capacity(N_STORAGE_UPDATES);
e0f551e
to
40922cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @TzahiTaub)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 49 at r1 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
I didn't undetsrand the mask computation. I understand it tries to be sort of a general computation for the first byte depending on how much bits we want in total (an assuming only the first byte should be masked). Can't you just write 7 or
0b111
? :)
In any case, I see we haveFelt::random
in the CLI crate. Maybe we should move it.
Removed!
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 15 at r1 (raw file):
pub mod state_diff_generator_test; pub(crate) const GENERATED_CONTRACT_ADDRESS: u32 = 500_u32;
Done.
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 20 at r1 (raw file):
pub fn generate_random_state_diff(rng: &mut StdRng) -> StateDiff { let mut storage_updates = HashMap::new(); let mut contract_updates = HashMap::new();
Done.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
0dc425e
to
49e11a0
Compare
40922cc
to
8c8d07b
Compare
49e11a0
to
060e57d
Compare
8c8d07b
to
9894ccb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 1 of 6 files at r2, all commit messages.
Reviewable status: 1 of 6 files reviewed, all discussions resolved (waiting on @yoavGrs)
060e57d
to
4529771
Compare
9894ccb
to
51956c2
Compare
4529771
to
40f7e01
Compare
51956c2
to
bcacb09
Compare
40f7e01
to
2f3c0bc
Compare
bcacb09
to
f62fb62
Compare
2f3c0bc
to
8b266b6
Compare
f62fb62
to
6f9c987
Compare
8b266b6
to
42f405e
Compare
7508903
to
fed2e2d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 1 of 6 files at r2, 2 of 2 files at r4, all commit messages.
Reviewable status: 4 of 6 files reviewed, 1 unresolved discussion (waiting on @yoavGrs)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 34 at r4 (raw file):
} pub(crate) fn generate_random_storage_entry(
Can be private and still be used in the test module (though I don't mind it to be of crate level)
Code quote:
pub(crate)
fed2e2d
to
d023438
Compare
fb7dac8
to
5d0091c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 5 of 426 files at r5, 421 of 421 files at r6, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @yoavGrs)
d023438
to
0cd05e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 420 of 426 files at r7.
Reviewable status: 420 of 426 files reviewed, 1 unresolved discussion (waiting on @yoavGrs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 6 of 426 files at r7, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
0cd05e8
to
d38614f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 2 of 6 files at r8, all commit messages.
Reviewable status: 422 of 426 files reviewed, all discussions resolved (waiting on @yoavGrs)
ea3cf3d
to
316ae20
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dorimedini-starkware reviewed 418 of 426 files at r7, 3 of 6 files at r8, 2 of 4 files at r9, 3 of 3 files at r10, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @yoavGrs)
crates/starknet_committer/src/block_committer/random_structs.rs
line 196 at r10 (raw file):
None => upper_bound, Some(caller_max) => min(upper_bound, caller_max), };
non-blocking
Suggestion:
let max_patricia_key = min(upper_bound, max.unwrap_or(upper_bound));
crates/starknet_committer/src/block_committer/random_structs.rs
line 197 at r10 (raw file):
Some(caller_max) => min(upper_bound, caller_max), }; PatriciaKey::try_from(Felt::random(rng, Some(max_patricia_key))).unwrap()
non-blocking
Suggestion:
Self::try_from(Felt::random(rng, Some(max_patricia_key))).unwrap()
crates/starknet_committer/src/block_committer/state_diff_generator_test.rs
line 37 at r10 (raw file):
} assert!(storage_updates.len() >= (n_iterations * 99 / 100), "Key distribution is limited"); }
maybe this works? I think fixtures are run in test scope, not module scope, so you should get a recreated seeded randomness each test
Suggestion:
#[fixture]
fn rng() -> StdRng {
let seed = 42_u64; // Constant seed for reproducibility.
StdRng::seed_from_u64(seed)
}
#[rstest]
fn generate_random_state_diff_test(rng: StdRng) {
let state_diff = generate_random_state_diff(&mut rng);
let contract =
state_diff.storage_updates.get(&ContractAddress::from(CONTRACT_ADDRESS)).unwrap();
assert_eq!(contract.len(), N_STORAGE_UPDATES);
}
#[rstest]
fn key_distribution_test(rng: StdRng) {
let n_iterations = N_STORAGE_UPDATES * 100;
let mut storage_updates = HashMap::with_capacity(n_iterations);
for _ in 0..n_iterations {
let (key, value) = generate_random_storage_entry(&mut rng);
storage_updates.insert(key, value);
}
assert!(storage_updates.len() >= (n_iterations * 99 / 100), "Key distribution is limited");
}
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 14 at r10 (raw file):
pub mod state_diff_generator_test; pub(crate) const CONTRACT_ADDRESS: u32 = 500_u32;
so it's easier to search for, and so it doesn't come up in other searches
Suggestion:
const RANDOM_STATE_DIFF_CONTRACT_ADDRESS: u32 = 500_u32;
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 17 at r10 (raw file):
pub(crate) const N_STORAGE_UPDATES: usize = 1000_usize; pub fn generate_random_state_diff(rng: &mut StdRng) -> StateDiff {
no need to enforce specific RNG type, right?
Suggestion:
pub fn generate_random_state_diff<R: Rng>(rng: &mut R) -> StateDiff {
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 31 at r10 (raw file):
class_hash_to_compiled_class_hash: HashMap::new(), storage_updates, }
this doesn't work?
Suggestion:
StateDiff {
storage_updates,
..Default::default()
}
crates/starknet_committer/src/block_committer.rs
line 5 at r10 (raw file):
pub mod input; #[cfg(any(feature = "testing", test))] pub mod random_structs;
why wasn't this module gated before...? by mistake? no business logic uses it, right?
Code quote:
#[cfg(any(feature = "testing", test))]
pub mod random_structs;
crates/starknet_committer/Cargo.toml
line 39 at r10 (raw file):
[features] testing = ["starknet_patricia/testing"]
move to above [dependencies]
?
Code quote:
[features]
testing = ["starknet_patricia/testing"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 4 of 4 files at r9.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @yoavGrs)
crates/starknet_committer/Cargo.toml
line 39 at r10 (raw file):
Previously, dorimedini-starkware wrote…
move to above
[dependencies]
?
I think it's better to have it imported only when using the testing feature, we shouldn't use it in prod builds.
crates/starknet_committer/src/block_committer.rs
line 5 at r10 (raw file):
Previously, dorimedini-starkware wrote…
why wasn't this module gated before...? by mistake? no business logic uses it, right?
Don't know, I think this should be done on main as well.
crates/starknet_committer/src/block_committer/random_structs.rs
line 196 at r10 (raw file):
Previously, dorimedini-starkware wrote…
non-blocking
Apparently the random of felt ignores the max
param it gets, so it doesn't matter too much :)
crates/starknet_committer/src/block_committer/state_diff_generator_test.rs
line 37 at r10 (raw file):
Previously, dorimedini-starkware wrote…
maybe this works? I think fixtures are run in test scope, not module scope, so you should get a recreated seeded randomness each test
Correct.
316ae20
to
6827065
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 423 of 426 files reviewed, 2 unresolved discussions (waiting on @dorimedini-starkware and @yoavGrs)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 17 at r10 (raw file):
Previously, dorimedini-starkware wrote…
no need to enforce specific RNG type, right?
No, but what is the gain from making this generic?
6827065
to
e9e64ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 423 of 426 files reviewed, 2 unresolved discussions (waiting on @dorimedini-starkware and @yoavGrs)
crates/starknet_committer/src/block_committer/state_diff_generator.rs
line 17 at r10 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
No, but what is the gain from making this generic?
OK, actually replaced the RNG as the previous was crypto safe, and the current should be faster
e9e64ee
to
6be935f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dorimedini-starkware reviewed 1 of 3 files at r11, 2 of 2 files at r13, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
crates/starknet_committer/Cargo.toml
line 39 at r10 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
I think it's better to have it imported only when using the testing feature, we shouldn't use it in prod builds.
yes yes, I just mean: move the [features]
block up in this file
crates/starknet_committer/src/block_committer/random_structs.rs
line 196 at r10 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
Apparently the random of felt ignores the
max
param it gets, so it doesn't matter too much :)
hmmmmm damn... why??
6be935f
to
47896eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 423 of 426 files reviewed, all discussions resolved (waiting on @dorimedini-starkware)
crates/starknet_committer/Cargo.toml
line 39 at r10 (raw file):
Previously, dorimedini-starkware wrote…
yes yes, I just mean: move the
[features]
block up in this file
Oh
crates/starknet_committer/src/block_committer/random_structs.rs
line 196 at r10 (raw file):
Previously, dorimedini-starkware wrote…
hmmmmm damn... why??
Cause it would be too obvious to have it the other way around? I'll open a PR on main...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TzahiTaub reviewed 1 of 3 files at r10, 1 of 3 files at r11, 3 of 3 files at r14, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @yoavGrs)
No description provided.