Skip to content

Commit 8ce732f

Browse files
starknet_patricia_storage: add short-key storage
1 parent a4b41f9 commit 8ce732f

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

Cargo.lock

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

crates/starknet_patricia_storage/Cargo.toml

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

1515
[dependencies]
16+
blake2.workspace = true
17+
digest.workspace = true
1618
hex.workspace = true
1719
libmdbx.workspace = true
1820
lru.workspace = true

crates/starknet_patricia_storage/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pub mod map_storage;
44
#[cfg(test)]
55
pub mod map_storage_test;
66
pub mod mdbx_storage;
7+
pub mod short_key_storage;
78
pub mod storage_trait;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use std::marker::PhantomData;
2+
3+
use blake2::digest::consts::{
4+
U16,
5+
U17,
6+
U18,
7+
U19,
8+
U20,
9+
U21,
10+
U22,
11+
U23,
12+
U24,
13+
U25,
14+
U26,
15+
U27,
16+
U28,
17+
U29,
18+
U30,
19+
U31,
20+
U32,
21+
};
22+
use blake2::Blake2s;
23+
use digest::Digest;
24+
25+
use crate::storage_trait::{DbHashMap, DbKey, DbValue, PatriciaStorageResult, Storage};
26+
27+
#[macro_export]
28+
macro_rules! define_short_key_storage {
29+
($( ( $sizes:ty, $names:ident ) ),+ $(,)?) => {
30+
$(
31+
$crate::define_short_key_storage!($sizes, $names);
32+
)+
33+
};
34+
35+
($size:ty, $name:ident) => {
36+
/// A storage that hashes (using blake) each key to a $size - byte key.
37+
pub struct $name<S: Storage> {
38+
storage: S,
39+
_n_bytes: PhantomData<$size>,
40+
}
41+
42+
impl<S: Storage> $name<S> {
43+
pub fn new(storage: S) -> Self {
44+
Self { storage, _n_bytes: PhantomData }
45+
}
46+
47+
pub fn small_key(key: &DbKey) -> DbKey {
48+
let mut hasher = Blake2s::<$size>::new();
49+
hasher.update(key.0.as_slice());
50+
let result = hasher.finalize();
51+
DbKey(result.as_slice().to_vec())
52+
}
53+
}
54+
55+
impl<S: Storage> Storage for $name<S> {
56+
fn get(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
57+
self.storage.get(&Self::small_key(key))
58+
}
59+
60+
fn set(&mut self, key: DbKey, value: DbValue) -> PatriciaStorageResult<Option<DbValue>> {
61+
self.storage.set(Self::small_key(&key), value)
62+
}
63+
64+
fn mget(&mut self, keys: &[&DbKey]) -> PatriciaStorageResult<Vec<Option<DbValue>>> {
65+
let small_keys = keys
66+
.iter()
67+
.map(|key| Self::small_key(key))
68+
.collect::<Vec<_>>();
69+
self.storage.mget(small_keys.iter().collect::<Vec<&DbKey>>().as_slice())
70+
}
71+
72+
fn mset(&mut self, key_to_value: DbHashMap) -> PatriciaStorageResult<()> {
73+
self.storage.mset(
74+
key_to_value
75+
.into_iter()
76+
.map(|(key, value)| (Self::small_key(&key), value))
77+
.collect()
78+
)
79+
}
80+
81+
fn delete(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
82+
self.storage.delete(&Self::small_key(key))
83+
}
84+
}
85+
};
86+
}
87+
88+
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+
);

0 commit comments

Comments
 (0)