Skip to content

Commit e445ff3

Browse files
starknet_committer_cli: add option for key size
1 parent db97ed3 commit e445ff3

File tree

2 files changed

+59
-28
lines changed

2 files changed

+59
-28
lines changed

crates/starknet_committer_cli/src/main.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clap::{Args, Parser, Subcommand};
77
use starknet_committer_cli::commands::run_storage_benchmark;
88
use starknet_patricia_storage::map_storage::{CachedStorage, MapStorage};
99
use starknet_patricia_storage::mdbx_storage::MdbxStorage;
10+
use starknet_patricia_storage::short_key_storage::wrap_storage_or_panic;
1011
use starknet_patricia_storage::storage_trait::Storage;
1112
use tracing::info;
1213
use tracing::level_filters::LevelFilter;
@@ -43,6 +44,10 @@ struct StorageArgs {
4344
/// checkpointing is ignored.
4445
#[clap(long, default_value = "cached-mdbx")]
4546
storage_type: StorageType,
47+
/// If not none, wraps the storage in a key-shrinking storage.
48+
/// See `short_key_storage.rs` for more details and allowed values.
49+
#[clap(long, default_value = None)]
50+
key_size: Option<u8>,
4651
/// If using cached storage, the size of the cache.
4752
#[clap(long, default_value = "1000000")]
4853
cache_size: usize,
@@ -85,6 +90,7 @@ pub async fn run_committer_cli(
8590
n_iterations,
8691
n_diffs,
8792
storage_type,
93+
key_size,
8894
cache_size,
8995
checkpoint_interval,
9096
log_level,
@@ -102,13 +108,18 @@ pub async fn run_committer_cli(
102108

103109
let (mut storage, checkpoint_dir): (Box<dyn Storage>, Option<&str>) = match storage_type
104110
{
105-
StorageType::MapStorage => (Box::new(MapStorage::default()), None),
111+
StorageType::MapStorage => {
112+
(wrap_storage_or_panic(key_size, MapStorage::default()), None)
113+
}
106114
StorageType::Mdbx => {
107115
let storage_path = storage_path
108116
.unwrap_or_else(|| format!("{data_path}/storage/{storage_type:?}"));
109117
fs::create_dir_all(&storage_path).expect("Failed to create storage directory.");
110118
(
111-
Box::new(MdbxStorage::open(Path::new(&storage_path)).unwrap()),
119+
wrap_storage_or_panic(
120+
key_size,
121+
MdbxStorage::open(Path::new(&storage_path)).unwrap(),
122+
),
112123
Some(&checkpoint_dir),
113124
)
114125
}
@@ -117,10 +128,13 @@ pub async fn run_committer_cli(
117128
.unwrap_or_else(|| format!("{data_path}/storage/{storage_type:?}"));
118129
fs::create_dir_all(&storage_path).expect("Failed to create storage directory.");
119130
(
120-
Box::new(CachedStorage::new(
121-
MdbxStorage::open(Path::new(&storage_path)).unwrap(),
122-
NonZeroUsize::new(cache_size).unwrap(),
123-
)),
131+
wrap_storage_or_panic(
132+
key_size,
133+
CachedStorage::new(
134+
MdbxStorage::open(Path::new(&storage_path)).unwrap(),
135+
NonZeroUsize::new(cache_size).unwrap(),
136+
),
137+
),
124138
Some(&checkpoint_dir),
125139
)
126140
}

crates/starknet_patricia_storage/src/short_key_storage.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,33 @@ use crate::storage_trait::{DbHashMap, DbKey, DbValue, PatriciaStorageResult, Sto
2626

2727
#[macro_export]
2828
macro_rules! define_short_key_storage {
29-
($( ( $sizes:ty, $names:ident ) ),+ $(,)?) => {
29+
($( ( $sizes:expr, $sizes_type:ty, $names:ident ) ),+ $(,)?) => {
3030
$(
31-
$crate::define_short_key_storage!($sizes, $names);
31+
$crate::define_short_key_storage!($sizes, $sizes_type, $names);
3232
)+
33+
34+
/// Wrap an existing storage implementation in a boxed short key storage implementation.
35+
/// If no size is given, boxes and returns the original storage.
36+
/// Panics if the size is not within the allowed range.
37+
pub fn wrap_storage_or_panic<S: Storage + 'static>(
38+
size: Option<u8>,
39+
storage: S,
40+
) -> Box<dyn Storage> {
41+
let Some(size) = size else {
42+
return Box::new(storage);
43+
};
44+
match size {
45+
$( $sizes => Box::new($names::new(storage)), )+
46+
size => panic!("Invalid key size {size}."),
47+
}
48+
}
3349
};
3450

35-
($size:ty, $name:ident) => {
51+
($size:expr, $size_type:ty, $name:ident) => {
3652
/// A storage that hashes each key to a $size - byte key.
3753
pub struct $name<S: Storage> {
3854
storage: S,
39-
_bytes: PhantomData<$size>,
55+
_bytes: PhantomData<$size_type>,
4056
}
4157

4258
impl<S: Storage> $name<S> {
@@ -45,7 +61,7 @@ macro_rules! define_short_key_storage {
4561
}
4662

4763
pub fn small_key(key: &DbKey) -> DbKey {
48-
let mut hasher = Blake2s::<$size>::new();
64+
let mut hasher = Blake2s::<$size_type>::new();
4965
hasher.update(key.0.as_slice());
5066
let result = hasher.finalize();
5167
DbKey(result.as_slice().to_vec())
@@ -81,26 +97,27 @@ macro_rules! define_short_key_storage {
8197
fn delete(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
8298
self.storage.delete(&Self::small_key(key))
8399
}
100+
84101
}
85102
};
86103
}
87104

88105
define_short_key_storage!(
89-
(U16, ShortKeyStorage16),
90-
(U17, ShortKeyStorage17),
91-
(U18, ShortKeyStorage18),
92-
(U19, ShortKeyStorage19),
93-
(U20, ShortKeyStorage20),
94-
(U21, ShortKeyStorage21),
95-
(U22, ShortKeyStorage22),
96-
(U23, ShortKeyStorage23),
97-
(U24, ShortKeyStorage24),
98-
(U25, ShortKeyStorage25),
99-
(U26, ShortKeyStorage26),
100-
(U27, ShortKeyStorage27),
101-
(U28, ShortKeyStorage28),
102-
(U29, ShortKeyStorage29),
103-
(U30, ShortKeyStorage30),
104-
(U31, ShortKeyStorage31),
105-
(U32, ShortKeyStorage32)
106+
(16, U16, ShortKeyStorage16),
107+
(17, U17, ShortKeyStorage17),
108+
(18, U18, ShortKeyStorage18),
109+
(19, U19, ShortKeyStorage19),
110+
(20, U20, ShortKeyStorage20),
111+
(21, U21, ShortKeyStorage21),
112+
(22, U22, ShortKeyStorage22),
113+
(23, U23, ShortKeyStorage23),
114+
(24, U24, ShortKeyStorage24),
115+
(25, U25, ShortKeyStorage25),
116+
(26, U26, ShortKeyStorage26),
117+
(27, U27, ShortKeyStorage27),
118+
(28, U28, ShortKeyStorage28),
119+
(29, U29, ShortKeyStorage29),
120+
(30, U30, ShortKeyStorage30),
121+
(31, U31, ShortKeyStorage31),
122+
(32, U32, ShortKeyStorage32)
106123
);

0 commit comments

Comments
 (0)