Skip to content

Commit fec938d

Browse files
committed
starknet_committer,starknet_committer_and_os_cli,starknet_patricia: make FilledNode generic and introduce the FactsFilledNode alias
1 parent 2732847 commit fec938d

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
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,6 @@ 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;
2322
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
2423
BinaryData,
2524
EdgeData,
@@ -34,6 +33,7 @@ use starknet_types_core::felt::Felt;
3433
use strum::IntoEnumIterator;
3534

3635
use crate::block_committer::input::StarknetStorageValue;
36+
use crate::db::facts_db::types::FactDbFilledNode;
3737
use crate::forest::filled_forest::FilledForest;
3838
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
3939
use crate::patricia_merkle_tree::types::{
@@ -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_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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
4444
}
4545
}
4646

47-
impl<L: Leaf> FilledNode<L> {
47+
impl<L: Leaf> FilledNode<L, HashOutput> {
4848
pub fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
4949
self.hash.0.to_bytes_be()
5050
}
@@ -54,8 +54,7 @@ impl<L: Leaf> FilledNode<L> {
5454
}
5555
}
5656

57-
impl<L: Leaf> HasDynamicPrefix for FilledNode<L> {
58-
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
57+
impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
5958
type KeyContext = <L as HasStaticPrefix>::KeyContext;
6059
fn get_prefix(&self, _key_context: &Self::KeyContext) -> DbKeyPrefix {
6160
match &self.data {
@@ -73,7 +72,7 @@ pub struct FactNodeDeserializationContext {
7372
pub node_hash: HashOutput,
7473
}
7574

76-
impl<L: Leaf> DBObject for FilledNode<L> {
75+
impl<L: Leaf> DBObject for FilledNode<L, HashOutput> {
7776
type DeserializeContext = FactNodeDeserializationContext;
7877
/// This method serializes the filled node into a byte vector, where:
7978
/// - For binary nodes: Concatenates left and right hashes.

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashF
1717
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
1818
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;
1919

20+
type FactDbFilledNode<L> = FilledNode<L, HashOutput>;
21+
2022
#[cfg(test)]
2123
#[path = "tree_test.rs"]
2224
pub mod tree_test;
@@ -49,14 +51,14 @@ pub trait FilledTree<L: Leaf>: Sized + Send {
4951

5052
#[derive(Debug, Eq, PartialEq)]
5153
pub struct FilledTreeImpl<L: Leaf> {
52-
pub tree_map: HashMap<NodeIndex, FilledNode<L>>,
54+
pub tree_map: HashMap<NodeIndex, FactDbFilledNode<L>>,
5355
pub root_hash: HashOutput,
5456
}
5557

5658
impl<L: Leaf + 'static> FilledTreeImpl<L> {
5759
fn initialize_filled_tree_output_map_with_placeholders<'a>(
5860
updated_skeleton: &impl UpdatedSkeletonTree<'a>,
59-
) -> HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>> {
61+
) -> HashMap<NodeIndex, Mutex<Option<FactDbFilledNode<L>>>> {
6062
let mut filled_tree_output_map = HashMap::new();
6163
for (index, node) in updated_skeleton.get_nodes() {
6264
if !matches!(node, UpdatedSkeletonNode::UnmodifiedSubTree(_)) {
@@ -72,7 +74,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
7274
Arc::new(leaf_index_to_leaf_input.keys().map(|index| (*index, Mutex::new(None))).collect())
7375
}
7476

75-
pub(crate) fn get_all_nodes(&self) -> &HashMap<NodeIndex, FilledNode<L>> {
77+
pub(crate) fn get_all_nodes(&self) -> &HashMap<NodeIndex, FactDbFilledNode<L>> {
7678
&self.tree_map
7779
}
7880

@@ -179,7 +181,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
179181
index: NodeIndex,
180182
leaf_modifications: Option<Arc<LeafModifications<L>>>,
181183
leaf_index_to_leaf_input: Arc<HashMap<NodeIndex, Mutex<Option<L::Input>>>>,
182-
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>>>,
184+
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FactDbFilledNode<L>>>>>,
183185
leaf_index_to_leaf_output: Arc<HashMap<NodeIndex, Mutex<Option<L::Output>>>>,
184186
) -> FilledTreeResult<HashOutput>
185187
where
@@ -219,7 +221,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
219221
Self::write_to_output_map(
220222
filled_tree_output_map,
221223
index,
222-
FilledNode { hash, data },
224+
FactDbFilledNode { hash, data },
223225
)?;
224226
Ok(hash)
225227
}
@@ -242,7 +244,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
242244
Self::write_to_output_map(
243245
filled_tree_output_map,
244246
index,
245-
FilledNode { hash, data },
247+
FactDbFilledNode { hash, data },
246248
)?;
247249
Ok(hash)
248250
}
@@ -259,7 +261,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
259261
Self::write_to_output_map(
260262
filled_tree_output_map,
261263
index,
262-
FilledNode { hash, data },
264+
FactDbFilledNode { hash, data },
263265
)?;
264266
if let Some(output) = leaf_output {
265267
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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::{
1616
NodeData,
1717
PathToBottom,
1818
};
19-
use crate::patricia_merkle_tree::node_data::leaf::{LeafModifications, SkeletonLeaf};
19+
use crate::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications, SkeletonLeaf};
2020
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
2121
use crate::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
2222
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
@@ -26,6 +26,8 @@ use crate::patricia_merkle_tree::updated_skeleton_tree::tree::{
2626
UpdatedSkeletonTreeImpl,
2727
};
2828

29+
type FactDbFilledNode<L> = FilledNode<L, HashOutput>;
30+
2931
#[tokio::test(flavor = "multi_thread")]
3032
/// This test is a sanity test for computing the root hash of the patricia merkle tree with a single
3133
/// node that is a leaf with hash==1.
@@ -312,7 +314,7 @@ fn get_small_tree_updated_skeleton_and_leaf_modifications()
312314
}
313315

314316
fn get_small_tree_expected_filled_tree_map_and_root_hash()
315-
-> (HashMap<NodeIndex, FilledNode<MockLeaf>>, HashOutput) {
317+
-> (HashMap<NodeIndex, FactDbFilledNode<MockLeaf>>, HashOutput) {
316318
let expected_root_hash = HashOutput(Felt::from_hex("0x21").unwrap());
317319
let expected_filled_tree_map = HashMap::from([
318320
create_mock_binary_entry_for_testing(1, "0x21", "0xb", "0x16"),
@@ -366,10 +368,10 @@ fn create_mock_binary_entry_for_testing(
366368
hash: &str,
367369
left_hash: &str,
368370
right_hash: &str,
369-
) -> (NodeIndex, FilledNode<MockLeaf>) {
371+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
370372
(
371373
NodeIndex::from(index),
372-
FilledNode {
374+
FactDbFilledNode {
373375
hash: HashOutput(Felt::from_hex(hash).unwrap()),
374376
data: NodeData::Binary(BinaryData {
375377
left_data: HashOutput(Felt::from_hex(left_hash).unwrap()),
@@ -385,10 +387,10 @@ fn create_mock_edge_entry_for_testing(
385387
path: u128,
386388
length: u8,
387389
bottom_hash: &str,
388-
) -> (NodeIndex, FilledNode<MockLeaf>) {
390+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
389391
(
390392
NodeIndex::from(index),
391-
FilledNode {
393+
FactDbFilledNode {
392394
hash: HashOutput(Felt::from_hex(hash).unwrap()),
393395
data: NodeData::Edge(EdgeData {
394396
bottom_data: HashOutput(Felt::from_hex(bottom_hash).unwrap()),
@@ -405,10 +407,10 @@ fn create_mock_edge_entry_for_testing(
405407
fn create_mock_leaf_entry_for_testing(
406408
index: u128,
407409
hash: &str,
408-
) -> (NodeIndex, FilledNode<MockLeaf>) {
410+
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
409411
(
410412
NodeIndex::from(index),
411-
FilledNode {
413+
FactDbFilledNode {
412414
hash: HashOutput(Felt::from_hex(hash).unwrap()),
413415
data: NodeData::Leaf(MockLeaf(Felt::from_hex(hash).unwrap())),
414416
},

0 commit comments

Comments
 (0)