Skip to content

Commit 134e8a5

Browse files
committed
starknet_committer,starknet_patricia,starknet_patricia_storage: add KeyContext
1 parent 2834655 commit 134e8a5

File tree

18 files changed

+127
-47
lines changed

18 files changed

+127
-47
lines changed

crates/starknet_committer/src/db/external_test_utils.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::{
1515
UpdatedSkeletonTree,
1616
UpdatedSkeletonTreeImpl,
1717
};
18+
use starknet_patricia_storage::db_object::HasStaticPrefix;
1819
use starknet_patricia_storage::map_storage::MapStorage;
1920

2021
use crate::db::facts_db::create_facts_tree::create_original_skeleton_tree;
@@ -27,7 +28,7 @@ pub async fn tree_computation_flow<L, TH>(
2728
) -> FilledTreeImpl<L>
2829
where
2930
TH: TreeHashFunction<L> + 'static,
30-
L: Leaf + 'static,
31+
L: Leaf + HasStaticPrefix<KeyContext = ()> + 'static,
3132
{
3233
let mut sorted_leaf_indices: Vec<NodeIndex> = leaf_modifications.keys().copied().collect();
3334
let sorted_leaf_indices = SortedLeafIndices::new(&mut sorted_leaf_indices);
@@ -37,6 +38,7 @@ where
3738
sorted_leaf_indices,
3839
&config,
3940
&leaf_modifications,
41+
&(),
4042
)
4143
.await
4244
.expect("Failed to create the original skeleton tree");
@@ -63,7 +65,10 @@ where
6365
.expect("Failed to create the filled tree")
6466
}
6567

