Skip to content

Commit

Permalink
refactor: change leaf data name to leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Jul 14, 2024
1 parent a8ac86e commit c1c5565
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::storage::map_storage::MapStorage;
use rand::Rng;

use super::filled_tree::tree::{FilledTree, StorageTrie};
use super::node_data::leaf::{LeafData, LeafModifications, SkeletonLeaf};
use super::node_data::leaf::{Leaf, LeafModifications, SkeletonLeaf};
use super::original_skeleton_tree::config::OriginalSkeletonStorageTrieConfig;
use super::original_skeleton_tree::tree::{OriginalSkeletonTree, OriginalSkeletonTreeImpl};
use super::types::{NodeIndex, SortedLeafIndices};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::errors::LeafError;
use crate::patricia_merkle_tree::node_data::leaf::ContractState;
use crate::patricia_merkle_tree::updated_skeleton_tree::errors::UpdatedSkeletonTreeError;
use crate::patricia_merkle_tree::{node_data::leaf::LeafData, types::NodeIndex};
use crate::patricia_merkle_tree::{node_data::leaf::Leaf, types::NodeIndex};

#[derive(thiserror::Error, Debug)]
pub enum FilledTreeError<L: LeafData> {
pub enum FilledTreeError<L: Leaf> {
#[error("Deleted leaf at index {0:?} appears in the updated skeleton tree.")]
DeletedLeafInSkeleton(NodeIndex),
#[error("Double update at node {index:?}. Existing value: {existing_value:?}.")]
Expand Down
4 changes: 2 additions & 2 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::impl_from_hex_for_felt_wrapper;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;

// TODO(Nimrod, 1/6/2024): Use the ClassHash defined in starknet-types-core when available.

Expand All @@ -24,7 +24,7 @@ impl_from_hex_for_felt_wrapper!(CompiledClassHash);

#[derive(Clone, Debug, PartialEq, Eq)]
/// A node in a Patricia-Merkle tree which was modified during an update.
pub struct FilledNode<L: LeafData> {
pub struct FilledNode<L: Leaf> {
pub hash: HashOutput,
pub data: NodeData<L>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, EdgePathLength, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::storage::db_object::DBObject;
use crate::storage::errors::DeserializationError;
use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue};
Expand All @@ -27,7 +27,7 @@ pub(crate) struct LeafCompiledClassToSerialize {
pub(crate) compiled_class_hash: Felt,
}

impl<L: LeafData> FilledNode<L> {
impl<L: Leaf> FilledNode<L> {
pub fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
self.hash.0.to_bytes_be()
}
Expand All @@ -37,7 +37,7 @@ impl<L: LeafData> FilledNode<L> {
}
}

impl<L: LeafData> DBObject for FilledNode<L> {
impl<L: Leaf> DBObject for FilledNode<L> {
/// This method serializes the filled node into a byte vector, where:
/// - For binary nodes: Concatenates left and right hashes.
/// - For edge nodes: Concatenates bottom hash, path, and path length.
Expand Down Expand Up @@ -84,7 +84,7 @@ impl<L: LeafData> DBObject for FilledNode<L> {
}
}

impl<L: LeafData> FilledNode<L> {
impl<L: Leaf> FilledNode<L> {
/// Deserializes filled nodes.
pub(crate) fn deserialize(
node_hash: HashOutput,
Expand Down
10 changes: 5 additions & 5 deletions crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::BinaryData;
use crate::patricia_merkle_tree::node_data::inner_node::EdgeData;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::leaf::ContractState;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::node_data::leaf::LeafModifications;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
Expand All @@ -32,7 +32,7 @@ pub(crate) type FilledTreeResult<T, L> = Result<T, FilledTreeError<L>>;
/// Consider a Patricia-Merkle Tree which has been updated with new leaves.
/// FilledTree consists of all nodes which were modified in the update, including their updated
/// data and hashes.
pub(crate) trait FilledTree<L: LeafData>: Sized {
pub(crate) trait FilledTree<L: Leaf>: Sized {
/// Computes and returns the filled tree.
async fn create<TH: TreeHashFunction<L> + 'static>(
updated_skeleton: impl UpdatedSkeletonTree + 'static,
Expand All @@ -48,7 +48,7 @@ pub(crate) trait FilledTree<L: LeafData>: Sized {
}

#[derive(Debug, Eq, PartialEq)]
pub struct FilledTreeImpl<L: LeafData> {
pub struct FilledTreeImpl<L: Leaf> {
pub tree_map: HashMap<NodeIndex, FilledNode<L>>,
pub root_hash: HashOutput,
}
Expand All @@ -58,7 +58,7 @@ pub type ClassesTrie = FilledTreeImpl<CompiledClassHash>;
pub type ContractsTrie = FilledTreeImpl<ContractState>;
pub type StorageTrieMap = HashMap<ContractAddress, StorageTrie>;

impl<L: LeafData + 'static> FilledTreeImpl<L> {
impl<L: Leaf + 'static> FilledTreeImpl<L> {
fn initialize_with_placeholders(
updated_skeleton: &impl UpdatedSkeletonTree,
) -> HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>> {
Expand Down Expand Up @@ -213,7 +213,7 @@ impl<L: LeafData + 'static> FilledTreeImpl<L> {
}
}

impl<L: LeafData + 'static> FilledTree<L> for FilledTreeImpl<L> {
impl<L: Leaf + 'static> FilledTree<L> for FilledTreeImpl<L> {
async fn create<TH: TreeHashFunction<L> + 'static>(
updated_skeleton: impl UpdatedSkeletonTree + 'static,
leaf_modifications: Arc<LeafModifications<L>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::errors::{EdgePathError, PathToBottomError};
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::types::{NodeIndex, SubTreeHeight};

use ethnum::U256;
Expand All @@ -15,7 +15,7 @@ pub mod inner_node_test;
#[cfg_attr(any(test, feature = "testing"), derive(EnumDiscriminants))]
#[cfg_attr(any(test, feature = "testing"), strum_discriminants(derive(EnumIter)))]
// A Patricia-Merkle tree node's data, i.e., the pre-image of its hash.
pub enum NodeData<L: LeafData> {
pub enum NodeData<L: Leaf> {
Binary(BinaryData),
Edge(EdgeData),
Leaf(L),
Expand Down
8 changes: 4 additions & 4 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::patricia_merkle_tree::node_data::errors::{LeafError, LeafResult};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::storage::db_object::{DBObject, Deserializable};

pub trait LeafData: Clone + Sync + Send + DBObject + Deserializable + Default + Debug + Eq {
pub trait Leaf: Clone + Sync + Send + DBObject + Deserializable + Default + Debug + Eq {
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;

Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct ContractState {
pub class_hash: ClassHash,
}

impl LeafData for StarknetStorageValue {
impl Leaf for StarknetStorageValue {
fn is_empty(&self) -> bool {
self.0 == Felt::ZERO
}
Expand All @@ -60,7 +60,7 @@ impl LeafData for StarknetStorageValue {
}
}

impl LeafData for CompiledClassHash {
impl Leaf for CompiledClassHash {
fn is_empty(&self) -> bool {
self.0 == Felt::ZERO
}
Expand All @@ -73,7 +73,7 @@ impl LeafData for CompiledClassHash {
}
}

impl LeafData for ContractState {
impl Leaf for ContractState {
fn is_empty(&self) -> bool {
self.nonce.0 == Felt::ZERO
&& self.class_hash.0 == Felt::ZERO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, Nonce};
use crate::patricia_merkle_tree::node_data::leaf::ContractState;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;

use rstest::rstest;
use std::fmt::Debug;
Expand All @@ -26,7 +26,7 @@ use std::fmt::Debug;
nonce: Nonce(Felt::from(23479515749555_u128)), storage_root_hash: HashOutput(Felt::from(2359743529034_u128)), class_hash: ClassHash(Felt::from(1349866415897798_u128))
})
]
fn test_leaf_serde<L: LeafData + Eq + Debug>(#[case] leaf: L) {
fn test_leaf_serde<L: Leaf + Eq + Debug>(#[case] leaf: L) {
let serialized = leaf.serialize();
let deserialized = L::deserialize(&serialized).unwrap();
assert_eq!(deserialized, leaf);
Expand All @@ -36,6 +36,6 @@ fn test_leaf_serde<L: LeafData + Eq + Debug>(#[case] leaf: L) {
#[case::storage_leaf(StarknetStorageValue::default())]
#[case::compiled_class_leaf(CompiledClassHash::default())]
#[case::contract_state_leaf(ContractState::default())]
fn test_default_is_empty<L: LeafData>(#[case] leaf: L) {
fn test_default_is_empty<L: Leaf>(#[case] leaf: L) {
assert!(leaf.is_empty())
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, LeafData, LeafModifications};
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, Leaf, LeafModifications};
use crate::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::NodeIndex;

/// Configures the creation of an original skeleton tree.
pub(crate) trait OriginalSkeletonTreeConfig<L: LeafData> {
pub(crate) trait OriginalSkeletonTreeConfig<L: Leaf> {
/// Configures whether modified leaves should be compared to the previous leaves and log out a
/// warning when encountering a trivial modification.
fn compare_modified_leaves(&self) -> bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::BinaryData;
use crate::patricia_merkle_tree::node_data::inner_node::EdgeData;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
Expand Down Expand Up @@ -119,7 +119,7 @@ impl OriginalSkeletonTreeImpl {
/// Given a list of subtrees, traverses towards their leaves and fetches all non-empty,
/// unmodified nodes. If `compare_modified_leaves` is set, function logs out a warning when
/// encountering a trivial modification. Fills the previous leaf values if it is not none.
fn fetch_nodes<L: LeafData>(
fn fetch_nodes<L: Leaf>(
&mut self,
subtrees: Vec<SubTree<'_>>,
storage: &impl Storage,
Expand Down Expand Up @@ -213,7 +213,7 @@ impl OriginalSkeletonTreeImpl {
self.fetch_nodes::<L>(next_subtrees, storage, config, previous_leaves)
}

fn calculate_subtrees_roots<L: LeafData>(
fn calculate_subtrees_roots<L: Leaf>(
subtrees: &[SubTree<'_>],
storage: &impl Storage,
) -> OriginalSkeletonTreeResult<Vec<FilledNode<L>>> {
Expand Down Expand Up @@ -246,7 +246,7 @@ impl OriginalSkeletonTreeImpl {
Ok(subtrees_roots)
}

pub(crate) fn create_impl<L: LeafData>(
pub(crate) fn create_impl<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand Down Expand Up @@ -274,7 +274,7 @@ impl OriginalSkeletonTreeImpl {
Ok(skeleton_tree)
}

pub(crate) fn create_and_get_previous_leaves_impl<L: LeafData>(
pub(crate) fn create_and_get_previous_leaves_impl<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand Down Expand Up @@ -344,7 +344,7 @@ impl OriginalSkeletonTreeImpl {
/// Given leaf indices that were previously empty leaves, logs out a warning for trivial
/// modification if a leaf is modified to an empty leaf.
/// If this check is suppressed by configuration, does nothing.
fn log_warning_for_empty_leaves<L: LeafData, T: Borrow<NodeIndex> + Debug>(
fn log_warning_for_empty_leaves<L: Leaf, T: Borrow<NodeIndex> + Debug>(
leaf_indices: &[T],
config: &impl OriginalSkeletonTreeConfig<L>,
) -> OriginalSkeletonTreeResult<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
use crate::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonNode;
Expand All @@ -16,7 +16,7 @@ pub(crate) type OriginalSkeletonTreeResult<T> = Result<T, OriginalSkeletonTreeEr
/// update. It also contains the hashes (for edge siblings - also the edge data) of the unmodified
/// nodes on the Merkle paths from the updated leaves to the root.
pub(crate) trait OriginalSkeletonTree: Sized {
fn create<L: LeafData>(
fn create<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand All @@ -27,7 +27,7 @@ pub(crate) trait OriginalSkeletonTree: Sized {

fn get_nodes_mut(&mut self) -> &mut OriginalSkeletonNodeMap;

fn create_and_get_previous_leaves<L: LeafData>(
fn create_and_get_previous_leaves<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand All @@ -42,7 +42,7 @@ pub(crate) struct OriginalSkeletonTreeImpl {
}

impl OriginalSkeletonTree for OriginalSkeletonTreeImpl {
fn create<L: LeafData>(
fn create<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand All @@ -59,7 +59,7 @@ impl OriginalSkeletonTree for OriginalSkeletonTreeImpl {
&mut self.nodes
}

fn create_and_get_previous_leaves<L: LeafData>(
fn create_and_get_previous_leaves<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
sorted_leaf_indices: SortedLeafIndices<'_>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, LeafData};
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, Leaf};

#[cfg(test)]
#[path = "hash_function_test.rs"]
Expand Down Expand Up @@ -35,7 +35,7 @@ impl HashFunction for PoseidonHashFunction {
}
}

pub(crate) trait TreeHashFunction<L: LeafData> {
pub(crate) trait TreeHashFunction<L: Leaf> {
/// Computes the hash of the given leaf.
fn compute_leaf_hash(leaf_data: &L) -> HashOutput;

Expand Down

0 comments on commit c1c5565

Please sign in to comment.