Skip to content

Commit 26d94e0

Browse files
committed
starknet_committer,starknet_patricia: add subtree trait
1 parent e7bb1ee commit 26d94e0

File tree

14 files changed

+147
-98
lines changed

14 files changed

+147
-98
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
pub mod create_facts_tree;
21
#[cfg(any(feature = "testing", test))]
32
pub mod external_test_utils;
43
pub mod facts_db;
54
pub mod forest_trait;
6-
pub mod traversal;

crates/starknet_committer/src/db/external_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::{
1717
};
1818
use starknet_patricia_storage::map_storage::MapStorage;
1919

20-
use crate::db::create_facts_tree::create_original_skeleton_tree;
20+
use crate::db::facts_db::create_facts_tree::create_original_skeleton_tree;
2121

2222
pub async fn tree_computation_flow<L, TH>(
2323
leaf_modifications: LeafModifications<L>,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::{
1818
OriginalSkeletonTreeImpl,
1919
OriginalSkeletonTreeResult,
2020
};
21-
use starknet_patricia::patricia_merkle_tree::traversal::SubTree;
21+
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
2222
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
2323
use starknet_patricia_storage::storage_trait::Storage;
2424
use tracing::warn;
2525

26-
use crate::db::traversal::calculate_subtrees_roots;
26+
use crate::db::facts_db::traversal::calculate_subtrees_roots;
27+
use crate::db::facts_db::types::FactsSubTree;
2728

2829
#[cfg(test)]
2930
#[path = "create_facts_tree_test.rs"]
@@ -42,7 +43,7 @@ macro_rules! log_trivial_modification {
4243
/// encountering a trivial modification. Fills the previous leaf values if it is not none.
4344
async fn fetch_nodes<'a, L: Leaf>(
4445
skeleton_tree: &mut OriginalSkeletonTreeImpl<'a>,
45-
subtrees: Vec<SubTree<'a>>,
46+
subtrees: Vec<FactsSubTree<'a>>,
4647
storage: &mut impl Storage,
4748
leaf_modifications: &LeafModifications<L>,
4849
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -101,7 +102,7 @@ async fn fetch_nodes<'a, L: Leaf>(
101102
leaves.extend(
102103
previously_empty_leaves_indices
103104
.iter()
104-
.map(|idx| (**idx, L::default()))
105+
.map(|idx| (*idx, L::default()))
105106
.collect::<HashMap<NodeIndex, L>>(),
106107
);
107108
}
@@ -161,7 +162,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf>(
161162
)?;
162163
return Ok(OriginalSkeletonTreeImpl::create_empty(sorted_leaf_indices));
163164
}
164-
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
165+
let main_subtree = FactsSubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
165166
let mut skeleton_tree = OriginalSkeletonTreeImpl { nodes: HashMap::new(), sorted_leaf_indices };
166167
fetch_nodes::<L>(
167168
&mut skeleton_tree,
@@ -192,7 +193,7 @@ pub async fn create_original_skeleton_tree_and_get_previous_leaves<'a, L: Leaf>(
192193
sorted_leaf_indices.get_indices().iter().map(|idx| (*idx, L::default())).collect(),
193194
));
194195
}
195-
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
196+
let main_subtree = FactsSubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
196197
let mut skeleton_tree = OriginalSkeletonTreeImpl { nodes: HashMap::new(), sorted_leaf_indices };
197198
let mut leaves = HashMap::new();
198199
fetch_nodes::<L>(
@@ -229,8 +230,8 @@ pub async fn get_leaves<'a, L: Leaf>(
229230
/// referred subtree or not.
230231
fn handle_subtree<'a>(
231232
skeleton_tree: &mut OriginalSkeletonTreeImpl<'a>,
232-
next_subtrees: &mut Vec<SubTree<'a>>,
233-
subtree: SubTree<'a>,
233+
next_subtrees: &mut Vec<FactsSubTree<'a>>,
234+
subtree: FactsSubTree<'a>,
234235
should_fetch_modified_leaves: bool,
235236
) {
236237
if !subtree.is_leaf() || (should_fetch_modified_leaves && !subtree.is_unmodified()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use starknet_patricia_storage::map_storage::MapStorage;
2323
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue};
2424
use starknet_types_core::felt::Felt;
2525

26-
use crate::db::create_facts_tree::create_original_skeleton_tree;
26+
use crate::db::facts_db::create_facts_tree::create_original_skeleton_tree;
2727

2828
#[tokio::test]
2929
#[rstest]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::block_committer::input::{
1414
ConfigImpl,
1515
StarknetStorageValue,
1616
};
17-
use crate::db::create_facts_tree::{
17+
use crate::db::facts_db::create_facts_tree::{
1818
create_original_skeleton_tree,
1919
create_original_skeleton_tree_and_get_previous_leaves,
2020
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod create_facts_tree;
2+
pub mod db;
3+
pub mod traversal;
4+
pub mod types;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
88
PreimageMap,
99
};
1010
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
11-
use starknet_patricia::patricia_merkle_tree::traversal::{SubTree, TraversalResult};
11+
use starknet_patricia::patricia_merkle_tree::traversal::{SubTreeTrait, TraversalResult};
1212
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
1313
use starknet_patricia_storage::errors::StorageError;
1414
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, Storage};
1515