66-
pub async fn single_tree_flow_test<L: Leaf + 'static, TH: TreeHashFunction<L> + 'static>(
68+
pub async fn single_tree_flow_test<
69+
L: Leaf + HasStaticPrefix<KeyContext = ()> + 'static,
70+
TH: TreeHashFunction<L> + 'static,
71+
>(
6772
leaf_modifications: LeafModifications<L>,
6873
storage: &mut MapStorage,
6974
root_hash: HashOutput,
@@ -85,7 +90,7 @@ pub async fn single_tree_flow_test<L: Leaf + 'static, TH: TreeHashFunction<L> +
8590
let json_hash = &json!(hash_result.0.to_hex_string());
8691
result_map.insert("root_hash", json_hash);
8792
// Serlialize the storage modifications.
88-
let json_storage = &json!(filled_tree.serialize());
93+
let json_storage = &json!(filled_tree.serialize(&()));
8994
result_map.insert("storage_changes", json_storage);
9095
serde_json::to_string(&result_map).expect("serialization failed")
9196
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::{
2020
};
2121
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
2222
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
23+
use starknet_patricia_storage::db_object::HasStaticPrefix;
2324
use starknet_patricia_storage::storage_trait::Storage;
2425
use tracing::warn;
2526

@@ -48,13 +49,15 @@ async fn fetch_nodes<'a, L: Leaf>(
4849
leaf_modifications: &LeafModifications<L>,
4950
config: &impl OriginalSkeletonTreeConfig<L>,
5051
mut previous_leaves: Option<&mut HashMap<NodeIndex, L>>,
52+
key_context: &<L as HasStaticPrefix>::KeyContext,
5153
) -> OriginalSkeletonTreeResult<()> {
5254
let mut current_subtrees = subtrees;
5355
let mut next_subtrees = Vec::new();
5456
while !current_subtrees.is_empty() {
5557
let should_fetch_modified_leaves =
5658
config.compare_modified_leaves() || previous_leaves.is_some();
57-
let filled_roots = calculate_subtrees_roots::<L>(&current_subtrees, storage).await?;
59+
let filled_roots =
60+
calculate_subtrees_roots::<L>(&current_subtrees, storage, key_context).await?;
5861
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) {
5962
match filled_root.data {
6063
// Binary node.
@@ -150,6 +153,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf>(
150153
sorted_leaf_indices: SortedLeafIndices<'a>,
151154
config: &impl OriginalSkeletonTreeConfig<L>,
152155
leaf_modifications: &LeafModifications<L>,
156+
key_context: &<L as HasStaticPrefix>::KeyContext,
153157
) -> OriginalSkeletonTreeResult<OriginalSkeletonTreeImpl<'a>> {
154158
if sorted_leaf_indices.is_empty() {
155159
return Ok(OriginalSkeletonTreeImpl::create_unmodified(root_hash));
@@ -171,6 +175,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf>(
171175
leaf_modifications,
172176
config,
173177
None,
178+
key_context,
174179
)
175180
.await?;
176181
Ok(skeleton_tree)
@@ -182,6 +187,7 @@ pub async fn create_original_skeleton_tree_and_get_previous_leaves<'a, L: Leaf>(
182187
sorted_leaf_indices: SortedLeafIndices<'a>,
183188
leaf_modifications: &LeafModifications<L>,
184189
config: &impl OriginalSkeletonTreeConfig<L>,
190+
key_context: &<L as HasStaticPrefix>::KeyContext,
185191
) -> OriginalSkeletonTreeResult<(OriginalSkeletonTreeImpl<'a>, HashMap<NodeIndex, L>)> {
186192
if sorted_leaf_indices.is_empty() {
187193
let unmodified = OriginalSkeletonTreeImpl::create_unmodified(root_hash);
@@ -203,6 +209,7 @@ pub async fn create_original_skeleton_tree_and_get_previous_leaves<'a, L: Leaf>(
203209
leaf_modifications,
204210
config,
205211
Some(&mut leaves),
212+
key_context,
206213
)
207214
.await?;
208215
Ok((skeleton_tree, leaves))
@@ -212,6 +219,7 @@ pub async fn get_leaves<'a, L: Leaf>(
212219
storage: &mut impl Storage,
213220
root_hash: HashOutput,
214221
sorted_leaf_indices: SortedLeafIndices<'a>,
222+
key_context: &<L as HasStaticPrefix>::KeyContext,
215223
) -> OriginalSkeletonTreeResult<HashMap<NodeIndex, L>> {
216224
let config = NoCompareOriginalSkeletonTrieConfig::default();
217225
let leaf_modifications = LeafModifications::new();
@@ -221,6 +229,7 @@ pub async fn get_leaves<'a, L: Leaf>(
221229
sorted_leaf_indices,
222230
&leaf_modifications,
223231
&config,
232+
key_context,
224233
)
225234
.await?;
226235
Ok(previous_leaves)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ async fn test_create_tree(
217217
sorted_leaf_indices,
218218
&config,
219219
&leaf_modifications,
220+
&(),
220221
)
221222
.await
222223
.unwrap();
@@ -225,7 +226,7 @@ async fn test_create_tree(
225226

226227
pub(crate) fn create_mock_leaf_entry(val: u128) -> (DbKey, DbValue) {
227228
let leaf = MockLeaf(Felt::from(val));
228-
(leaf.get_db_key(&leaf.0.to_bytes_be()), leaf.serialize())
229+
(leaf.get_db_key(&(), &leaf.0.to_bytes_be()), leaf.serialize())
229230
}
230231

231232
fn create_mock_leaf_modifications(

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<S: Storage> FactsDb<S> {
5454
contracts_trie_sorted_indices,
5555
&HashMap::new(),
5656
&OriginalSkeletonContractsTrieConfig::new(),
57+
&(),
5758
)
5859
.await?)
5960
}
@@ -82,6 +83,7 @@ impl<S: Storage> FactsDb<S> {
8283
*sorted_leaf_indices,
8384
&config,
8485
updates,
86+
&(),
8587
)
8688
.await?;
8789
storage_tries.insert(*address, original_skeleton);
@@ -104,6 +106,7 @@ impl<S: Storage> FactsDb<S> {
104106
contracts_trie_sorted_indices,
105107
&config,
106108
actual_classes_updates,
109+
&(),
107110
)
108111
.await?)
109112
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
1010
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
1111
use starknet_patricia::patricia_merkle_tree::traversal::{SubTreeTrait, TraversalResult};
1212
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
13+
use starknet_patricia_storage::db_object::HasStaticPrefix;
1314
use starknet_patricia_storage::errors::StorageError;
1415
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, Storage};
1516

@@ -23,12 +24,16 @@ pub mod traversal_test;
2324
pub async fn calculate_subtrees_roots<'a, L: Leaf>(
2425
subtrees: &[FactsSubTree<'a>],
2526
storage: &mut impl Storage,
27+
key_context: &<L as HasStaticPrefix>::KeyContext,
2628
) -> TraversalResult<Vec<FilledNode<L>>> {
2729
let mut subtrees_roots = vec![];
2830
let db_keys: Vec<DbKey> = subtrees
2931
.iter()
3032
.map(|subtree| {
31-
create_db_key(subtree.get_root_prefix::<L>(), &subtree.root_hash.0.to_bytes_be())
33+
create_db_key(
34+
subtree.get_root_prefix::<L>(key_context),
35+
&subtree.root_hash.0.to_bytes_be(),
36+
)
3237
})
3338
.collect();
3439

@@ -49,6 +54,7 @@ pub async fn fetch_patricia_paths<L: Leaf>(
4954
root_hash: HashOutput,
5055
sorted_leaf_indices: SortedLeafIndices<'_>,
5156
leaves: Option<&mut HashMap<NodeIndex, L>>,
57+
key_context: &<L as HasStaticPrefix>::KeyContext,
5258
) -> TraversalResult<PreimageMap> {
5359
let mut witnesses = PreimageMap::new();
5460

@@ -58,7 +64,14 @@ pub async fn fetch_patricia_paths<L: Leaf>(
5864

5965
let main_subtree = FactsSubTree::create(sorted_leaf_indices, NodeIndex::ROOT, root_hash);
6066

61-
fetch_patricia_paths_inner::<L>(storage, vec![main_subtree], &mut witnesses, leaves).await?;
67+
fetch_patricia_paths_inner::<L>(
68+
storage,
69+
vec![main_subtree],
70+
&mut witnesses,
71+
leaves,
72+
key_context,
73+
)
74+
.await?;
6275
Ok(witnesses)
6376
}
6477

@@ -74,11 +87,13 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
7487
subtrees: Vec<FactsSubTree<'a>>,
7588
witnesses: &mut PreimageMap,
7689
mut leaves: Option<&mut HashMap<NodeIndex, L>>,
90+
key_context: &<L as HasStaticPrefix>::KeyContext,
7791
) -> TraversalResult<()> {
7892
let mut current_subtrees = subtrees;
7993
let mut next_subtrees = Vec::new();
8094
while !current_subtrees.is_empty() {
81-
let filled_roots = calculate_subtrees_roots::<L>(&current_subtrees, storage).await?;
95+
let filled_roots =
96+
calculate_subtrees_roots::<L>(&current_subtrees, storage, key_context).await?;
8297
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) {
8398
// Always insert root.
8499
// No need to insert an unmodified node (which is not the root), because its parent is

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async fn test_fetch_patricia_paths_inner_impl(
8787
vec![main_subtree],
8888
&mut nodes,
8989
Some(&mut fetched_leaves),
90+
&(),
9091
)
9192
.await
9293
.unwrap();

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::PatriciaPr
33
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
44
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
55
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
6+
use starknet_patricia_storage::db_object::HasStaticPrefix;
67
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
78

89
#[derive(Debug, PartialEq)]
@@ -35,9 +36,12 @@ impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
3536
false
3637
}
3738

38-
fn get_root_prefix<L: Leaf>(&self) -> DbKeyPrefix {
39+
fn get_root_prefix<L: Leaf>(
40+
&self,
41+
key_context: &<L as HasStaticPrefix>::KeyContext,
42+
) -> DbKeyPrefix {
3943
if self.is_leaf() {
40-
PatriciaPrefix::Leaf(L::get_static_prefix()).into()
44+
PatriciaPrefix::Leaf(L::get_static_prefix(key_context)).into()
4145
} else {
4246
PatriciaPrefix::InnerNode.into()
4347
}

crates/starknet_committer/src/forest/filled_forest.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ impl FilledForest {
5959
fn serialize_trees(&self) -> DbHashMap {
6060
self.storage_tries
6161
.values()
62-
.flat_map(|tree| tree.serialize().into_iter())
63-
.chain(self.contracts_trie.serialize())
64-
.chain(self.classes_trie.serialize())
62+
.flat_map(|tree| tree.serialize(&()).into_iter())
63+
.chain(self.contracts_trie.serialize(&()))
64+
.chain(self.classes_trie.serialize(&()))
6565
.collect()
6666
}
67-
6867
async fn write_hashmap_to_storage(
6968
&self,
7069
storage: &mut impl Storage,
@@ -164,8 +163,8 @@ impl FilledForest {
164163
contract_address_to_storage_skeleton.len(),
165164
contract_address_to_storage_updates.len()
166165
);
167-
// `contract_address_to_storage_updates` includes all modified contracts, even those with
168-
// unmodified storage, see StateDiff::actual_storage_updates().
166+
// `contract_address_to_storage_updates` includes all modified contracts, even those
167+
// with unmodified storage, see StateDiff::actual_storage_updates().
169168
for (contract_address, storage_updates) in contract_address_to_storage_updates {
170169
let node_index = contract_address_into_node_index(&contract_address);
171170
let original_contract_state = original_contracts_trie_leaves

crates/starknet_committer/src/forest/skeleton_forest_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ macro_rules! compare_skeleton_tree {
5050

5151
pub(crate) fn create_storage_leaf_entry(val: u128) -> (DbKey, DbValue) {
5252
let leaf = StarknetStorageValue(Felt::from(val));
53-
(leaf.get_db_key(&leaf.0.to_bytes_be()), leaf.serialize())
53+
(leaf.get_db_key(&(), &leaf.0.to_bytes_be()), leaf.serialize())
5454
}
5555

5656
pub(crate) fn create_compiled_class_leaf_entry(val: u128) -> (DbKey, DbValue) {
5757
let leaf = CompiledClassHash(Felt::from(val));
58-
(leaf.get_db_key(&leaf.0.to_bytes_be()), leaf.serialize())
58+
(leaf.get_db_key(&(), &leaf.0.to_bytes_be()), leaf.serialize())
5959
}
6060

6161
pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (DbKey, DbValue) {
@@ -65,7 +65,7 @@ pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (DbKey, DbValue) {
6565
storage_root_hash: HashOutput(felt),
6666
class_hash: ClassHash(felt),
6767
};
68-
(leaf.get_db_key(&felt.to_bytes_be()), leaf.serialize())
68+
(leaf.get_db_key(&(), &felt.to_bytes_be()), leaf.serialize())
6969
}
7070

7171
// This test uses addition hash for simplicity (i.e hash(a,b) = a + b).

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::errors::{LeafError, Leaf
55
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
66
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
77
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;
8-
use starknet_patricia_storage::db_object::HasStaticPrefix;
8+
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
99
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
1010
use starknet_types_core::felt::Felt;
1111

@@ -22,7 +22,8 @@ pub struct ContractState {
2222
}
2323

2424
impl HasStaticPrefix for StarknetStorageValue {
25-
fn get_static_prefix() -> DbKeyPrefix {
25+
type KeyContext = EmptyKeyContext;
26+
fn get_static_prefix(_key_context: &Self::KeyContext) -> DbKeyPrefix {
2627
CommitterLeafPrefix::StorageLeaf.into()
2728
}
2829
}
@@ -41,7 +42,8 @@ impl Leaf for StarknetStorageValue {
4142
}
4243

4344
impl HasStaticPrefix for CompiledClassHash {
44-
fn get_static_prefix() -> DbKeyPrefix {
45+
type KeyContext = EmptyKeyContext;
46+
fn get_static_prefix(_key_context: &Self::KeyContext) -> DbKeyPrefix {
4547
CommitterLeafPrefix::CompiledClassLeaf.into()
4648
}
4749
}
@@ -60,7 +62,8 @@ impl Leaf for CompiledClassHash {
6062
}
6163

6264
impl HasStaticPrefix for ContractState {
63-
fn get_static_prefix() -> DbKeyPrefix {
65+
type KeyContext = ();
66+
fn get_static_prefix(_key_context: &Self::KeyContext) -> DbKeyPrefix {
6467
CommitterLeafPrefix::StateTreeLeaf.into()
6568
}
6669
}

0 commit comments

Comments
 (0)