Skip to content

Commit

Permalink
feat(starknet_patricia,starknet_committer): stop using custom Compile…
Browse files Browse the repository at this point in the history
…dClassHash type
  • Loading branch information
dorimedini-starkware committed Feb 6, 2025
1 parent 24c6121 commit 3e23729
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 79 deletions.
4 changes: 2 additions & 2 deletions crates/starknet_committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;

use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
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;
use starknet_patricia::storage::storage_trait::{StorageKey, StorageValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;

use crate::patricia_merkle_tree::types::{from_class_hash_for_node_index, CompiledClassHash};
use crate::patricia_merkle_tree::types::from_class_hash_for_node_index;

#[cfg(test)]
#[path = "input_test.rs"]
Expand Down
9 changes: 2 additions & 7 deletions crates/starknet_committer/src/forest/filled_forest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
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;
Expand All @@ -18,12 +18,7 @@ use crate::forest::forest_errors::{ForestError, ForestResult};
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::{
ClassesTrie,
CompiledClassHash,
ContractsTrie,
StorageTrieMap,
};
use crate::patricia_merkle_tree::types::{ClassesTrie, ContractsTrie, StorageTrieMap};

pub struct FilledForest {
pub storage_tries: StorageTrieMap,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use starknet_api::core::ContractAddress;
use starknet_api::core::{CompiledClassHash, ContractAddress};
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::{
Expand All @@ -22,7 +22,6 @@ use crate::patricia_merkle_tree::tree::{
OriginalSkeletonContractsTrieConfig,
OriginalSkeletonStorageTrieConfig,
};
use crate::patricia_merkle_tree::types::CompiledClassHash;

#[derive(Debug, PartialEq)]
pub(crate) struct OriginalSkeletonForest<'a> {
Expand Down
3 changes: 1 addition & 2 deletions crates/starknet_committer/src/forest/skeleton_forest_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::external_test_utils::{
create_32_bytes_entry,
Expand Down Expand Up @@ -33,7 +33,6 @@ 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::CompiledClassHash;

macro_rules! compare_skeleton_tree {
($actual_skeleton:expr, $expected_skeleton:expr, $expected_indices:expr) => {{
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_committer/src/hash_function/hash.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use starknet_api::core::CompiledClassHash;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::NodeData;
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::{
Expand All @@ -9,7 +10,6 @@ use starknet_types_core::hash::{Pedersen, Poseidon, StarkHash};

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
use crate::patricia_merkle_tree::types::CompiledClassHash;

/// Implementation of HashFunction for Pedersen hash function.
pub struct PedersenHashFunction;
Expand Down
3 changes: 1 addition & 2 deletions crates/starknet_committer/src/hash_function/hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hex;
use rstest::rstest;
use starknet_api::core::{ClassHash, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce};
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
Expand All @@ -16,7 +16,6 @@ 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::CompiledClassHash;

#[rstest]
// Random StateTreeTuples and the expected hash results were generated and computed elsewhere.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use starknet_types_core::felt::Felt;

use crate::block_committer::input::StarknetStorageValue;
use crate::hash_function::hash::TreeHashFunctionImpl;
use crate::patricia_merkle_tree::types::CompiledClassHash;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ContractState {
Expand All @@ -31,19 +30,6 @@ impl Leaf for StarknetStorageValue {
}
}

impl Leaf for CompiledClassHash {
type Input = Self;
type Output = ();

fn is_empty(&self) -> bool {
self.0 == Felt::ZERO
}

async fn create(input: Self::Input) -> LeafResult<(Self, Self::Output)> {
Ok((input, ()))
}
}

impl Leaf for ContractState {
type Input = ContractStateInput;
type Output = FilledTreeImpl<StarknetStorageValue>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use serde_json::Value;
use starknet_api::core::{ClassHash, Nonce};
use starknet_patricia::hash::hash_trait::HashOutput;
Expand All @@ -11,7 +9,6 @@ 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::CompiledClassHash;

impl DBObject for StarknetStorageValue {
/// Serializes the value into a 32-byte vector.
Expand All @@ -24,18 +21,6 @@ impl DBObject for StarknetStorageValue {
}
}

impl DBObject for CompiledClassHash {
/// Creates a json string describing the leaf and casts it into a byte vector.
fn serialize(&self) -> StorageValue {
let json_string = format!(r#"{{"compiled_class_hash": "{}"}}"#, self.0.to_hex_string());
StorageValue(json_string.into_bytes())
}

fn get_prefix(&self) -> Vec<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

impl DBObject for ContractState {
/// Creates a json string describing the leaf and casts it into a byte vector.
fn serialize(&self) -> StorageValue {
Expand Down Expand Up @@ -64,21 +49,6 @@ impl Deserializable for StarknetStorageValue {
}
}

impl Deserializable for CompiledClassHash {
fn deserialize(value: &StorageValue) -> Result<Self, DeserializationError> {
let json_str = std::str::from_utf8(&value.0)?;
let map: HashMap<String, String> = serde_json::from_str(json_str)?;
let hash_as_hex = map
.get("compiled_class_hash")
.ok_or(DeserializationError::NonExistingKey("compiled_class_hash".to_string()))?;
Ok(Self::from_hex(hash_as_hex)?)
}

fn prefix() -> Vec<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

impl Deserializable for ContractState {
fn deserialize(value: &StorageValue) -> Result<Self, DeserializationError> {
let json_str = std::str::from_utf8(&value.0)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

use rstest::rstest;
use starknet_api::core::{ClassHash, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce};
use starknet_api::felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
Expand All @@ -11,7 +11,6 @@ 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::CompiledClassHash;

#[rstest]
#[case::zero_storage_leaf(StarknetStorageValue(Felt::ZERO))]
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_committer/src/patricia_merkle_tree/tree.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use starknet_api::core::CompiledClassHash;
use starknet_patricia::generate_trie_config;
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
use crate::patricia_merkle_tree::types::CompiledClassHash;

generate_trie_config!(OriginalSkeletonStorageTrieConfig, StarknetStorageValue);

Expand Down
9 changes: 1 addition & 8 deletions crates/starknet_committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;

use starknet_api::core::{ClassHash, ContractAddress};
use starknet_patricia::impl_from_hex_for_felt_wrapper;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress};
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTreeImpl;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_types_core::felt::{Felt, FromStrError};

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
Expand All @@ -13,11 +11,6 @@ pub fn from_class_hash_for_node_index(class_hash: &ClassHash) -> NodeIndex {
NodeIndex::from_leaf_felt(&class_hash.0)
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct CompiledClassHash(pub Felt);

impl_from_hex_for_felt_wrapper!(CompiledClassHash);

pub type StorageTrie = FilledTreeImpl<StarknetStorageValue>;
pub type ClassesTrie = FilledTreeImpl<CompiledClassHash>;
pub type ContractsTrie = FilledTreeImpl<ContractState>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::collections::HashMap;

use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_committer::block_committer::input::{
ConfigImpl,
Input,
StarknetStorageKey,
StarknetStorageValue,
StateDiff,
};
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::storage::errors::DeserializationError;
use starknet_patricia::storage::storage_trait::{StorageKey, StorageValue};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::collections::HashMap;

use pretty_assertions::assert_eq;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_committer::block_committer::input::{
ConfigImpl,
Input,
StarknetStorageKey,
StarknetStorageValue,
StateDiff,
};
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::storage::errors::DeserializationError;
use starknet_patricia::storage::storage_trait::{StorageKey, StorageValue};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;

use ethnum::U256;
use serde_json::json;
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_committer::block_committer::input::{
StarknetStorageKey,
StarknetStorageValue,
Expand All @@ -13,7 +13,6 @@ 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::CompiledClassHash;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ use rand::prelude::IteratorRandom;
use rand::Rng;
use rand_distr::num_traits::ToPrimitive;
use rand_distr::{Distribution, Geometric};
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PATRICIA_KEY_UPPER_BOUND};
use starknet_api::core::{
ClassHash,
CompiledClassHash,
ContractAddress,
Nonce,
PATRICIA_KEY_UPPER_BOUND,
};
use starknet_api::felt;
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::{
ClassesTrie,
CompiledClassHash,
ContractsTrie,
StorageTrie,
StorageTrieMap,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod errors;
pub mod inner_node;
pub mod leaf;
pub mod leaf_impls;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

use starknet_api::core::CompiledClassHash;
use starknet_types_core::felt::Felt;

use crate::patricia_merkle_tree::node_data::errors::LeafResult;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::storage::db_object::{DBObject, Deserializable};
use crate::storage::errors::DeserializationError;
use crate::storage::storage_trait::{StarknetPrefix, StorageValue};

impl Deserializable for CompiledClassHash {
fn deserialize(value: &StorageValue) -> Result<Self, DeserializationError> {
let json_str = std::str::from_utf8(&value.0)?;
let map: HashMap<String, String> = serde_json::from_str(json_str)?;
let hash_as_hex = map
.get("compiled_class_hash")
.ok_or(DeserializationError::NonExistingKey("compiled_class_hash".to_string()))?;
Ok(Self(Felt::from_hex(hash_as_hex)?))
}

fn prefix() -> Vec<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

impl DBObject for CompiledClassHash {
/// Creates a json string describing the leaf and casts it into a byte vector.
fn serialize(&self) -> StorageValue {
let json_string = format!(r#"{{"compiled_class_hash": "{}"}}"#, self.0.to_hex_string());
StorageValue(json_string.into_bytes())
}

fn get_prefix(&self) -> Vec<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

impl Leaf for CompiledClassHash {
type Input = Self;
type Output = ();

fn is_empty(&self) -> bool {
self.0 == Felt::ZERO
}

async fn create(input: Self::Input) -> LeafResult<(Self, Self::Output)> {
Ok((input, ()))
}
}

0 comments on commit 3e23729

Please sign in to comment.