16+
use crate::db::facts_db::types::FactsSubTree;
17+
1618
#[cfg(test)]
1719
#[path = "traversal_test.rs"]
1820
pub mod traversal_test;
1921

2022
// TODO(Aviv, 17/07/2024): Split between storage prefix implementation and function logic.
2123
pub async fn calculate_subtrees_roots<'a, L: Leaf>(
22-
subtrees: &[SubTree<'a>],
24+
subtrees: &[FactsSubTree<'a>],
2325
storage: &mut impl Storage,
2426
) -> TraversalResult<Vec<FilledNode<L>>> {
2527
let mut subtrees_roots = vec![];
@@ -54,7 +56,7 @@ pub async fn fetch_patricia_paths<L: Leaf>(
5456
return Ok(witnesses);
5557
}
5658

57-
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
59+
let main_subtree = FactsSubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
5860

5961
fetch_patricia_paths_inner::<L>(storage, vec![main_subtree], &mut witnesses, leaves).await?;
6062
Ok(witnesses)
@@ -67,9 +69,9 @@ pub async fn fetch_patricia_paths<L: Leaf>(
6769
/// inner nodes in their paths.
6870
/// If `leaves` is not `None`, it also fetches the modified leaves and inserts them into the
6971
/// provided map.
70-
async fn fetch_patricia_paths_inner<'a, L: Leaf>(
72+
pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
7173
storage: &mut impl Storage,
72-
subtrees: Vec<SubTree<'a>>,
74+
subtrees: Vec<FactsSubTree<'a>>,
7375
witnesses: &mut PreimageMap,
7476
mut leaves: Option<&mut HashMap<NodeIndex, L>>,
7577
) -> TraversalResult<()> {
@@ -103,7 +105,7 @@ async fn fetch_patricia_paths_inner<'a, L: Leaf>(
103105
// Insert empty leaves descendent of the current subtree, that are not
104106
// descendents of the bottom node.
105107
for index in empty_leaves_indices {
106-
leaves_map.insert(*index, L::default());
108+
leaves_map.insert(index, L::default());
107109
}
108110
}
109111
next_subtrees.push(bottom_subtree);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
2525
Preimage,
2626
PreimageMap,
2727
};
28-
use starknet_patricia::patricia_merkle_tree::traversal::SubTree;
28+
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
2929
use starknet_patricia::patricia_merkle_tree::types::{SortedLeafIndices, SubTreeHeight};
3030
use starknet_patricia_storage::map_storage::MapStorage;
3131
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue};
3232
use starknet_types_core::felt::Felt;
3333
use starknet_types_core::hash::Pedersen;
3434

35-
use super::fetch_patricia_paths_inner;
35+
use crate::db::facts_db::traversal::fetch_patricia_paths_inner;
36+
use crate::db::facts_db::types::FactsSubTree;
3637

