Skip to content

Commit 91008e4

Browse files
committed
starknet_committer: add mdbx storage
1 parent 0b52a49 commit 91008e4

File tree

6 files changed

+82
-9
lines changed

6 files changed

+82
-9
lines changed

Cargo.lock

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

crates/starknet_committer_and_os_cli/src/committer_cli/commands.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,10 @@ pub async fn run_storage_benchmark() {
153153
);
154154
serialized_filled_forest.0.write_to_storage(&mut storage);
155155

156-
let n_new_facts = serialized_filled_forest
157-
.0
158-
.storage_tries
159-
.get(&contract_leaf)
160-
.unwrap()
161-
.tree_map
162-
.len();
156+
let n_new_facts =
157+
serialized_filled_forest.0.storage_tries.get(&contract_leaf).unwrap().tree_map.len();
163158
time_measurement.stop_measurement(n_new_facts);
164159

165-
166160
contracts_trie_root_hash = serialized_filled_forest.0.get_contract_root_hash();
167161
classes_trie_root_hash = serialized_filled_forest.0.get_compiled_class_root_hash();
168162
}

crates/starknet_patricia_storage/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ testing = []
1313
workspace = true
1414

1515
[dependencies]
16+
apollo_storage.workspace = true
1617
hex.workspace = true
18+
libmdbx.workspace = true
19+
lru.workspace = true
1720
serde = { workspace = true, features = ["derive"] }
1821
serde_json.workspace = true
1922
starknet-types-core.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod db_object;
22
pub mod errors;
33
pub mod map_storage;
4+
pub mod mdbx_storage;
45
pub mod storage_trait;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::path::Path;
2+
3+
use libmdbx::{Database as MdbxDb, TableFlags, WriteFlags, WriteMap};
4+
5+
use crate::map_storage::MapStorage;
6+
use crate::storage_trait::{DbKey, DbValue, PatriciaStorageResult, Storage};
7+
8+
pub struct MdbxStorage {
9+
db: MdbxDb<WriteMap>,
10+
}
11+
12+
impl MdbxStorage {
13+
pub fn open(path: &Path) -> PatriciaStorageResult<Self> {
14+
let db = MdbxDb::<WriteMap>::new().open(path)?;
15+
let txn = db.begin_rw_txn()?;
16+
txn.create_table(None, TableFlags::empty())?;
17+
txn.commit()?;
18+
Ok(Self { db })
19+
}
20+
}
21+
22+
impl Storage for MdbxStorage {
23+
fn get(&self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
24+
let txn = self.db.begin_ro_txn()?;
25+
let table = txn.open_table(None)?;
26+
Ok(txn.get(&table, &key.0)?.map(DbValue))
27+
}
28+
29+
fn set(&mut self, key: DbKey, value: DbValue) -> PatriciaStorageResult<Option<DbValue>> {
30+
let txn = self.db.begin_rw_txn()?;
31+
let table = txn.open_table(None)?;
32+
let prev_val = txn.get(&table, &key.0)?.map(DbValue);
33+
txn.put(&table, key.0, value.0, WriteFlags::UPSERT)?;
34+
txn.commit()?;
35+
Ok(prev_val)
36+
}
37+
38+
fn mget(&self, keys: &[DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
39+
let txn = self.db.begin_ro_txn()?;
40+
let table = txn.open_table(None)?;
41+
let mut res = Vec::with_capacity(keys.len());
42+
for key in keys {
43+
res.push(txn.get(&table, &key.0)?.map(DbValue));
44+
}
45+
Ok(res)
46+
}
47+
48+
fn mset(&mut self, key_to_value: MapStorage) -> PatriciaStorageResult<()> {
49+
let txn = self.db.begin_rw_txn()?;
50+
let table = txn.open_table(None)?;
51+
for (key, value) in key_to_value {
52+
txn.put(&table, key.0, value.0, WriteFlags::UPSERT)?;
53+
}
54+
txn.commit()?;
55+
Ok(())
56+
}
57+
58+
fn delete(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
59+
let txn = self.db.begin_rw_txn()?;
60+
let table = txn.open_table(None)?;
61+
let prev_val = txn.get(&table, &key.0)?.map(DbValue);
62+
if prev_val.is_some() {
63+
txn.del(&table, &key.0, None)?;
64+
}
65+
txn.commit()?;
66+
Ok(prev_val)
67+
}
68+
}

crates/starknet_patricia_storage/src/storage_trait.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ pub struct DbValue(pub Vec<u8>);
1212

1313
/// An error that can occur when interacting with the database.
1414
#[derive(thiserror::Error, Debug)]
15-
pub enum PatriciaStorageError {}
15+
pub enum PatriciaStorageError {
16+
/// An error that occurred in the database library.
17+
#[error(transparent)]
18+
Mdbx(#[from] libmdbx::Error),
19+
}
1620

1721
pub type PatriciaStorageResult<T> = Result<T, PatriciaStorageError>;
1822

0 commit comments

Comments
 (0)