Skip to content

Commit 00a4204

Browse files
committed
starknet_committer,starknet_patricia: layout-dependent db-key separator
1 parent d188156 commit 00a4204

File tree

7 files changed

+61
-46
lines changed

7 files changed

+61
-46
lines changed

crates/starknet_committer/src/db/facts_db/types.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
77
use starknet_patricia::patricia_merkle_tree::traversal::{SubTreeTrait, UnmodifiedChildTraversal};
88
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
99
use starknet_patricia_storage::db_object::HasStaticPrefix;
10-
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
10+
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix};
1111

1212
use crate::block_committer::input::InputContext;
1313

@@ -46,19 +46,17 @@ impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
4646
Self::NodeDeserializeContext { is_leaf: self.is_leaf(), node_hash: self.root_hash }
4747
}
4848

49-
fn get_root_prefix<L: Leaf>(
49+
fn get_root_db_key<L: Leaf>(
5050
&self,
5151
key_context: &<L as HasStaticPrefix>::KeyContext,
52-
) -> DbKeyPrefix {
53-
if self.is_leaf() {
52+
) -> DbKey {
53+
let prefix: DbKeyPrefix = if self.is_leaf() {
5454
PatriciaPrefix::Leaf(L::get_static_prefix(key_context)).into()
5555
} else {
5656
PatriciaPrefix::InnerNode.into()
57-
}
58-
}
59-
60-
fn get_root_suffix(&self) -> Vec<u8> {
61-
self.root_hash.0.to_bytes_be().to_vec()
57+
};
58+
let suffix = self.root_hash.0.to_bytes_be();
59+
create_db_key(prefix, &suffix)
6260
}
6361
}
6462
/// Used for reading the roots in facts layout case.

crates/starknet_committer/src/db/index_db/leaves.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ use starknet_patricia_storage::db_object::{
88
DBObject,
99
EmptyDeserializationContext,
1010
EmptyKeyContext,
11+
HasDynamicPrefix,
1112
HasStaticPrefix,
1213
};
1314
use starknet_patricia_storage::errors::{DeserializationError, SerializationError};
14-
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue};
15+
use starknet_patricia_storage::storage_trait::{
16+
create_db_key_no_separator,
17+
DbKey,
18+
DbKeyPrefix,
19+
DbValue,
20+
};
1521
use starknet_types_core::felt::Felt;
1622

