Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/starknet_committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ repository.workspace = true
license.workspace = true
description = "Computes and manages Starknet state."

[features]
testing = ["starknet_patricia/testing"]

[dependencies]
ethnum.workspace = true
hex.workspace = true
Expand All @@ -19,7 +22,6 @@ starknet_api.workspace = true
starknet_patricia.workspace = true
starknet_patricia_storage.workspace = true
strum.workspace = true
strum_macros.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["rt"] }
tracing.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_committer/src/block_committer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod commit;
pub mod errors;
pub mod input;
#[cfg(any(feature = "testing", test))]
pub mod random_structs;
#[cfg(any(feature = "testing", test))]
pub mod state_diff_generator;
24 changes: 17 additions & 7 deletions crates/starknet_committer/src/block_committer/random_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use rand::prelude::IteratorRandom;
use rand::Rng;
use rand_distr::num_traits::ToPrimitive;
use rand_distr::{Distribution, Geometric};
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PATRICIA_KEY_UPPER_BOUND};
use starknet_api::core::{
ClassHash,
ContractAddress,
Nonce,
PatriciaKey,
PATRICIA_KEY_UPPER_BOUND,
};
use starknet_patricia::felt::u256_from_felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::external_test_utils::{
Expand Down Expand Up @@ -181,14 +187,18 @@ random_filled_node!(StarknetStorageValue);
random_filled_node!(CompiledClassHash);
random_filled_node!(ContractState);

impl RandomValue for PatriciaKey {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
let upper_bound = u256_from_felt(&Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND));
let max_patricia_key = min(upper_bound, max.unwrap_or(upper_bound));

Self::try_from(Felt::random(rng, Some(max_patricia_key))).unwrap()
}
}

impl RandomValue for ContractAddress {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
let address_max = u256_from_felt(&Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND));
let max = match max {
None => address_max,
Some(caller_max) => min(address_max, caller_max),
};
ContractAddress::try_from(Felt::random(rng, Some(max))).unwrap()
ContractAddress(PatriciaKey::random(rng, max))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::collections::HashMap;

use rand::Rng;
use starknet_api::core::{ContractAddress, PatriciaKey};
use starknet_api::state::StorageKey;

use crate::block_committer::input::{StarknetStorageKey, StarknetStorageValue, StateDiff};
use crate::block_committer::random_structs::RandomValue;

#[cfg(test)]
#[path = "state_diff_generator_test.rs"]
pub mod state_diff_generator_test;

pub(crate) const RANDOM_STATE_DIFF_CONTRACT_ADDRESS: u32 = 500_u32;
pub(crate) const N_STORAGE_UPDATES: usize = 1000_usize;

pub fn generate_random_state_diff<R: Rng>(rng: &mut R) -> StateDiff {
let mut storage_updates = HashMap::new();
let mut contract_updates = HashMap::with_capacity(N_STORAGE_UPDATES);
for _ in 0..N_STORAGE_UPDATES {
let storage_entry = generate_random_storage_entry(rng);
contract_updates.insert(storage_entry.0, storage_entry.1);
}

storage_updates
.insert(ContractAddress::from(RANDOM_STATE_DIFF_CONTRACT_ADDRESS), contract_updates);
StateDiff { storage_updates, ..Default::default() }
}

fn generate_random_storage_entry<R: Rng>(
rng: &mut R,
) -> (StarknetStorageKey, StarknetStorageValue) {
let key = StarknetStorageKey(StorageKey(PatriciaKey::random(rng, None)));
let value = StarknetStorageValue::random(rng, None);
(key, value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::collections::HashMap;

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use rstest::{fixture, rstest};
use starknet_api::core::ContractAddress;

use crate::block_committer::state_diff_generator::{
generate_random_state_diff,
generate_random_storage_entry,
N_STORAGE_UPDATES,
RANDOM_STATE_DIFF_CONTRACT_ADDRESS,
};

#[fixture]
fn rng() -> SmallRng {
let seed = 42_u64; // Constant seed for reproducibility.
SmallRng::seed_from_u64(seed)
}

#[rstest]
fn generate_random_state_diff_test(mut rng: impl Rng) {
let state_diff = generate_random_state_diff(&mut rng);
let contract = state_diff
.storage_updates
.get(&ContractAddress::from(RANDOM_STATE_DIFF_CONTRACT_ADDRESS))
.unwrap();
assert_eq!(contract.len(), N_STORAGE_UPDATES);
}

#[rstest]
fn key_distribution_test(mut rng: impl Rng) {
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");
}
3 changes: 2 additions & 1 deletion crates/starknet_committer_and_os_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ serde_json.workspace = true
serde_repr.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_committer.workspace = true
# The testing feature is needed for the python tests.
starknet_committer = { workspace = true, features = ["testing"] }
# The 'testing' feature of starknet_os should be moved under this crate's `testing` feature, when it
# exists.
starknet_os = { workspace = true, features = ["deserialize", "testing"] }
Expand Down
Loading