Skip to content

Commit f1271ca

Browse files
starknet_committer: collect siblings when traversing the tree
1 parent 180e776 commit f1271ca

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

crates/starknet_committer/src/db/create_original_skeleton_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ pub(crate) async fn test_create_original_skeleton<L, Layout>(
188188
&config,
189189
&leaf_modifications,
190190
None,
191+
None,
191192
&EmptyKeyContext,
192193
)
193194
.await

crates/starknet_committer/src/db/external_test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ where
4242
&config,
4343
&leaf_modifications.iter().map(|(k, v)| (*k, v.clone().into())).collect(),
4444
None,
45+
None,
4546
key_context,
4647
)
4748
.await

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub async fn get_leaves<'a, L: Leaf>(
3434
&config,
3535
&leaf_modifications,
3636
Some(&mut previous_leaves),
37+
None,
3738
key_context,
3839
)
3940
.await?;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
6969
let mut current_subtrees = subtrees;
7070
let mut next_subtrees = Vec::new();
7171
while !current_subtrees.is_empty() {
72-
let filled_roots =
73-
get_roots_from_storage::<L, FactsNodeLayout>(&current_subtrees, storage, key_context)
74-
.await?;
72+
let filled_roots = get_roots_from_storage::<L, FactsNodeLayout>(
73+
&current_subtrees,
74+
storage,
75+
key_context,
76+
None,
77+
)
78+
.await?;
7579
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) {
7680
match filled_root.data {
7781
// Binary node.

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use starknet_patricia::patricia_merkle_tree::traversal::{
2727
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
2828
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext, HasStaticPrefix};
2929
use starknet_patricia_storage::errors::StorageError;
30-
use starknet_patricia_storage::storage_trait::{AsyncStorage, DbKey, Storage};
30+
use starknet_patricia_storage::storage_trait::{AsyncStorage, DbHashMap, DbKey, Storage};
3131
use tracing::warn;
3232

3333
use crate::block_committer::input::{
@@ -47,6 +47,7 @@ macro_rules! log_trivial_modification {
4747
};
4848
}
4949

50+
#[allow(clippy::too_many_arguments)]
5051
/// Fetches the Patricia witnesses, required to build the original skeleton tree from storage.
5152
///
5253
/// Given a list of subtrees, traverses towards their leaves and fetches all non-empty,
@@ -62,6 +63,7 @@ pub(crate) async fn fetch_nodes<'a, L, Layout>(
6263
leaf_modifications: &LeafModifications<L>,
6364
config: &impl OriginalSkeletonTreeConfig,
6465
mut previous_leaves: Option<&mut HashMap<NodeIndex, L>>,
66+
mut siblings_map: Option<&mut DbHashMap>,
6567
key_context: &<L as HasStaticPrefix>::KeyContext,
6668
) -> OriginalSkeletonTreeResult<()>
6769
where
@@ -73,8 +75,13 @@ where
7375
let should_fetch_modified_leaves =
7476
config.compare_modified_leaves() || previous_leaves.is_some();
7577
while !current_subtrees.is_empty() {
76-
let filled_roots =
77-
get_roots_from_storage::<L, Layout>(&current_subtrees, storage, key_context).await?;
78+
let filled_roots = get_roots_from_storage::<L, Layout>(
79+
&current_subtrees,
80+
storage,
81+
key_context,
82+
siblings_map.as_deref_mut(),
83+
)
84+
.await?;
7885
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.into_iter()) {
7986
if subtree.is_unmodified() {
8087
handle_unmodified_subtree(skeleton_tree, &mut next_subtrees, filled_root, subtree);
@@ -244,17 +251,21 @@ pub async fn get_roots_from_storage<'a, L: Leaf, Layout: NodeLayout<'a, L>>(
244251
subtrees: &[Layout::SubTree],
245252
storage: &impl Storage,
246253
key_context: &<L as HasStaticPrefix>::KeyContext,
254+
mut siblings_map: Option<&mut DbHashMap>,
247255
) -> TraversalResult<Vec<FilledNode<L, Layout::NodeData>>> {
248256
let mut subtrees_roots = vec![];
249257
let db_keys: Vec<DbKey> =
250258
subtrees.iter().map(|subtree| subtree.get_root_db_key::<L>(key_context)).collect();
251259

252260
let db_vals = storage.mget(&db_keys.iter().collect::<Vec<&DbKey>>()).await?;
253-
for ((subtree, optional_val), db_key) in subtrees.iter().zip(db_vals.iter()).zip(db_keys) {
261+
for ((subtree, optional_val), db_key) in subtrees.iter().zip(db_vals.into_iter()).zip(db_keys) {
254262
let Some(val) = optional_val else { Err(StorageError::MissingKey(db_key))? };
255263
let filled_node =
256-
Layout::NodeDbObject::deserialize(val, &subtree.get_root_context())?.into();
264+
Layout::NodeDbObject::deserialize(&val, &subtree.get_root_context())?.into();
257265
subtrees_roots.push(filled_node);
266+
if subtree.is_unmodified() {
267+
siblings_map.as_mut().map(|m| m.insert(db_key, val));
268+
}
258269
}
259270
Ok(subtrees_roots)
260271
}
@@ -279,6 +290,7 @@ pub(crate) fn log_warning_for_empty_leaves<L: Leaf, T: Borrow<NodeIndex> + Debug
279290
Ok(())
280291
}
281292

293+
#[allow(clippy::too_many_arguments)]
282294
/// Creates an original skeleton tree by fetching Patricia nodes from storage.
283295
///
284296
/// Traverses the trie from the root towards the modified leaves, collecting all nodes needed
@@ -300,6 +312,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf, Layout: NodeLayout<'a, L
300312
config: &impl OriginalSkeletonTreeConfig,
301313
leaf_modifications: &LeafModifications<L>,
302314
previous_leaves: Option<&mut HashMap<NodeIndex, L>>,
315+
siblings_map: Option<&mut DbHashMap>,
303316
key_context: &<L as HasStaticPrefix>::KeyContext,
304317
) -> OriginalSkeletonTreeResult<OriginalSkeletonTreeImpl<'a>> {
305318
if sorted_leaf_indices.is_empty() {
@@ -332,6 +345,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf, Layout: NodeLayout<'a, L
332345
leaf_modifications,
333346
config,
334347
previous_leaves,
348+
siblings_map,
335349
key_context,
336350
)
337351
.await?;
@@ -396,6 +410,7 @@ where
396410
&config,
397411
&HashMap::new(),
398412
Some(&mut leaves),
413+
None,
399414
&EmptyKeyContext,
400415
)
401416
.await?;
@@ -433,6 +448,7 @@ where
433448
.map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value)))
434449
.collect(),
435450
None,
451+
None,
436452
&EmptyKeyContext,
437453
)
438454
.await?)
@@ -542,6 +558,7 @@ where
542558
&trie_config,
543559
&leaf_modifications,
544560
None,
561+
None,
545562
&address,
546563
)
547564
.await?;

0 commit comments

Comments
 (0)