Skip to content

Commit da4c242

Browse files
committed
starknet_committer: test mdbx storage
1 parent fd3ce7a commit da4c242

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ tracing.workspace = true
2727
[dev-dependencies]
2828
starknet_api = { workspace = true, features = ["testing"] }
2929
starknet_patricia = { workspace = true, features = ["testing"] }
30+
tempfile.workspace = true
3031

3132
[lints]
3233
workspace = true

crates/starknet_committer/src/block_committer/input.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use starknet_api::state::StorageKey;
66
use starknet_patricia::hash::hash_trait::HashOutput;
77
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{LeafModifications, SkeletonLeaf};
88
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
9+
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
910
use starknet_types_core::felt::Felt;
1011
use tracing::level_filters::LevelFilter;
1112

@@ -50,9 +51,21 @@ impl From<&StarknetStorageKey> for NodeIndex {
5051
}
5152
}
5253

54+
impl From<&StarknetStorageKey> for DbKey {
55+
fn from(key: &StarknetStorageKey) -> Self {
56+
Self(serde_json::to_vec(&key.0.0).expect("Serialization of PatriciaKey failed"))
57+
}
58+
}
59+
5360
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
5461
pub struct StarknetStorageValue(pub Felt);
5562

63+
impl From<&StarknetStorageValue> for DbValue {
64+
fn from(value: &StarknetStorageValue) -> Self {
65+
Self(serde_json::to_vec(&value.0).expect("Serialization of Felt failed"))
66+
}
67+
}
68+
5669
#[derive(Debug, Default, Eq, PartialEq)]
5770
pub struct StateDiff {
5871
pub address_to_class_hash: HashMap<ContractAddress, ClassHash>,

crates/starknet_committer/src/block_committer/state_diff_generator_test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::collections::HashMap;
2+
use tempfile::tempdir;
23

34
use rand::rngs::StdRng;
45
use rand::SeedableRng;
56
use rstest::rstest;
67
use starknet_api::core::ContractAddress;
8+
use starknet_patricia_storage::mdbx_storage::MdbxStorage;
9+
use starknet_patricia_storage::storage_trait::{DbKey, DbValue, Storage};
710

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

3033
let n_iterations = N_STORAGE_UPDATES * 100;
34+
3135
let mut storage_updates = HashMap::with_capacity(n_iterations);
36+
let mut mdbx_storage = MdbxStorage::open(tempdir().unwrap().path()).unwrap();
37+
3238
for _ in 0..n_iterations {
3339
let (key, value) = generate_random_storage_entry(&mut rng);
3440
storage_updates.insert(key, value);
3541
}
42+
43+
mdbx_storage
44+
.mset(
45+
storage_updates
46+
.iter()
47+
.map(|(key, value)| (DbKey::from(key), DbValue::from(value)))
48+
.collect(),
49+
)
50+
.unwrap();
51+
52+
for (key, value) in &storage_updates {
53+
let stored_value = mdbx_storage.get(&DbKey::from(key)).unwrap().unwrap();
54+
assert_eq!(stored_value, DbValue::from(value));
55+
}
56+
3657
assert!(storage_updates.len() >= (n_iterations * 99 / 100), "Key distribution is limited");
3758
}

crates/starknet_patricia_storage/src/mdbx_storage.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
use std::path::Path;
22

3-
use libmdbx::{Database as MdbxDb, TableFlags, WriteFlags, WriteMap};
3+
use libmdbx::{Database, Geometry, PageSize, TableFlags, WriteFlags, WriteMap};
44

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

88
pub struct MdbxStorage {
9-
db: MdbxDb<WriteMap>,
9+
db: Database<WriteMap>,
1010
}
1111

1212
impl MdbxStorage {
1313
pub fn open(path: &Path) -> PatriciaStorageResult<Self> {
14-
let db = MdbxDb::<WriteMap>::new().open(path)?;
14+
let db = Database::<WriteMap>::new()
15+
.set_geometry(Geometry {
16+
size: Some(1 << 20..1 << 40),
17+
growth_step: Some(1 << 32),
18+
page_size: Some(PageSize::MinimalAcceptable),
19+
..Default::default()
20+
})
21+
.set_max_tables(1)
22+
.open(path)?;
1523
let txn = db.begin_rw_txn()?;
1624
txn.create_table(None, TableFlags::empty())?;
1725
txn.commit()?;

0 commit comments

Comments
 (0)