3738
fn to_preimage_map(raw_preimages: HashMap<u32, Vec<u32>>) -> PreimageMap {
3839
raw_preimages
@@ -73,7 +74,7 @@ async fn test_fetch_patricia_paths_inner_impl(
7374
.iter()
7475
.map(|&idx| small_tree_index_to_full(U256::from(idx), height))
7576
.collect::<Vec<_>>();
76-
let main_subtree = SubTree {
77+
let main_subtree = FactsSubTree {
7778
sorted_leaf_indices: SortedLeafIndices::new(&mut leaf_indices),
7879
root_index: small_tree_index_to_full(U256::ONE, height),
7980
root_hash,
@@ -514,10 +515,10 @@ struct TestPatriciaPathsInput {
514515
/// The files names indicate the tree height, number of initial leaves and number of modified
515516
/// leaves. The hash function used in the python tests is Pedersen.
516517
/// The leaves values are their NodeIndices.
517-
#[case(include_str!("../../resources/fetch_patricia_paths_test_10_200_50.json"))]
518-
#[case(include_str!("../../resources/fetch_patricia_paths_test_10_5_2.json"))]
519-
#[case(include_str!("../../resources/fetch_patricia_paths_test_10_100_30.json"))]
520-
#[case(include_str!("../../resources/fetch_patricia_paths_test_8_120_70.json"))]
518+
#[case(include_str!("../../../resources/fetch_patricia_paths_test_10_200_50.json"))]
519+
#[case(include_str!("../../../resources/fetch_patricia_paths_test_10_5_2.json"))]
520+
#[case(include_str!("../../../resources/fetch_patricia_paths_test_10_100_30.json"))]
521+
#[case(include_str!("../../../resources/fetch_patricia_paths_test_8_120_70.json"))]
521522
async fn test_fetch_patricia_paths_inner_from_json(#[case] input_data: &str) {
522523
let input: TestPatriciaPathsInput = serde_json::from_str(input_data)
523524
.unwrap_or_else(|error| panic!("JSON was not well-formatted: {error:?}"));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use starknet_api::hash::HashOutput;
2+
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::PatriciaPrefix;
3+
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
4+
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
5+
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
6+
7+
#[derive(Debug, PartialEq)]
8+
pub struct FactsSubTree<'a> {
9+
pub sorted_leaf_indices: SortedLeafIndices<'a>,
10+
pub root_index: NodeIndex,
11+
pub root_hash: HashOutput,
12+
}
13+
14+
impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
15+
type ChildData = HashOutput;
16+
fn create_child(
17+
sorted_leaf_indices: SortedLeafIndices<'a>,
18+
root_index: NodeIndex,
19+
child_data: Self::ChildData,
20+
) -> Self {
21+
Self { sorted_leaf_indices, root_index, root_hash: child_data }
22+
}
23+
fn get_root_index(&self) -> NodeIndex {
24+
self.root_index
25+
}
26+
fn get_sorted_leaf_indices(&self) -> &SortedLeafIndices<'a> {
27+
&self.sorted_leaf_indices
28+
}
29+
fn should_traverse_unmodified_children() -> bool {
30+
false
31+
}
32+
}
33+
34+
impl<'a> FactsSubTree<'a> {
35+
pub fn get_root_prefix<L: Leaf>(&self) -> PatriciaPrefix {
36+
if self.is_leaf() {
37+
PatriciaPrefix::Leaf(L::get_static_prefix())
38+
} else {
39+
PatriciaPrefix::InnerNode
40+
}
41+
}
42+
}

crates/starknet_committer/src/forest/skeleton_forest_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::block_committer::input::{
3232
StarknetStorageValue,
3333
StateDiff,
3434
};
35-
use crate::db::facts_db::FactsDb;
35+
use crate::db::facts_db::db::FactsDb;
3636
use crate::db::forest_trait::ForestReader;
3737
use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkeletonForest};
3838
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;

0 commit comments

Comments
 (0)