diff --git a/crates/starknet_committer/src/block_committer/input.rs b/crates/starknet_committer/src/block_committer/input.rs index 7d26e8a0a69..2a347968011 100644 --- a/crates/starknet_committer/src/block_committer/input.rs +++ b/crates/starknet_committer/src/block_committer/input.rs @@ -119,7 +119,7 @@ impl From for StateDiff { } /// Trait contains all optional configurations of the committer. -pub trait Config: Debug + Eq + PartialEq { +pub trait Config: Debug + Eq + PartialEq + Send + Sync { /// Indicates whether a warning should be given in case of a trivial state update. /// If the configuration is set, it requires that the storage will contain the original data for /// the modified leaves. Otherwise, it is not required. diff --git a/crates/starknet_committer/src/db/external_test_utils.rs b/crates/starknet_committer/src/db/external_test_utils.rs index 322ae88c7bd..4048cdd47f5 100644 --- a/crates/starknet_committer/src/db/external_test_utils.rs +++ b/crates/starknet_committer/src/db/external_test_utils.rs @@ -25,7 +25,7 @@ pub async fn tree_computation_flow( leaf_modifications: LeafModifications, storage: &mut MapStorage, root_hash: HashOutput, - config: impl OriginalSkeletonTreeConfig, + config: impl OriginalSkeletonTreeConfig, ) -> FilledTreeImpl where TH: TreeHashFunction + 'static, @@ -73,7 +73,7 @@ pub async fn single_tree_flow_test< leaf_modifications: LeafModifications, storage: &mut MapStorage, root_hash: HashOutput, - config: impl OriginalSkeletonTreeConfig, + config: impl OriginalSkeletonTreeConfig, ) -> String { // Move from leaf number to actual index. let leaf_modifications = leaf_modifications diff --git a/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs b/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs index 6dd3ce69462..908bc0f0973 100644 --- a/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs +++ b/crates/starknet_committer/src/db/facts_db/create_facts_tree.rs @@ -10,10 +10,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{ NodeData, }; use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications}; -use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::{ - NoCompareOriginalSkeletonTrieConfig, - OriginalSkeletonTreeConfig, -}; +use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig; use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonNode; use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::{ OriginalSkeletonTreeImpl, @@ -29,6 +26,7 @@ use crate::db::db_layout::NodeLayout; use crate::db::facts_db::db::FactsNodeLayout; use crate::db::facts_db::traversal::get_roots_from_storage; use crate::db::facts_db::types::FactsSubTree; +use crate::patricia_merkle_tree::tree::OriginalSkeletonTrieDontCompareConfig; #[cfg(test)] #[path = "create_facts_tree_test.rs"] @@ -54,7 +52,7 @@ async fn fetch_nodes<'a, L, Layout>( subtrees: Vec, storage: &mut impl Storage, leaf_modifications: &LeafModifications, - config: &impl OriginalSkeletonTreeConfig, + config: &impl OriginalSkeletonTreeConfig, mut previous_leaves: Option<&mut HashMap>, key_context: &::KeyContext, ) -> OriginalSkeletonTreeResult<()> @@ -155,7 +153,7 @@ pub async fn create_original_skeleton_tree<'a, L: Leaf>( storage: &mut impl Storage, root_hash: HashOutput, sorted_leaf_indices: SortedLeafIndices<'a>, - config: &impl OriginalSkeletonTreeConfig, + config: &impl OriginalSkeletonTreeConfig, leaf_modifications: &LeafModifications, key_context: &::KeyContext, ) -> OriginalSkeletonTreeResult> { @@ -190,7 +188,7 @@ pub async fn create_original_skeleton_tree_and_get_previous_leaves<'a, L: Leaf>( root_hash: HashOutput, sorted_leaf_indices: SortedLeafIndices<'a>, leaf_modifications: &LeafModifications, - config: &impl OriginalSkeletonTreeConfig, + config: &impl OriginalSkeletonTreeConfig, key_context: &::KeyContext, ) -> OriginalSkeletonTreeResult<(OriginalSkeletonTreeImpl<'a>, HashMap)> { if sorted_leaf_indices.is_empty() { @@ -228,7 +226,7 @@ pub async fn get_leaves<'a, L: Leaf>( sorted_leaf_indices: SortedLeafIndices<'a>, key_context: &::KeyContext, ) -> OriginalSkeletonTreeResult> { - let config = NoCompareOriginalSkeletonTrieConfig::default(); + let config = OriginalSkeletonTrieDontCompareConfig; let leaf_modifications = LeafModifications::new(); let (_, previous_leaves) = create_original_skeleton_tree_and_get_previous_leaves( storage, @@ -328,7 +326,7 @@ fn handle_child_subtree<'a, SubTree: SubTreeTrait<'a>>( fn log_warning_for_empty_leaves + Debug>( leaf_indices: &[T], leaf_modifications: &LeafModifications, - config: &impl OriginalSkeletonTreeConfig, + config: &impl OriginalSkeletonTreeConfig, ) -> OriginalSkeletonTreeResult<()> { if !config.compare_modified_leaves() { return Ok(()); diff --git a/crates/starknet_committer/src/db/facts_db/create_facts_tree_test.rs b/crates/starknet_committer/src/db/facts_db/create_facts_tree_test.rs index 6530a786d9a..78ed54501fa 100644 --- a/crates/starknet_committer/src/db/facts_db/create_facts_tree_test.rs +++ b/crates/starknet_committer/src/db/facts_db/create_facts_tree_test.rs @@ -13,7 +13,6 @@ use starknet_patricia::patricia_merkle_tree::external_test_utils::{ create_unmodified_subtree_skeleton_node, AdditionHash, MockLeaf, - OriginalSkeletonMockTrieConfig, }; use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications; use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonNode; @@ -24,6 +23,7 @@ use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue}; use starknet_types_core::felt::Felt; use crate::db::facts_db::create_facts_tree::create_original_skeleton_tree; +use crate::patricia_merkle_tree::tree::OriginalSkeletonTrieConfig; #[tokio::test] #[rstest] @@ -208,7 +208,7 @@ async fn test_create_tree( .into_iter() .map(|(idx, leaf)| (NodeIndex::from_subtree_index(idx, subtree_height), leaf)) .collect(); - let config = OriginalSkeletonMockTrieConfig::new(compare_modified_leaves); + let config = OriginalSkeletonTrieConfig::new(compare_modified_leaves); let mut sorted_leaf_indices: Vec = leaf_modifications.keys().copied().collect(); let sorted_leaf_indices = SortedLeafIndices::new(&mut sorted_leaf_indices); let skeleton_tree = create_original_skeleton_tree( diff --git a/crates/starknet_committer/src/db/facts_db/db.rs b/crates/starknet_committer/src/db/facts_db/db.rs index 2e7ed2e88d1..6928120d38d 100644 --- a/crates/starknet_committer/src/db/facts_db/db.rs +++ b/crates/starknet_committer/src/db/facts_db/db.rs @@ -37,11 +37,7 @@ use crate::forest::filled_forest::FilledForest; use crate::forest::forest_errors::{ForestError, ForestResult}; use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkeletonForest}; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::tree::{ - OriginalSkeletonClassesTrieConfig, - OriginalSkeletonContractsTrieConfig, - OriginalSkeletonStorageTrieConfig, -}; +use crate::patricia_merkle_tree::tree::OriginalSkeletonTrieConfig; use crate::patricia_merkle_tree::types::CompiledClassHash; pub struct FactsNodeLayout {} @@ -80,7 +76,7 @@ impl FactsDb { contracts_trie_root_hash, contracts_trie_sorted_indices, &HashMap::new(), - &OriginalSkeletonContractsTrieConfig::new(), + &OriginalSkeletonTrieConfig::new(true), &EmptyKeyContext, ) .await?) @@ -101,8 +97,7 @@ impl FactsDb { let contract_state = original_contracts_trie_leaves .get(&contract_address_into_node_index(address)) .ok_or(ForestError::MissingContractCurrentState(*address))?; - let config = - OriginalSkeletonStorageTrieConfig::new(config.warn_on_trivial_modifications()); + let config = OriginalSkeletonTrieConfig::new(config.warn_on_trivial_modifications()); let original_skeleton = create_original_skeleton_tree( &mut self.storage, @@ -125,7 +120,7 @@ impl FactsDb { config: &impl Config, contracts_trie_sorted_indices: SortedLeafIndices<'a>, ) -> ForestResult> { - let config = OriginalSkeletonClassesTrieConfig::new(config.warn_on_trivial_modifications()); + let config = OriginalSkeletonTrieConfig::new(config.warn_on_trivial_modifications()); Ok(create_original_skeleton_tree( &mut self.storage, diff --git a/crates/starknet_committer/src/patricia_merkle_tree/tree.rs b/crates/starknet_committer/src/patricia_merkle_tree/tree.rs index 2326aa47a7e..41319f99750 100644 --- a/crates/starknet_committer/src/patricia_merkle_tree/tree.rs +++ b/crates/starknet_committer/src/patricia_merkle_tree/tree.rs @@ -2,7 +2,6 @@ use std::collections::HashMap; use starknet_api::core::{ClassHash, ContractAddress}; use starknet_api::hash::HashOutput; -use starknet_patricia::generate_trie_config; use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig; use starknet_patricia::patricia_merkle_tree::traversal::TraversalResult; use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices}; @@ -24,21 +23,28 @@ use crate::patricia_merkle_tree::types::{ RootHashes, StarknetForestProofs, }; -generate_trie_config!(OriginalSkeletonStorageTrieConfig, StarknetStorageValue); -generate_trie_config!(OriginalSkeletonClassesTrieConfig, CompiledClassHash); +pub(crate) struct OriginalSkeletonTrieConfig { + compare_modified_leaves: bool, +} -pub(crate) struct OriginalSkeletonContractsTrieConfig; +impl OriginalSkeletonTrieConfig { + pub(crate) fn new(should_compare_modified_leaves: bool) -> Self { + Self { compare_modified_leaves: should_compare_modified_leaves } + } +} -impl OriginalSkeletonTreeConfig for OriginalSkeletonContractsTrieConfig { +impl OriginalSkeletonTreeConfig for OriginalSkeletonTrieConfig { fn compare_modified_leaves(&self) -> bool { - false + self.compare_modified_leaves } } -impl OriginalSkeletonContractsTrieConfig { - pub(crate) fn new() -> Self { - Self +pub(crate) struct OriginalSkeletonTrieDontCompareConfig; + +impl OriginalSkeletonTreeConfig for OriginalSkeletonTrieDontCompareConfig { + fn compare_modified_leaves(&self) -> bool { + false } } diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs b/crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs index 32a29392d83..eeb8af76078 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/external_test_utils.rs @@ -21,10 +21,8 @@ use super::node_data::leaf::Leaf; use super::original_skeleton_tree::node::OriginalSkeletonNode; use super::types::{NodeIndex, SubTreeHeight}; use crate::felt::u256_from_felt; -use crate::generate_trie_config; use crate::patricia_merkle_tree::errors::TypesError; use crate::patricia_merkle_tree::node_data::errors::{LeafError, LeafResult}; -use crate::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig; pub(crate) const TEST_PREFIX: &[u8] = &[0]; @@ -70,8 +68,6 @@ impl Leaf for MockLeaf { } } -generate_trie_config!(OriginalSkeletonMockTrieConfig, MockLeaf); - pub fn u256_try_into_felt(value: &U256) -> Result> { if *value > u256_from_felt(&Felt::MAX) { return Err(TypesError::ConversionError { diff --git a/crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/config.rs b/crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/config.rs index 079888dda72..de9fcef85f9 100644 --- a/crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/config.rs +++ b/crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/config.rs @@ -1,41 +1,6 @@ -use crate::patricia_merkle_tree::node_data::leaf::Leaf; - /// Configures the creation of an original skeleton tree. -pub trait OriginalSkeletonTreeConfig { +pub trait OriginalSkeletonTreeConfig { /// 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; } - -// TODO(Aviv 05/08/2024): Move this macro to starknet_committer crate -#[macro_export] -macro_rules! generate_trie_config { - ($struct_name:ident, $leaf_type:ty) => { - pub struct $struct_name { - compare_modified_leaves: bool, - } - - impl $struct_name { - #[allow(dead_code)] - pub fn new(compare_modified_leaves: bool) -> Self { - Self { compare_modified_leaves } - } - } - - impl OriginalSkeletonTreeConfig<$leaf_type> for $struct_name { - fn compare_modified_leaves(&self) -> bool { - self.compare_modified_leaves - } - } - }; -} - -#[derive(Default)] -/// Generic config that doesn't compare the modified leaves. -pub struct NoCompareOriginalSkeletonTrieConfig(std::marker::PhantomData); - -impl OriginalSkeletonTreeConfig for NoCompareOriginalSkeletonTrieConfig { - fn compare_modified_leaves(&self) -> bool { - false - } -}