1723
use crate::block_committer::input::StarknetStorageValue;
@@ -107,6 +113,11 @@ impl_leaf_for_wrappers!(
107113

108114
impl DBObject for IndexLayoutContractState {
109115
type DeserializeContext = EmptyDeserializationContext;
116+
117+
fn get_db_key(&self, key_context: &Self::KeyContext, suffix: &[u8]) -> DbKey {
118+
create_db_key_no_separator(self.get_prefix(key_context), suffix)
119+
}
120+
110121
fn serialize(&self) -> Result<DbValue, SerializationError> {
111122
serialize_felts(&[self.0.class_hash.0, self.0.storage_root_hash.0, self.0.nonce.0])
112123
}
@@ -133,6 +144,10 @@ impl DBObject for IndexLayoutContractState {
133144
impl DBObject for IndexLayoutCompiledClassHash {
134145
type DeserializeContext = EmptyDeserializationContext;
135146

147+
fn get_db_key(&self, key_context: &Self::KeyContext, suffix: &[u8]) -> DbKey {
148+
create_db_key_no_separator(self.get_prefix(key_context), suffix)
149+
}
150+
136151
fn serialize(&self) -> Result<DbValue, SerializationError> {
137152
serialize_felts(&[self.0.0])
138153
}
@@ -150,6 +165,10 @@ impl DBObject for IndexLayoutCompiledClassHash {
150165
impl DBObject for IndexLayoutStarknetStorageValue {
151166
type DeserializeContext = EmptyDeserializationContext;
152167

168+
fn get_db_key(&self, key_context: &Self::KeyContext, suffix: &[u8]) -> DbKey {
169+
create_db_key_no_separator(self.get_prefix(key_context), suffix)
170+
}
171+
153172
fn serialize(&self) -> Result<DbValue, SerializationError> {
154173
serialize_felts(&[self.0.0])
155174
}

crates/starknet_committer/src/db/index_db/types.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_functio
1717
use starknet_patricia_storage::db_object::{
1818
DBObject,
1919
EmptyDeserializationContext,
20+
HasDynamicPrefix,
2021
HasStaticPrefix,
2122
};
2223
use starknet_patricia_storage::errors::{DeserializationError, SerializationResult};
23-
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue};
24+
use starknet_patricia_storage::storage_trait::{
25+
create_db_key_no_separator,
26+
DbKey,
27+
DbKeyPrefix,
28+
DbValue,
29+
};
2430
use starknet_types_core::felt::Felt;
2531

2632
use crate::hash_function::hash::TreeHashFunctionImpl;
@@ -58,6 +64,10 @@ where
5864
{
5965
type DeserializeContext = IndexNodeContext;
6066

67+
fn get_db_key(&self, key_context: &Self::KeyContext, suffix: &[u8]) -> DbKey {
68+
create_db_key_no_separator(self.get_prefix(key_context), suffix)
69+
}
70+
6171
fn serialize(&self) -> SerializationResult<DbValue> {
6272
match &self.0.data {
6373
// Binary node - only serialize the hash.
@@ -178,14 +188,9 @@ impl<'a> SubTreeTrait<'a> for IndexLayoutSubTree<'a> {
178188
Self::NodeDeserializeContext { is_leaf: self.is_leaf() }
179189
}
180190

181-
fn get_root_prefix<L: Leaf>(
182-
&self,
183-
key_context: &<L as HasStaticPrefix>::KeyContext,
184-
) -> DbKeyPrefix {
185-
L::get_static_prefix(key_context)
186-
}
187-
188-
fn get_root_suffix(&self) -> Vec<u8> {
189-
self.root_index.0.to_be_bytes().to_vec()
191+
fn get_root_db_key<L: Leaf>(&self, key_context: &<L as HasStaticPrefix>::KeyContext) -> DbKey {
192+
let prefix = L::get_static_prefix(key_context);
193+
let suffix = self.root_index.0.to_be_bytes();
194+
create_db_key_no_separator(prefix, &suffix)
190195
}
191196
}

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use starknet_patricia::patricia_merkle_tree::traversal::{
2626
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
2727
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext, HasStaticPrefix};
2828
use starknet_patricia_storage::errors::StorageError;
29-
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, Storage};
29+
use starknet_patricia_storage::storage_trait::{DbKey, Storage};
3030
use tracing::warn;
3131

3232
use crate::block_committer::input::{
@@ -245,12 +245,8 @@ pub async fn get_roots_from_storage<'a, L: Leaf, Layout: NodeLayout<'a, L>>(
245245
key_context: &<L as HasStaticPrefix>::KeyContext,
246246
) -> TraversalResult<Vec<FilledNode<L, Layout::NodeData>>> {
247247
let mut subtrees_roots = vec![];
248-
let db_keys: Vec<DbKey> = subtrees
249-
.iter()
250-
.map(|subtree| {
251-
create_db_key(subtree.get_root_prefix::<L>(key_context), &subtree.get_root_suffix())
252-
})
253-
.collect();
248+
let db_keys: Vec<DbKey> =
249+
subtrees.iter().map(|subtree| subtree.get_root_db_key::<L>(key_context)).collect();
254250

255251
let db_vals = storage.mget(&db_keys.iter().collect::<Vec<&DbKey>>()).await?;
256252
for ((subtree, optional_val), db_key) in subtrees.iter().zip(db_vals.iter()).zip(db_keys) {

crates/starknet_patricia/src/patricia_merkle_tree/traversal.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use starknet_api::hash::HashOutput;
22
use starknet_patricia_storage::db_object::HasStaticPrefix;
33
use starknet_patricia_storage::errors::{DeserializationError, StorageError};
4-
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, PatriciaStorageError};
4+
use starknet_patricia_storage::storage_trait::{DbKey, PatriciaStorageError};
55
use thiserror::Error;
66

77
use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;
@@ -116,14 +116,8 @@ pub trait SubTreeTrait<'a>: Sized {
116116
/// [crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTree].
117117
fn should_traverse_unmodified_child(data: Self::NodeData) -> UnmodifiedChildTraversal;
118118

119-
/// Returns the [DbKeyPrefix] of the root node.
120-
fn get_root_prefix<L: Leaf>(
121-
&self,
122-
key_context: &<L as HasStaticPrefix>::KeyContext,
123-
) -> DbKeyPrefix;
124-
125-
/// Returns the suffix of the root's db key.
126-
fn get_root_suffix(&self) -> Vec<u8>;
119+
/// Returns the full [DbKey] for the root node.
120+
fn get_root_db_key<L: Leaf>(&self, key_context: &<L as HasStaticPrefix>::KeyContext) -> DbKey;
127121

128122
/// Returns the `Self::NodeDeserializeContext` that's needed to deserialize the root node from a
129123
/// raw `DbValue`.

crates/starknet_patricia/src/patricia_merkle_tree/traversal_test.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ethnum::U256;
22
use pretty_assertions::assert_eq;
33
use rstest::rstest;
44
use starknet_patricia_storage::db_object::HasStaticPrefix;
5-
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
5+
use starknet_patricia_storage::storage_trait::DbKey;
66

77
use crate::patricia_merkle_tree::external_test_utils::{small_tree_index_to_full, TEST_PREFIX};
88
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePath, EdgePathLength, PathToBottom};
@@ -44,16 +44,12 @@ impl<'a> SubTreeTrait<'a> for TestSubTree<'a> {
4444

4545
fn get_root_context(&self) -> Self::NodeDeserializeContext {}
4646

47-
fn get_root_prefix<L: Leaf>(
47+
fn get_root_db_key<L: Leaf>(
4848
&self,
4949
_key_context: &<L as HasStaticPrefix>::KeyContext,
50-
) -> DbKeyPrefix {
51-
// Dummy prefix for testing purposes (we only need a prefix when interacting with storage).
52-
DbKeyPrefix::new(TEST_PREFIX.into())
53-
}
54-
55-
fn get_root_suffix(&self) -> Vec<u8> {
56-
vec![]
50+
) -> DbKey {
51+
// Dummy key for testing purposes (we only need a key when interacting with storage).
52+
DbKey(TEST_PREFIX.to_vec())
5753
}
5854
}
5955

crates/starknet_patricia_storage/src/storage_trait.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,18 @@ impl Serialize for DbKey {
205205
}
206206
}
207207

208-
/// Returns a `DbKey` from a prefix and a suffix.
208+
/// Returns a `DbKey` from a prefix and a suffix, with a ":" separator.
209+
/// This is used in the facts layout.
209210
pub fn create_db_key(prefix: DbKeyPrefix, suffix: &[u8]) -> DbKey {
210211
DbKey([prefix.to_bytes().to_vec(), b":".to_vec(), suffix.to_vec()].concat())
211212
}
212213

214+
/// Returns a `DbKey` from a prefix and a suffix, without a separator.
215+
/// This is used in the index layout.
216+
pub fn create_db_key_no_separator(prefix: DbKeyPrefix, suffix: &[u8]) -> DbKey {
217+
DbKey([prefix.to_bytes().to_vec(), suffix.to_vec()].concat())
218+
}
219+
213220
/// Extracts the suffix from a `DbKey`. If the key doesn't match the prefix, None is returned.
214221
pub fn try_extract_suffix_from_db_key<'a>(
215222
key: &'a DbKey,

0 commit comments

Comments
 (0)