Skip to content

Commit 47896eb

Browse files
yoavGrsTzahiTaub
authored andcommitted
committer: generate random state diff test util.
1 parent 7d5977b commit 47896eb

File tree

7 files changed

+101
-10
lines changed

7 files changed

+101
-10
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_committer/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ repository.workspace = true
66
license.workspace = true
77
description = "Computes and manages Starknet state."
88

9+
[features]
10+
testing = ["starknet_patricia/testing"]
11+
912
[dependencies]
1013
ethnum.workspace = true
1114
hex.workspace = true
@@ -19,7 +22,6 @@ starknet_api.workspace = true
1922
starknet_patricia.workspace = true
2023
starknet_patricia_storage.workspace = true
2124
strum.workspace = true
22-
strum_macros.workspace = true
2325
thiserror.workspace = true
2426
tokio = { workspace = true, features = ["rt"] }
2527
tracing.workspace = true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
pub mod commit;
22
pub mod errors;
33
pub mod input;
4+
#[cfg(any(feature = "testing", test))]
45
pub mod random_structs;
6+
#[cfg(any(feature = "testing", test))]
7+
pub mod state_diff_generator;

crates/starknet_committer/src/block_committer/random_structs.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ use rand::prelude::IteratorRandom;
66
use rand::Rng;
77
use rand_distr::num_traits::ToPrimitive;
88
use rand_distr::{Distribution, Geometric};
9-
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PATRICIA_KEY_UPPER_BOUND};
9+
use starknet_api::core::{
10+
ClassHash,
11+
ContractAddress,
12+
Nonce,
13+
PatriciaKey,
14+
PATRICIA_KEY_UPPER_BOUND,
15+
};
1016
use starknet_patricia::felt::u256_from_felt;
1117
use starknet_patricia::hash::hash_trait::HashOutput;
1218
use starknet_patricia::patricia_merkle_tree::external_test_utils::{
@@ -181,14 +187,18 @@ random_filled_node!(StarknetStorageValue);
181187
random_filled_node!(CompiledClassHash);
182188
random_filled_node!(ContractState);
183189

190+
impl RandomValue for PatriciaKey {
191+
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
192+
let upper_bound = u256_from_felt(&Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND));
193+
let max_patricia_key = min(upper_bound, max.unwrap_or(upper_bound));
194+
195+
Self::try_from(Felt::random(rng, Some(max_patricia_key))).unwrap()
196+
}
197+
}
198+
184199
impl RandomValue for ContractAddress {
185200
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
186-
let address_max = u256_from_felt(&Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND));
187-
let max = match max {
188-
None => address_max,
189-
Some(caller_max) => min(address_max, caller_max),
190-
};
191-
ContractAddress::try_from(Felt::random(rng, Some(max))).unwrap()
201+
ContractAddress(PatriciaKey::random(rng, max))
192202
}
193203
}
194204

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::collections::HashMap;
2+
3+
use rand::Rng;
4+
use starknet_api::core::{ContractAddress, PatriciaKey};
5+
use starknet_api::state::StorageKey;
6+
7+
use crate::block_committer::input::{StarknetStorageKey, StarknetStorageValue, StateDiff};
8+
use crate::block_committer::random_structs::RandomValue;
9+
10+
#[cfg(test)]
11+
#[path = "state_diff_generator_test.rs"]
12+
pub mod state_diff_generator_test;
13+
14+
pub(crate) const RANDOM_STATE_DIFF_CONTRACT_ADDRESS: u32 = 500_u32;
15+
pub(crate) const N_STORAGE_UPDATES: usize = 1000_usize;
16+
17+
pub fn generate_random_state_diff<R: Rng>(rng: &mut R) -> StateDiff {
18+
let mut storage_updates = HashMap::new();
19+
let mut contract_updates = HashMap::with_capacity(N_STORAGE_UPDATES);
20+
for _ in 0..N_STORAGE_UPDATES {
21+
let storage_entry = generate_random_storage_entry(rng);
22+
contract_updates.insert(storage_entry.0, storage_entry.1);
23+
}
24+
25+
storage_updates
26+
.insert(ContractAddress::from(RANDOM_STATE_DIFF_CONTRACT_ADDRESS), contract_updates);
27+
StateDiff { storage_updates, ..Default::default() }
28+
}
29+
30+
fn generate_random_storage_entry<R: Rng>(
31+
rng: &mut R,
32+
) -> (StarknetStorageKey, StarknetStorageValue) {
33+
let key = StarknetStorageKey(StorageKey(PatriciaKey::random(rng, None)));
34+
let value = StarknetStorageValue::random(rng, None);
35+
(key, value)
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::collections::HashMap;
2+
3+
use rand::rngs::SmallRng;
4+
use rand::{Rng, SeedableRng};
5+
use rstest::{fixture, rstest};
6+
use starknet_api::core::ContractAddress;
7+
8+
use crate::block_committer::state_diff_generator::{
9+
generate_random_state_diff,
10+
generate_random_storage_entry,
11+
N_STORAGE_UPDATES,
12+
RANDOM_STATE_DIFF_CONTRACT_ADDRESS,
13+
};
14+
15+
#[fixture]
16+
fn rng() -> SmallRng {
17+
let seed = 42_u64; // Constant seed for reproducibility.
18+
SmallRng::seed_from_u64(seed)
19+
}
20+
21+
#[rstest]
22+
fn generate_random_state_diff_test(mut rng: impl Rng) {
23+
let state_diff = generate_random_state_diff(&mut rng);
24+
let contract = state_diff
25+
.storage_updates
26+
.get(&ContractAddress::from(RANDOM_STATE_DIFF_CONTRACT_ADDRESS))
27+
.unwrap();
28+
assert_eq!(contract.len(), N_STORAGE_UPDATES);
29+
}
30+
31+
#[rstest]
32+
fn key_distribution_test(mut rng: impl Rng) {
33+
let n_iterations = N_STORAGE_UPDATES * 100;
34+
let mut storage_updates = HashMap::with_capacity(n_iterations);
35+
for _ in 0..n_iterations {
36+
let (key, value) = generate_random_storage_entry(&mut rng);
37+
storage_updates.insert(key, value);
38+
}
39+
assert!(storage_updates.len() >= (n_iterations * 99 / 100), "Key distribution is limited");
40+
}

crates/starknet_committer_and_os_cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ serde_json.workspace = true
3838
serde_repr.workspace = true
3939
starknet-types-core.workspace = true
4040
starknet_api.workspace = true
41-
starknet_committer.workspace = true
41+
# The testing feature is needed for the python tests.
42+
starknet_committer = { workspace = true, features = ["testing"] }
4243
# The 'testing' feature of starknet_os should be moved under this crate's `testing` feature, when it
4344
# exists.
4445
starknet_os = { workspace = true, features = ["deserialize", "testing"] }

0 commit comments

Comments
 (0)