From 1e3413d19b435a4294777ec0041ff28dd699a00d Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Thu, 6 Feb 2025 11:06:36 +0200 Subject: [PATCH] feat(starknet_patricia,starknet_committer): stop using custom ClassHash type --- crates/starknet_committer/Cargo.toml | 1 + .../src/block_committer/commit.rs | 11 +++++++---- .../src/block_committer/input.rs | 17 +++++++++++++---- .../src/forest/filled_forest.rs | 3 +-- .../src/forest/skeleton_forest_test.rs | 4 ++-- .../src/forest/updated_skeleton_forest.rs | 4 ++-- .../src/hash_function/hash_test.rs | 3 ++- .../src/patricia_merkle_tree/leaf/leaf_impl.rs | 3 ++- .../src/patricia_merkle_tree/leaf/leaf_serde.rs | 10 +++------- .../leaf/leaf_serde_test.rs | 6 ++++-- .../src/patricia_merkle_tree/types.rs | 13 +++---------- .../src/committer_cli/parse_input/cast.rs | 4 ++-- .../src/committer_cli/parse_input/read_test.rs | 4 ++-- .../src/committer_cli/tests/python_tests.rs | 4 ++-- .../committer_cli/tests/utils/random_structs.rs | 3 +-- 15 files changed, 47 insertions(+), 43 deletions(-) diff --git a/crates/starknet_committer/Cargo.toml b/crates/starknet_committer/Cargo.toml index 37d8e7ba0cb..26f1a4ba69b 100644 --- a/crates/starknet_committer/Cargo.toml +++ b/crates/starknet_committer/Cargo.toml @@ -20,6 +20,7 @@ tokio = { workspace = true, features = ["rt"] } tracing.workspace = true [dev-dependencies] +starknet_api = { workspace = true, features = ["testing"] } starknet_patricia = { workspace = true, features = ["testing"] } [lints] diff --git a/crates/starknet_committer/src/block_committer/commit.rs b/crates/starknet_committer/src/block_committer/commit.rs index 6bc2463a878..bd1b8543231 100644 --- a/crates/starknet_committer/src/block_committer/commit.rs +++ b/crates/starknet_committer/src/block_committer/commit.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices}; use starknet_patricia_storage::map_storage::MapStorage; use tracing::{info, warn}; @@ -18,7 +18,7 @@ use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkele use crate::forest::updated_skeleton_forest::UpdatedSkeletonForest; use crate::hash_function::hash::TreeHashFunctionImpl; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{from_class_hash_for_node_index, Nonce}; type BlockCommitmentResult = Result; @@ -119,8 +119,11 @@ pub(crate) fn get_all_modified_indices( .iter() .map(|address| from_contract_address_for_node_index(address)) .collect(); - let classes_trie_indices: Vec = - state_diff.class_hash_to_compiled_class_hash.keys().map(NodeIndex::from).collect(); + let classes_trie_indices: Vec = state_diff + .class_hash_to_compiled_class_hash + .keys() + .map(from_class_hash_for_node_index) + .collect(); let storage_tries_indices: HashMap> = accessed_addresses .iter() .map(|address| { diff --git a/crates/starknet_committer/src/block_committer/input.rs b/crates/starknet_committer/src/block_committer/input.rs index 98d6ef90af5..6da43642bea 100644 --- a/crates/starknet_committer/src/block_committer/input.rs +++ b/crates/starknet_committer/src/block_committer/input.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use std::fmt::Debug; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::node_data::leaf::{LeafModifications, SkeletonLeaf}; use starknet_patricia::patricia_merkle_tree::types::NodeIndex; @@ -9,7 +9,11 @@ use starknet_patricia_storage::storage_trait::{DbKey, DbValue}; use starknet_types_core::felt::Felt; use tracing::level_filters::LevelFilter; -use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{ + from_class_hash_for_node_index, + CompiledClassHash, + Nonce, +}; #[cfg(test)] #[path = "input_test.rs"] @@ -134,7 +138,10 @@ impl StateDiff { self.class_hash_to_compiled_class_hash .iter() .map(|(class_hash, compiled_class_hash)| { - (class_hash.into(), SkeletonLeaf::from(compiled_class_hash.0)) + ( + from_class_hash_for_node_index(class_hash), + SkeletonLeaf::from(compiled_class_hash.0), + ) }) .collect() } @@ -159,7 +166,9 @@ impl StateDiff { pub(crate) fn actual_classes_updates(&self) -> LeafModifications { self.class_hash_to_compiled_class_hash .iter() - .map(|(class_hash, compiled_class_hash)| (class_hash.into(), *compiled_class_hash)) + .map(|(class_hash, compiled_class_hash)| { + (from_class_hash_for_node_index(class_hash), *compiled_class_hash) + }) .collect() } } diff --git a/crates/starknet_committer/src/forest/filled_forest.rs b/crates/starknet_committer/src/forest/filled_forest.rs index bafdc35ad42..e9f165c1e07 100644 --- a/crates/starknet_committer/src/forest/filled_forest.rs +++ b/crates/starknet_committer/src/forest/filled_forest.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree; use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications; @@ -19,7 +19,6 @@ use crate::forest::updated_skeleton_forest::UpdatedSkeletonForest; use crate::hash_function::hash::ForestHashFunction; use crate::patricia_merkle_tree::leaf::leaf_impl::{ContractState, ContractStateInput}; use crate::patricia_merkle_tree::types::{ - ClassHash, ClassesTrie, CompiledClassHash, ContractsTrie, diff --git a/crates/starknet_committer/src/forest/skeleton_forest_test.rs b/crates/starknet_committer/src/forest/skeleton_forest_test.rs index fe74a1d123a..211ecb5129e 100644 --- a/crates/starknet_committer/src/forest/skeleton_forest_test.rs +++ b/crates/starknet_committer/src/forest/skeleton_forest_test.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use pretty_assertions::assert_eq; use rstest::rstest; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::external_test_utils::{ create_32_bytes_entry, @@ -33,7 +33,7 @@ use crate::block_committer::input::{ }; use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkeletonForest}; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; macro_rules! compare_skeleton_tree { ($actual_skeleton:expr, $expected_skeleton:expr, $expected_indices:expr) => {{ diff --git a/crates/starknet_committer/src/forest/updated_skeleton_forest.rs b/crates/starknet_committer/src/forest/updated_skeleton_forest.rs index b13a1d2ae7b..0059832a5b3 100644 --- a/crates/starknet_committer/src/forest/updated_skeleton_forest.rs +++ b/crates/starknet_committer/src/forest/updated_skeleton_forest.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::patricia_merkle_tree::node_data::leaf::{LeafModifications, SkeletonLeaf}; use starknet_patricia::patricia_merkle_tree::types::NodeIndex; use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::{ @@ -13,7 +13,7 @@ use crate::block_committer::input::from_contract_address_for_node_index; use crate::forest::forest_errors::{ForestError, ForestResult}; use crate::forest::original_skeleton_forest::OriginalSkeletonForest; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ClassHash, Nonce}; +use crate::patricia_merkle_tree::types::Nonce; pub(crate) struct UpdatedSkeletonForest { pub(crate) classes_trie: UpdatedSkeletonTreeImpl, diff --git a/crates/starknet_committer/src/hash_function/hash_test.rs b/crates/starknet_committer/src/hash_function/hash_test.rs index 9f1b9fe6a1f..815234ba9f9 100644 --- a/crates/starknet_committer/src/hash_function/hash_test.rs +++ b/crates/starknet_committer/src/hash_function/hash_test.rs @@ -1,5 +1,6 @@ use hex; use rstest::rstest; +use starknet_api::core::ClassHash; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{ BinaryData, @@ -15,7 +16,7 @@ use starknet_types_core::hash::Pedersen; use crate::block_committer::input::StarknetStorageValue; use crate::hash_function::hash::TreeHashFunctionImpl; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; #[rstest] // Random StateTreeTuples and the expected hash results were generated and computed elsewhere. diff --git a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs index 8c309e901b9..82941ab50a1 100644 --- a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs +++ b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_impl.rs @@ -1,3 +1,4 @@ +use starknet_api::core::ClassHash; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::filled_tree::tree::{FilledTree, FilledTreeImpl}; use starknet_patricia::patricia_merkle_tree::node_data::errors::{LeafError, LeafResult}; @@ -11,7 +12,7 @@ use starknet_types_core::felt::Felt; use super::leaf_serde::CommitterLeafPrefix; use crate::block_committer::input::StarknetStorageValue; use crate::hash_function::hash::TreeHashFunctionImpl; -use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct ContractState { diff --git a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs index 44811a97fc9..e44306c7a3d 100644 --- a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs +++ b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use serde_json::Value; +use starknet_api::core::ClassHash; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight; use starknet_patricia_storage::db_object::{DBObject, Deserializable}; @@ -10,12 +11,7 @@ use starknet_types_core::felt::Felt; use crate::block_committer::input::StarknetStorageValue; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ - fixed_hex_string_no_prefix, - ClassHash, - CompiledClassHash, - Nonce, -}; +use crate::patricia_merkle_tree::types::{fixed_hex_string_no_prefix, CompiledClassHash, Nonce}; #[derive(Clone, Debug)] pub enum CommitterLeafPrefix { @@ -101,7 +97,7 @@ impl Deserializable for ContractState { Ok(Self { nonce: Nonce::from_hex(&nonce_as_hex)?, storage_root_hash: HashOutput::from_hex(&root_hash_as_hex)?, - class_hash: ClassHash::from_hex(&class_hash_as_hex)?, + class_hash: ClassHash(Felt::from_hex(&class_hash_as_hex)?), }) } } diff --git a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde_test.rs b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde_test.rs index 31b803fd52c..53c6e5848b2 100644 --- a/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde_test.rs +++ b/crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde_test.rs @@ -1,6 +1,8 @@ use std::fmt::Debug; use rstest::rstest; +use starknet_api::core::ClassHash; +use starknet_api::felt; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf; use starknet_patricia_storage::db_object::Deserializable; @@ -9,7 +11,7 @@ use starknet_types_core::felt::Felt; use crate::block_committer::input::StarknetStorageValue; use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; -use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use crate::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; #[rstest] #[case::zero_storage_leaf(StarknetStorageValue(Felt::ZERO))] @@ -62,6 +64,6 @@ fn test_deserialize_contract_state_without_nonce() { // Validate the fields (nonce should be the default "0") assert_eq!(contract_state.nonce, Nonce::from_hex("0x0").unwrap()); - assert_eq!(contract_state.class_hash, ClassHash::from_hex("0x1234abcd").unwrap()); + assert_eq!(contract_state.class_hash, ClassHash(felt!("0x1234abcd"))); assert_eq!(contract_state.storage_root_hash, HashOutput::from_hex("0x5678").unwrap()); } diff --git a/crates/starknet_committer/src/patricia_merkle_tree/types.rs b/crates/starknet_committer/src/patricia_merkle_tree/types.rs index f7d97809951..544c98d0a43 100644 --- a/crates/starknet_committer/src/patricia_merkle_tree/types.rs +++ b/crates/starknet_committer/src/patricia_merkle_tree/types.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_patricia::impl_from_hex_for_felt_wrapper; use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTreeImpl; use starknet_patricia::patricia_merkle_tree::types::NodeIndex; @@ -13,17 +13,10 @@ pub fn fixed_hex_string_no_prefix(felt: &Felt) -> String { format!("{:064x}", felt) } -// TODO(Nimrod, 1/6/2024): Use the ClassHash defined in starknet-types-core when available. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub struct ClassHash(pub Felt); - -impl From<&ClassHash> for NodeIndex { - fn from(val: &ClassHash) -> Self { - NodeIndex::from_leaf_felt(&val.0) - } +pub fn from_class_hash_for_node_index(class_hash: &ClassHash) -> NodeIndex { + NodeIndex::from_leaf_felt(&class_hash.0) } -impl_from_hex_for_felt_wrapper!(ClassHash); #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct Nonce(pub Felt); diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/cast.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/cast.rs index ebc608ee421..e7c3b9d0448 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/cast.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/cast.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_committer::block_committer::input::{ ConfigImpl, Input, @@ -8,7 +8,7 @@ use starknet_committer::block_committer::input::{ StarknetStorageValue, StateDiff, }; -use starknet_committer::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use starknet_committer::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia_storage::errors::DeserializationError; use starknet_patricia_storage::storage_trait::{DbKey, DbValue}; diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/read_test.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/read_test.rs index a360098e35e..2a8b7405501 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/read_test.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/parse_input/read_test.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use assert_matches::assert_matches; use pretty_assertions::assert_eq; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_committer::block_committer::input::{ ConfigImpl, Input, @@ -10,7 +10,7 @@ use starknet_committer::block_committer::input::{ StarknetStorageValue, StateDiff, }; -use starknet_committer::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce}; +use starknet_committer::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia_storage::errors::DeserializationError; use starknet_patricia_storage::storage_trait::{DbKey, DbValue}; diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs index 69acf8f4763..d64d6d87be4 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs @@ -3,7 +3,7 @@ use std::fmt::Debug; use ethnum::U256; use serde_json::json; -use starknet_api::core::ContractAddress; +use starknet_api::core::{ClassHash, ContractAddress}; use starknet_committer::block_committer::input::{ StarknetStorageKey, StarknetStorageValue, @@ -13,7 +13,7 @@ use starknet_committer::forest::filled_forest::FilledForest; 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::{ClassHash, CompiledClassHash, Nonce}; +use starknet_committer::patricia_merkle_tree::types::{CompiledClassHash, Nonce}; use starknet_patricia::hash::hash_trait::HashOutput; use starknet_patricia::patricia_merkle_tree::external_test_utils::single_tree_flow_test; use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode; diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/random_structs.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/random_structs.rs index 8cfc37a005a..4b62038496f 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/random_structs.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/random_structs.rs @@ -6,12 +6,11 @@ use rand::prelude::IteratorRandom; use rand::Rng; use rand_distr::num_traits::ToPrimitive; use rand_distr::{Distribution, Geometric}; -use starknet_api::core::{ContractAddress, PATRICIA_KEY_UPPER_BOUND}; +use starknet_api::core::{ClassHash, ContractAddress, PATRICIA_KEY_UPPER_BOUND}; use starknet_committer::block_committer::input::StarknetStorageValue; use starknet_committer::forest::filled_forest::FilledForest; use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState; use starknet_committer::patricia_merkle_tree::types::{ - ClassHash, ClassesTrie, CompiledClassHash, ContractsTrie,