Skip to content
Open
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: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/starknet_committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing.workspace = true
[dev-dependencies]
starknet_api = { workspace = true, features = ["testing"] }
starknet_patricia = { workspace = true, features = ["testing"] }
tempfile.workspace = true

[lints]
workspace = true
Expand Down
13 changes: 13 additions & 0 deletions crates/starknet_committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet_api::state::StorageKey;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{LeafModifications, SkeletonLeaf};
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;

Expand Down Expand Up @@ -50,9 +51,21 @@ impl From<&StarknetStorageKey> for NodeIndex {
}
}

impl From<&StarknetStorageKey> for DbKey {
fn from(key: &StarknetStorageKey) -> Self {
Self(serde_json::to_vec(&key.0.0).expect("Serialization of PatriciaKey failed"))
}
}

#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub struct StarknetStorageValue(pub Felt);

impl From<&StarknetStorageValue> for DbValue {
fn from(value: &StarknetStorageValue) -> Self {
Self(serde_json::to_vec(&value.0).expect("Serialization of Felt failed"))
}
}

#[derive(Debug, Default, Eq, PartialEq)]
pub struct StateDiff {
pub address_to_class_hash: HashMap<ContractAddress, ClassHash>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::collections::HashMap;
use tempfile::tempdir;

use rand::rngs::StdRng;
use rand::SeedableRng;
use rstest::rstest;
use starknet_api::core::ContractAddress;
use starknet_patricia_storage::mdbx_storage::MdbxStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue, Storage};

use crate::block_committer::state_diff_generator::{
generate_random_state_diff,
Expand All @@ -28,10 +31,28 @@ fn key_distribution_test() {
let mut rng = StdRng::seed_from_u64(seed);

let n_iterations = N_STORAGE_UPDATES * 100;

let mut storage_updates = HashMap::with_capacity(n_iterations);
let mut mdbx_storage = MdbxStorage::open(tempdir().unwrap().path()).unwrap();

for _ in 0..n_iterations {
let (key, value) = generate_random_storage_entry(&mut rng);
storage_updates.insert(key, value);
}

mdbx_storage
.mset(
storage_updates
.iter()
.map(|(key, value)| (DbKey::from(key), DbValue::from(value)))
.collect(),
)
.unwrap();

for (key, value) in &storage_updates {
let stored_value = mdbx_storage.get(&DbKey::from(key)).unwrap().unwrap();
assert_eq!(stored_value, DbValue::from(value));
}

assert!(storage_updates.len() >= (n_iterations * 99 / 100), "Key distribution is limited");
}
14 changes: 11 additions & 3 deletions crates/starknet_patricia_storage/src/mdbx_storage.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use std::path::Path;

use libmdbx::{Database as MdbxDb, TableFlags, WriteFlags, WriteMap};
use libmdbx::{Database, Geometry, PageSize, TableFlags, WriteFlags, WriteMap};

use crate::map_storage::MapStorage;
use crate::storage_trait::{DbKey, DbValue, PatriciaStorageResult, Storage};

pub struct MdbxStorage {
db: MdbxDb<WriteMap>,
db: Database<WriteMap>,
}

impl MdbxStorage {
pub fn open(path: &Path) -> PatriciaStorageResult<Self> {
let db = MdbxDb::<WriteMap>::new().open(path)?;
let db = Database::<WriteMap>::new()
.set_geometry(Geometry {
size: Some(1 << 20..1 << 40),
growth_step: Some(1 << 32),
page_size: Some(PageSize::MinimalAcceptable),
..Default::default()
})
.set_max_tables(1)
.open(path)?;
let txn = db.begin_rw_txn()?;
txn.create_table(None, TableFlags::empty())?;
txn.commit()?;
Expand Down
Loading