Skip to content

Commit 815ce74

Browse files
committed
starknet_committer,starknet_patricia: enrich subtree trait to support fetch nodes
1 parent 525d944 commit 815ce74

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use starknet_api::hash::HashOutput;
2-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::PatriciaPrefix;
2+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
3+
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::{
4+
FactNodeDeserializationContext,
5+
PatriciaPrefix,
6+
};
37
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
48
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
59
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
@@ -13,8 +17,11 @@ pub struct FactsSubTree<'a> {
1317
pub root_hash: HashOutput,
1418
}
1519

20+
pub type FactDbFilledNode<L> = FilledNode<L, HashOutput>;
21+
1622
impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
1723
type NodeData = HashOutput;
24+
type NodeContext = FactNodeDeserializationContext;
1825

1926
fn create(
2027
sorted_leaf_indices: SortedLeafIndices<'a>,
@@ -36,6 +43,14 @@ impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
3643
false
3744
}
3845

46+
fn unmodified_child_hash(data: Self::NodeData) -> Option<HashOutput> {
47+
Some(data)
48+
}
49+
50+
fn get_root_context(&self) -> Self::NodeContext {
51+
Self::NodeContext { is_leaf: self.is_leaf(), node_hash: self.root_hash }
52+
}
53+
3954
fn get_root_prefix<L: Leaf>(
4055
&self,
4156
key_context: &<L as HasStaticPrefix>::KeyContext,
@@ -46,4 +61,8 @@ impl<'a> SubTreeTrait<'a> for FactsSubTree<'a> {
4661
PatriciaPrefix::InnerNode.into()
4762
}
4863
}
64+
65+
fn get_root_suffix(&self) -> Vec<u8> {
66+
self.root_hash.0.to_bytes_be().to_vec()
67+
}
4968
}

crates/starknet_patricia/src/patricia_merkle_tree/traversal.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use starknet_api::hash::HashOutput;
12
use starknet_patricia_storage::db_object::HasStaticPrefix;
23
use starknet_patricia_storage::errors::{DeserializationError, StorageError};
34
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, PatriciaStorageError};
@@ -29,6 +30,7 @@ pub type TraversalResult<T> = Result<T, TraversalError>;
2930
pub trait SubTreeTrait<'a>: Sized {
3031
/// Extra data a node can carry (e.g. its hash).
3132
type NodeData: Copy;
33+
type NodeContext;
3234

3335
/// Creates a concrete child node given its index and data.
3436
fn create(
@@ -102,9 +104,18 @@ pub trait SubTreeTrait<'a>: Sized {
102104
/// original skeleton tree.
103105
fn should_traverse_unmodified_children() -> bool;
104106

107+
/// When should_traverse_unmodified_children is false, this function is used to get the hash
108+
/// of the unmodified child (in this case NodeData will be HashOutput).
109+
fn unmodified_child_hash(data: Self::NodeData) -> Option<HashOutput>;
110+
105111
/// Returns the [DbKeyPrefix] of the root node.
106112
fn get_root_prefix<L: Leaf>(
107113
&self,
108114
key_context: &<L as HasStaticPrefix>::KeyContext,
109115
) -> DbKeyPrefix;
116+
117+
fn get_root_suffix(&self) -> Vec<u8>;
118+
119+
// Need when deserializing the root node from a raw DbValue.
120+
fn get_root_context(&self) -> Self::NodeContext;
110121
}

crates/starknet_patricia/src/patricia_merkle_tree/traversal_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use ethnum::U256;
22
use pretty_assertions::assert_eq;
33
use rstest::rstest;
4+
use starknet_api::hash::HashOutput;
45
use starknet_patricia_storage::db_object::HasStaticPrefix;
56
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
67

@@ -20,6 +21,7 @@ struct TestSubTree<'a> {
2021

2122
impl<'a> SubTreeTrait<'a> for TestSubTree<'a> {
2223
type NodeData = ();
24+
type NodeContext = ();
2325

2426
fn create(
2527
sorted_leaf_indices: SortedLeafIndices<'a>,
@@ -41,13 +43,23 @@ impl<'a> SubTreeTrait<'a> for TestSubTree<'a> {
4143
false
4244
}
4345

46+
fn unmodified_child_hash(_child_data: Self::NodeData) -> Option<HashOutput> {
47+
None
48+
}
49+
50+
fn get_root_context(&self) -> Self::NodeContext {}
51+
4452
fn get_root_prefix<L: Leaf>(
4553
&self,
4654
_key_context: &<L as HasStaticPrefix>::KeyContext,
4755
) -> DbKeyPrefix {
4856
// Dummy prefix for testing purposes (we only need a prefix when interacting with storage).
4957
DbKeyPrefix::new(&[0])
5058
}
59+
60+
fn get_root_suffix(&self) -> Vec<u8> {
61+
vec![]
62+
}
5163
}
5264

5365
/// case::single_right_child

0 commit comments

Comments
 (0)