Skip to content

Commit a33b741

Browse files
committed
starknet_committer,starknet_committer_and_os_cli,starknet_patricia: make FilledNode generic
1 parent 0eb7c84 commit a33b741

File tree

9 files changed

+55
-45
lines changed

9 files changed

+55
-45
lines changed

crates/starknet_committer/src/block_committer/random_structs.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use starknet_patricia::patricia_merkle_tree::external_test_utils::{
1919
get_random_u256,
2020
u256_try_into_felt,
2121
};
22-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
22+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
2323
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
2424
BinaryData,
2525
EdgeData,
@@ -178,7 +178,7 @@ impl RandomValue for NodeIndex {
178178

179179
macro_rules! random_filled_node {
180180
($leaf:ty) => {
181-
impl RandomValue for FilledNode<$leaf> {
181+
impl RandomValue for FactDbFilledNode<$leaf> {
182182
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
183183
Self { data: NodeData::random(rng, max), hash: HashOutput::random(rng, max) }
184184
}
@@ -216,11 +216,13 @@ macro_rules! random_filled_tree {
216216
}
217217
.as_usize();
218218

219-
let mut nodes: Vec<(NodeIndex, FilledNode<$leaf>)> = (0..max_node_number)
220-
.map(|_| (NodeIndex::random(rng, max_size), FilledNode::random(rng, max_size)))
219+
let mut nodes: Vec<(NodeIndex, FactDbFilledNode<$leaf>)> = (0..max_node_number)
220+
.map(|_| {
221+
(NodeIndex::random(rng, max_size), FactDbFilledNode::random(rng, max_size))
222+
})
221223
.collect();
222224

223-
nodes.push((NodeIndex::ROOT, FilledNode::random(rng, max_size)));
225+
nodes.push((NodeIndex::ROOT, FactDbFilledNode::random(rng, max_size)));
224226

225227
Self {
226228
tree_map: nodes.into_iter().collect(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async fn fetch_nodes<'a, L: Leaf>(
6161
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) {
6262
match filled_root.data {
6363
// Binary node.
64-
NodeData::<L, HashOutput>::Binary(BinaryData { left_data, right_data }) => {
64+
NodeData::Binary(BinaryData { left_data, right_data }) => {
6565
if subtree.is_unmodified() {
6666
skeleton_tree.nodes.insert(
6767
subtree.root_index,
@@ -87,7 +87,7 @@ async fn fetch_nodes<'a, L: Leaf>(
8787
)
8888
}
8989
// Edge node.
90-
NodeData::<L, HashOutput>::Edge(EdgeData { bottom_data, path_to_bottom }) => {
90+
NodeData::Edge(EdgeData { bottom_data, path_to_bottom }) => {
9191
skeleton_tree
9292
.nodes
9393
.insert(subtree.root_index, OriginalSkeletonNode::Edge(path_to_bottom));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use starknet_api::hash::HashOutput;
4-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
4+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
55
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FactNodeDeserializationContext;
66
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
77
NodeData,
@@ -26,7 +26,7 @@ pub async fn calculate_subtrees_roots<'a, L: Leaf>(
2626
subtrees: &[FactsSubTree<'a>],
2727
storage: &mut impl Storage,
2828
key_context: &<L as HasStaticPrefix>::KeyContext,
29-
) -> TraversalResult<Vec<FilledNode<L>>> {
29+
) -> TraversalResult<Vec<FactDbFilledNode<L>>> {
3030
let mut subtrees_roots = vec![];
3131
let db_keys: Vec<DbKey> = subtrees
3232
.iter()

crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use starknet_committer::hash_function::hash::TreeHashFunctionImpl;
1717
use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1818
use starknet_committer::patricia_merkle_tree::tree::OriginalSkeletonStorageTrieConfig;
1919
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
20-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
20+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
2121
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
2222
BinaryData,
2323
EdgeData,
@@ -269,8 +269,8 @@ pub(crate) fn test_binary_serialize_test(binary_input: HashMap<String, u128>) ->
269269
};
270270

271271
// Create a filled node (irrelevant leaf type) with binary data and zero hash.
272-
let filled_node: FilledNode<StarknetStorageValue> =
273-
FilledNode { data: NodeData::Binary(binary_data), hash: HashOutput(Felt::ZERO) };
272+
let filled_node: FactDbFilledNode<StarknetStorageValue> =
273+
FactDbFilledNode { data: NodeData::Binary(binary_data), hash: HashOutput(Felt::ZERO) };
274274

275275
// Serialize the binary node and insert it into the map under the key "value".
276276
let value = filled_node.serialize();
@@ -417,23 +417,23 @@ pub(crate) fn test_node_db_key() -> String {
417417
// Generate keys for different node types.
418418
let hash = HashOutput(zero);
419419

420-
let binary_node: FilledNode<StarknetStorageValue> = FilledNode {
420+
let binary_node: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
421421
data: NodeData::Binary(BinaryData { left_data: hash, right_data: hash }),
422422
hash,
423423
};
424424
let binary_node_key = binary_node.db_key(&EmptyKeyContext).0;
425425

426-
let edge_node: FilledNode<StarknetStorageValue> = FilledNode {
426+
let edge_node: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
427427
data: NodeData::Edge(EdgeData { bottom_data: hash, path_to_bottom: Default::default() }),
428428
hash,
429429
};
430430

431431
let edge_node_key = edge_node.db_key(&EmptyKeyContext).0;
432432

433-
let storage_leaf = FilledNode { data: NodeData::Leaf(StarknetStorageValue(zero)), hash };
433+
let storage_leaf = FactDbFilledNode { data: NodeData::Leaf(StarknetStorageValue(zero)), hash };
434434
let storage_leaf_key = storage_leaf.db_key(&EmptyKeyContext).0;
435435

436-
let state_tree_leaf = FilledNode {
436+
let state_tree_leaf = FactDbFilledNode {
437437
data: NodeData::Leaf(ContractState {
438438
class_hash: ClassHash(zero),
439439
storage_root_hash: HashOutput(zero),
@@ -443,7 +443,8 @@ pub(crate) fn test_node_db_key() -> String {
443443
};
444444
let state_tree_leaf_key = state_tree_leaf.db_key(&EmptyKeyContext).0;
445445

446-
let compiled_class_leaf = FilledNode { data: NodeData::Leaf(CompiledClassHash(zero)), hash };
446+
let compiled_class_leaf =
447+
FactDbFilledNode { data: NodeData::Leaf(CompiledClassHash(zero)), hash };
447448
let compiled_class_leaf_key = compiled_class_leaf.db_key(&EmptyKeyContext).0;
448449

449450
// Store keys in a HashMap.
@@ -509,7 +510,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
509510
let binary_data: HashMap<String, u128> = serde_json::from_str(binary_json)?;
510511

511512
// Create a binary node from the parsed data.
512-
let binary_rust: FilledNode<StarknetStorageValue> = FilledNode {
513+
let binary_rust: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
513514
data: NodeData::Binary(BinaryData {
514515
left_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "left")?)),
515516
right_data: HashOutput(Felt::from(*get_or_key_not_found(&binary_data, "right")?)),
@@ -525,7 +526,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
525526
let edge_data: HashMap<String, u128> = serde_json::from_str(edge_json)?;
526527

527528
// Create an edge node from the parsed data.
528-
let edge_rust: FilledNode<StarknetStorageValue> = FilledNode {
529+
let edge_rust: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
529530
data: NodeData::Edge(EdgeData {
530531
bottom_data: HashOutput(Felt::from(*get_or_key_not_found(&edge_data, "bottom")?)),
531532
path_to_bottom: PathToBottom::new(
@@ -552,7 +553,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
552553
let storage_leaf_data: HashMap<String, u128> = serde_json::from_str(storage_leaf_json)?;
553554

554555
// Create a storage leaf node from the parsed data.
555-
let storage_leaf_rust = FilledNode {
556+
let storage_leaf_rust = FactDbFilledNode {
556557
data: NodeData::Leaf(StarknetStorageValue(Felt::from(*get_or_key_not_found(
557558
&storage_leaf_data,
558559
"value",
@@ -571,7 +572,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
571572
serde_json::from_str(contract_state_leaf)?;
572573

573574
// Create a contract state leaf node from the parsed data.
574-
let contract_state_leaf_rust = FilledNode {
575+
let contract_state_leaf_rust = FactDbFilledNode {
575576
data: NodeData::Leaf(ContractState {
576577
class_hash: ClassHash(Felt::from(*get_or_key_not_found(
577578
&contract_state_leaf_data,
@@ -601,7 +602,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
601602
serde_json::from_str(compiled_class_leaf)?;
602603

603604
// Create a compiled class leaf node from the parsed data.
604-
let compiled_class_leaf_rust = FilledNode {
605+
let compiled_class_leaf_rust = FactDbFilledNode {
605606
data: NodeData::Leaf(CompiledClassHash(Felt::from(*get_or_key_not_found(
606607
&compiled_class_leaf_data,
607608
"compiled_class_hash",

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ use crate::patricia_merkle_tree::node_data::leaf::Leaf;
55

66
#[derive(Clone, Debug, PartialEq, Eq)]
77
/// A node in a Patricia-Merkle tree, complete with its hash and data.
8-
pub struct FilledNode<L: Leaf> {
8+
pub struct FilledNode<L: Leaf, ChildData> {
99
pub hash: HashOutput,
10-
pub data: NodeData<L, HashOutput>,
10+
pub data: NodeData<L, ChildData>,
1111
}
12+
13+
// TODO(Ariel, 14/12/2025): move this type (along with DBObject impl) to the facts_db module in
14+
// starknet_committer. This can happen after serialization of FilledTree is made generic in the
15+
// layout.
16+
pub type FactDbFilledNode<L> = FilledNode<L, HashOutput>;

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use starknet_patricia_storage::errors::DeserializationError;
1010
use starknet_patricia_storage::storage_trait::{DbKey, DbKeyPrefix, DbValue};
1111
use starknet_types_core::felt::Felt;
1212

13-
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
13+
use crate::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
1414
use crate::patricia_merkle_tree::node_data::inner_node::{
1515
BinaryData,
1616
EdgeData,
@@ -44,7 +44,9 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
4444
}
4545
}
4646

47-
impl<L: Leaf> FilledNode<L> {
47+
// TODO(Ariel, 14/12/2025): generalize this to both layouts (e.g. via a new trait). ATM db_key is
48+
// only used in the filled tree serialize function, which assumes facts layout.
49+
impl<L: Leaf> FactDbFilledNode<L> {
4850
pub fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
4951
self.hash.0.to_bytes_be()
5052
}
@@ -54,7 +56,7 @@ impl<L: Leaf> FilledNode<L> {
5456
}
5557
}
5658

57-
impl<L: Leaf> HasDynamicPrefix for FilledNode<L> {
59+
impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
5860
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
5961
type KeyContext = <L as HasStaticPrefix>::KeyContext;
6062

@@ -74,7 +76,7 @@ pub struct FactNodeDeserializationContext {
7476
pub node_hash: HashOutput,
7577
}
7678

77-
impl<L: Leaf> DBObject for FilledNode<L> {
79+
impl<L: Leaf> DBObject for FactDbFilledNode<L> {
7880
type DeserializeContext = FactNodeDeserializationContext;
7981
/// This method serializes the filled node into a byte vector, where:
8082
/// - For binary nodes: Concatenates left and right hashes.

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
99
use starknet_patricia_storage::storage_trait::DbHashMap;
1010

1111
use crate::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
12-
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
12+
use crate::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
1313
use crate::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData, NodeData};
1414
use crate::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
1515
use crate::patricia_merkle_tree::types::NodeIndex;
@@ -49,14 +49,14 @@ pub trait FilledTree<L: Leaf>: Sized + Send {
4949

5050
#[derive(Debug, Eq, PartialEq)]
5151
pub struct FilledTreeImpl<L: Leaf> {
52-
pub tree_map: HashMap<NodeIndex, FilledNode<L>>,
52+
pub tree_map: HashMap<NodeIndex, FactDbFilledNode<L>>,
5353
pub root_hash: HashOutput,
5454
}
5555

5656
impl<L: Leaf + 'static> FilledTreeImpl<L> {
5757
fn initialize_filled_tree_output_map_with_placeholders<'a>(
5858
updated_skeleton: &impl UpdatedSkeletonTree<'a>,
59-
) -> HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>> {
59+
) -> HashMap<NodeIndex, Mutex<Option<FactDbFilledNode<L>>>> {
6060
let mut filled_tree_output_map = HashMap::new();
6161
for (index, node) in updated_skeleton.get_nodes() {
6262
if !matches!(node, UpdatedSkeletonNode::UnmodifiedSubTree(_)) {
@@ -72,7 +72,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
7272
Arc::new(leaf_index_to_leaf_input.keys().map(|index| (*index, Mutex::new(None))).collect())
7373
}
7474

75-
pub(crate) fn get_all_nodes(&self) -> &HashMap<NodeIndex, FilledNode<L>> {
75+
pub(crate) fn get_all_nodes(&self) -> &HashMap<NodeIndex, FactDbFilledNode<L>> {
7676
&self.tree_map
7777
}
7878

@@ -179,7 +179,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
179179
index: NodeIndex,
180180
leaf_modifications: Option<Arc<LeafModifications<L>>>,
181181
leaf_index_to_leaf_input: Arc<HashMap<NodeIndex, Mutex<Option<L::Input>>>>,
182-
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>>>,
182+
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FactDbFilledNode<L>>>>>,
183183
leaf_index_to_leaf_output: Arc<HashMap<NodeIndex, Mutex<Option<L::Output>>>>,
184184
) -> FilledTreeResult<HashOutput>
185185
where
@@ -219,7 +219,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
219219
Self::write_to_output_map(
220220
filled_tree_output_map,
221221
index,
222-
FilledNode { hash, data },
222+
FactDbFilledNode { hash, data },
223223
)?;
224224
Ok(hash)
225225
}
@@ -242,7 +242,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
242242
Self::write_to_output_map(
243243
filled_tree_output_map,
244244
index,
245-
FilledNode { hash, data },
245+
FactDbFilledNode { hash, data },
246246
)?;
247247
Ok(hash)
248248
}
@@ -259,7 +259,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
259259
Self::write_to_output_map(
260260
filled_tree_output_map,
261261
index,
262-
FilledNode { hash, data },
262+
FactDbFilledNode { hash, data },
263263
)?;
264264
if let Some(output) = leaf_output {
265265
Self::write_to_output_map(leaf_index_to_leaf_output, index, output)?

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/tree_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use starknet_types_core::felt::Felt;
55

66
use crate::patricia_merkle_tree::external_test_utils::MockLeaf;
77
use crate::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
8-
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
8+
use crate::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
99
use crate::patricia_merkle_tree::filled_tree::tree::{FilledTree, FilledTreeImpl};
1010
use crate::patricia_merkle_tree::internal_test_utils::TestTreeHashFunction;
1111
use crate::patricia_merkle_tree::node_data::errors::LeafError;
@@ -312,7 +312,7 @@ fn get_small_tree_updated_skeleton_and_leaf_modifications()
312312
}
313313

314314
fn get_small_tree_expected_filled_tree_map_and_root_hash()
315-
-> (HashMap<NodeIndex, FilledNode<MockLeaf>>, HashOutput) {
315+
-> (HashMap<NodeIndex, FactDbFilledNode<MockLeaf>>, HashOutput) {
316316
let expected_root_hash = HashOutput(Felt::from_hex("0x21").unwrap());
317317
let expected_filled_tree_map = HashMap::from([
318318
create_mock_binary_entry_for_testing(1, "0x21", "0xb", "0x16"),
@@ -366,10 +366,10 @@ fn create_mock_binary_entry_for_testing(
366366
hash: &str,
367367
left_hash: &str,
368368
right_hash: &str,
369-
) -> (NodeIndex, FilledNode<MockLeaf>) {
369+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
370370
(
371371
NodeIndex::from(index),
372-
FilledNode {
372+
FactDbFilledNode {
373373
hash: HashOutput(Felt::from_hex(hash).unwrap()),
374374
data: NodeData::Binary(BinaryData {
375375
left_data: HashOutput(Felt::from_hex(left_hash).unwrap()),
@@ -385,10 +385,10 @@ fn create_mock_edge_entry_for_testing(
385385
path: u128,
386386
length: u8,
387387
bottom_hash: &str,
388-
) -> (NodeIndex, FilledNode<MockLeaf>) {
388+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
389389
(
390390
NodeIndex::from(index),
391-
FilledNode {
391+
FactDbFilledNode {
392392
hash: HashOutput(Felt::from_hex(hash).unwrap()),
393393
data: NodeData::Edge(EdgeData {
394394
bottom_data: HashOutput(Felt::from_hex(bottom_hash).unwrap()),
@@ -405,10 +405,10 @@ fn create_mock_edge_entry_for_testing(
405405
fn create_mock_leaf_entry_for_testing(
406406
index: u128,
407407
hash: &str,
408-
) -> (NodeIndex, FilledNode<MockLeaf>) {
408+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
409409
(
410410
NodeIndex::from(index),
411-
FilledNode {
411+
FactDbFilledNode {
412412
hash: HashOutput(Felt::from_hex(hash).unwrap()),
413413
data: NodeData::Leaf(MockLeaf(Felt::from_hex(hash).unwrap())),
414414
},

crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl PathToBottom {
200200
}
201201
}
202202

203-
// TODO(Ariel 10/12/2025): Move Preimage to the fact_db module in starknet_committer (add a flatten
203+
// TODO(Ariel): Move Preimage to the fact_db module in starknet_committer (add a flatten
204204
// trait to be implemented in starknet_committer for BinaryData and EdgeData).
205205
#[derive(Clone, Debug, PartialEq)]
206206
pub enum Preimage {

0 commit comments

Comments
 (0)