Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions crates/starknet_committer/src/block_committer/random_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet_patricia::patricia_merkle_tree::external_test_utils::{
get_random_u256,
u256_try_into_felt,
};
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
EdgeData,
Expand Down Expand Up @@ -178,7 +178,7 @@ impl RandomValue for NodeIndex {

macro_rules! random_filled_node {
($leaf:ty) => {
impl RandomValue for FilledNode<$leaf> {
impl RandomValue for FactDbFilledNode<$leaf> {
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
Self { data: NodeData::random(rng, max), hash: HashOutput::random(rng, max) }
}
Expand Down Expand Up @@ -216,11 +216,13 @@ macro_rules! random_filled_tree {
}
.as_usize();

let mut nodes: Vec<(NodeIndex, FilledNode<$leaf>)> = (0..max_node_number)
.map(|_| (NodeIndex::random(rng, max_size), FilledNode::random(rng, max_size)))
let mut nodes: Vec<(NodeIndex, FactDbFilledNode<$leaf>)> = (0..max_node_number)
.map(|_| {
(NodeIndex::random(rng, max_size), FactDbFilledNode::random(rng, max_size))
})
.collect();

nodes.push((NodeIndex::ROOT, FilledNode::random(rng, max_size)));
nodes.push((NodeIndex::ROOT, FactDbFilledNode::random(rng, max_size)));

Self {
tree_map: nodes.into_iter().collect(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn fetch_nodes<'a, L: Leaf>(
for (filled_root, subtree) in filled_roots.into_iter().zip(current_subtrees.iter()) {
match filled_root.data {
// Binary node.
NodeData::<L, HashOutput>::Binary(BinaryData { left_data, right_data }) => {
NodeData::Binary(BinaryData { left_data, right_data }) => {
if subtree.is_unmodified() {
skeleton_tree.nodes.insert(
subtree.root_index,
Expand All @@ -87,7 +87,7 @@ async fn fetch_nodes<'a, L: Leaf>(
)
}
// Edge node.
NodeData::<L, HashOutput>::Edge(EdgeData { bottom_data, path_to_bottom }) => {
NodeData::Edge(EdgeData { bottom_data, path_to_bottom }) => {
skeleton_tree
.nodes
.insert(subtree.root_index, OriginalSkeletonNode::Edge(path_to_bottom));
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_committer/src/db/facts_db/traversal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use starknet_api::hash::HashOutput;
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
use starknet_patricia::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FactNodeDeserializationContext;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
NodeData,
Expand All @@ -26,7 +26,7 @@ pub async fn calculate_subtrees_roots<'a, L: Leaf>(
subtrees: &[FactsSubTree<'a>],
storage: &mut impl Storage,
key_context: &<L as HasStaticPrefix>::KeyContext,
) -> TraversalResult<Vec<FilledNode<L>>> {
) -> TraversalResult<Vec<FactDbFilledNode<L>>> {
let mut subtrees_roots = vec![];
let db_keys: Vec<DbKey> = subtrees
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_committer::hash_function::hash::TreeHashFunctionImpl;
use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState;
use starknet_committer::patricia_merkle_tree::tree::OriginalSkeletonStorageTrieConfig;
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
EdgeData,
Expand Down Expand Up @@ -269,8 +269,8 @@ pub(crate) fn test_binary_serialize_test(binary_input: HashMap<String, u128>) ->
};

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

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

let binary_node: FilledNode<StarknetStorageValue> = FilledNode {
let binary_node: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
data: NodeData::Binary(BinaryData { left_data: hash, right_data: hash }),
hash,
};
let binary_node_key = binary_node.db_key(&EmptyKeyContext).0;

let edge_node: FilledNode<StarknetStorageValue> = FilledNode {
let edge_node: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
data: NodeData::Edge(EdgeData { bottom_data: hash, path_to_bottom: Default::default() }),
hash,
};

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

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

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

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

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

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

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

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

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

// Create a compiled class leaf node from the parsed data.
let compiled_class_leaf_rust = FilledNode {
let compiled_class_leaf_rust = FactDbFilledNode {
data: NodeData::Leaf(CompiledClassHash(Felt::from(*get_or_key_not_found(
&compiled_class_leaf_data,
"compiled_class_hash",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ use crate::patricia_merkle_tree::node_data::leaf::Leaf;

#[derive(Clone, Debug, PartialEq, Eq)]
/// A node in a Patricia-Merkle tree, complete with its hash and data.
pub struct FilledNode<L: Leaf> {
pub struct FilledNode<L: Leaf, ChildData> {
pub hash: HashOutput,
pub data: NodeData<L, HashOutput>,
pub data: NodeData<L, ChildData>,
}

// TODO(Ariel, 14/12/2025): move this type (along with DBObject impl) to the facts_db module in
// starknet_committer. This can happen after serialization of FilledTree is made generic in the
// layout.
pub type FactDbFilledNode<L> = FilledNode<L, HashOutput>;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::storage_trait::{DbKey, DbKeyPrefix, DbValue};
use starknet_types_core::felt::Felt;

use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
EdgeData,
Expand Down Expand Up @@ -44,7 +44,9 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
}
}

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

impl<L: Leaf> HasDynamicPrefix for FilledNode<L> {
impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
type KeyContext = <L as HasStaticPrefix>::KeyContext;

Expand All @@ -74,7 +76,7 @@ pub struct FactNodeDeserializationContext {
pub node_hash: HashOutput,
}

impl<L: Leaf> DBObject for FilledNode<L> {
impl<L: Leaf> DBObject for FactDbFilledNode<L> {
type DeserializeContext = FactNodeDeserializationContext;
/// This method serializes the filled node into a byte vector, where:
/// - For binary nodes: Concatenates left and right hashes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
use starknet_patricia_storage::storage_trait::DbHashMap;

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

#[derive(Debug, Eq, PartialEq)]
pub struct FilledTreeImpl<L: Leaf> {
pub tree_map: HashMap<NodeIndex, FilledNode<L>>,
pub tree_map: HashMap<NodeIndex, FactDbFilledNode<L>>,
pub root_hash: HashOutput,
}

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

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

Expand Down Expand Up @@ -179,7 +179,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
index: NodeIndex,
leaf_modifications: Option<Arc<LeafModifications<L>>>,
leaf_index_to_leaf_input: Arc<HashMap<NodeIndex, Mutex<Option<L::Input>>>>,
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>>>,
filled_tree_output_map: Arc<HashMap<NodeIndex, Mutex<Option<FactDbFilledNode<L>>>>>,
leaf_index_to_leaf_output: Arc<HashMap<NodeIndex, Mutex<Option<L::Output>>>>,
) -> FilledTreeResult<HashOutput>
where
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
Self::write_to_output_map(
filled_tree_output_map,
index,
FilledNode { hash, data },
FactDbFilledNode { hash, data },
)?;
Ok(hash)
}
Expand All @@ -242,7 +242,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
Self::write_to_output_map(
filled_tree_output_map,
index,
FilledNode { hash, data },
FactDbFilledNode { hash, data },
)?;
Ok(hash)
}
Expand All @@ -259,7 +259,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
Self::write_to_output_map(
filled_tree_output_map,
index,
FilledNode { hash, data },
FactDbFilledNode { hash, data },
)?;
if let Some(output) = leaf_output {
Self::write_to_output_map(leaf_index_to_leaf_output, index, output)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet_types_core::felt::Felt;

use crate::patricia_merkle_tree::external_test_utils::MockLeaf;
use crate::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
use crate::patricia_merkle_tree::filled_tree::tree::{FilledTree, FilledTreeImpl};
use crate::patricia_merkle_tree::internal_test_utils::TestTreeHashFunction;
use crate::patricia_merkle_tree::node_data::errors::LeafError;
Expand Down Expand Up @@ -312,7 +312,7 @@ fn get_small_tree_updated_skeleton_and_leaf_modifications()
}

fn get_small_tree_expected_filled_tree_map_and_root_hash()
-> (HashMap<NodeIndex, FilledNode<MockLeaf>>, HashOutput) {
-> (HashMap<NodeIndex, FactDbFilledNode<MockLeaf>>, HashOutput) {
let expected_root_hash = HashOutput(Felt::from_hex("0x21").unwrap());
let expected_filled_tree_map = HashMap::from([
create_mock_binary_entry_for_testing(1, "0x21", "0xb", "0x16"),
Expand Down Expand Up @@ -366,10 +366,10 @@ fn create_mock_binary_entry_for_testing(
hash: &str,
left_hash: &str,
right_hash: &str,
) -> (NodeIndex, FilledNode<MockLeaf>) {
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
(
NodeIndex::from(index),
FilledNode {
FactDbFilledNode {
hash: HashOutput(Felt::from_hex(hash).unwrap()),
data: NodeData::Binary(BinaryData {
left_data: HashOutput(Felt::from_hex(left_hash).unwrap()),
Expand All @@ -385,10 +385,10 @@ fn create_mock_edge_entry_for_testing(
path: u128,
length: u8,
bottom_hash: &str,
) -> (NodeIndex, FilledNode<MockLeaf>) {
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
(
NodeIndex::from(index),
FilledNode {
FactDbFilledNode {
hash: HashOutput(Felt::from_hex(hash).unwrap()),
data: NodeData::Edge(EdgeData {
bottom_data: HashOutput(Felt::from_hex(bottom_hash).unwrap()),
Expand All @@ -405,10 +405,10 @@ fn create_mock_edge_entry_for_testing(
fn create_mock_leaf_entry_for_testing(
index: u128,
hash: &str,
) -> (NodeIndex, FilledNode<MockLeaf>) {
) -> (NodeIndex, FactDbFilledNode<MockLeaf>) {
(
NodeIndex::from(index),
FilledNode {
FactDbFilledNode {
hash: HashOutput(Felt::from_hex(hash).unwrap()),
data: NodeData::Leaf(MockLeaf(Felt::from_hex(hash).unwrap())),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl PathToBottom {
}
}

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