Skip to content

Commit

Permalink
feat(starknet_patricia,starknet_committer): stop using custom Felt type
Browse files Browse the repository at this point in the history
  • Loading branch information
dorimedini-starkware committed Feb 11, 2025
1 parent 29aeb18 commit 564f9e5
Show file tree
Hide file tree
Showing 33 changed files with 90 additions and 182 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/starknet_committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;

use starknet_patricia::felt::Felt;
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::{DbKey, DbValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;

use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rstest::rstest;
use starknet_patricia::felt::Felt;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_types_core::felt::Felt;

use crate::block_committer::input::ContractAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashMap;

use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::external_test_utils::{
create_32_bytes_entry,
Expand All @@ -19,6 +18,7 @@ use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndice
use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;

use crate::block_committer::commit::get_all_modified_indices;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashMap;

use starknet_patricia::felt::Felt;
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::{
UpdatedSkeletonTree,
UpdatedSkeletonTreeImpl,
};
use starknet_types_core::felt::Felt;

use crate::block_committer::input::ContractAddress;
use crate::forest::forest_errors::{ForestError, ForestResult};
Expand Down
31 changes: 11 additions & 20 deletions crates/starknet_committer/src/hash_function/hash.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use starknet_patricia::felt::Felt;
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::{
HashFunction,
TreeHashFunction,
};
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Pedersen, Poseidon, StarkHash};

use crate::block_committer::input::StarknetStorageValue;
Expand All @@ -15,15 +15,15 @@ use crate::patricia_merkle_tree::types::CompiledClassHash;
pub struct PedersenHashFunction;
impl HashFunction for PedersenHashFunction {
fn hash(left: &Felt, right: &Felt) -> HashOutput {
HashOutput(Felt(Pedersen::hash(&left.0, &right.0)))
HashOutput(Pedersen::hash(left, right))
}
}

/// Implementation of HashFunction for Poseidon hash function.
pub struct PoseidonHashFunction;
impl HashFunction for PoseidonHashFunction {
fn hash(left: &Felt, right: &Felt) -> HashOutput {
HashOutput(Felt(Poseidon::hash(&left.0, &right.0)))
HashOutput(Poseidon::hash(left, right))
}
}

Expand All @@ -43,19 +43,13 @@ impl TreeHashFunctionImpl {
/// <https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/starknet-state/#trie_construction>
impl TreeHashFunction<ContractState> for TreeHashFunctionImpl {
fn compute_leaf_hash(contract_state: &ContractState) -> HashOutput {
HashOutput(
Pedersen::hash(
&Pedersen::hash(
&Pedersen::hash(
&contract_state.class_hash.0.into(),
&contract_state.storage_root_hash.0.into(),
),
&contract_state.nonce.0.into(),
),
&Self::CONTRACT_STATE_HASH_VERSION.into(),
)
.into(),
)
HashOutput(Pedersen::hash(
&Pedersen::hash(
&Pedersen::hash(&contract_state.class_hash.0, &contract_state.storage_root_hash.0),
&contract_state.nonce.0,
),
&Self::CONTRACT_STATE_HASH_VERSION,
))
}
fn compute_node_hash(node_data: &NodeData<ContractState>) -> HashOutput {
Self::compute_node_hash_with_inner_hash_function::<PedersenHashFunction>(node_data)
Expand All @@ -71,10 +65,7 @@ impl TreeHashFunction<CompiledClassHash> for TreeHashFunctionImpl {
.expect(
"could not parse hex string corresponding to b'CONTRACT_CLASS_LEAF_V0' to Felt",
);
HashOutput(
Poseidon::hash(&contract_class_leaf_version.into(), &compiled_class_hash.0.into())
.into(),
)
HashOutput(Poseidon::hash(&contract_class_leaf_version, &compiled_class_hash.0))
}
fn compute_node_hash(node_data: &NodeData<CompiledClassHash>) -> HashOutput {
Self::compute_node_hash_with_inner_hash_function::<PoseidonHashFunction>(node_data)
Expand Down
12 changes: 4 additions & 8 deletions crates/starknet_committer/src/hash_function/hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use hex;
use rstest::rstest;
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
Expand All @@ -10,6 +9,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
PathToBottom,
};
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::Pedersen;

use crate::block_committer::input::StarknetStorageValue;
Expand Down Expand Up @@ -97,10 +97,7 @@ fn test_tree_hash_function_impl_binary_node(
TreeHashFunctionImpl::compute_node_hash(&NodeData::<StarknetStorageValue>::Binary(
BinaryData { left_hash: HashOutput(left_hash), right_hash: HashOutput(right_hash) },
));
assert_eq!(
hash_output,
HashOutput(Pedersen::hash(&left_hash.into(), &right_hash.into()).into())
);
assert_eq!(hash_output, HashOutput(Pedersen::hash(&left_hash, &right_hash)));
assert_eq!(hash_output, HashOutput(expected_hash));
}

Expand All @@ -126,9 +123,8 @@ fn test_tree_hash_function_impl_edge_node(
.unwrap(),
}),
);
let direct_hash_computation = HashOutput(
Felt::from(Pedersen::hash(&bottom_hash.into(), &edge_path.into())) + length.into(),
);
let direct_hash_computation =
HashOutput(Pedersen::hash(&bottom_hash, &edge_path.into()) + Felt::from(length));
assert_eq!(hash_output, HashOutput(expected_hash));
assert_eq!(hash_output, direct_hash_computation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use starknet_patricia::felt::Felt;
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};
Expand All @@ -7,6 +6,7 @@ use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;
use starknet_patricia_storage::db_object::HasStaticPrefix;
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
use starknet_types_core::felt::Felt;

use super::leaf_serde::CommitterLeafPrefix;
use crate::block_committer::input::StarknetStorageValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use std::collections::HashMap;

use serde_json::Value;
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
use starknet_patricia_storage::db_object::{DBObject, Deserializable};
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue};
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::{
fixed_hex_string_no_prefix,
ClassHash,
CompiledClassHash,
Nonce,
};

#[derive(Clone, Debug)]
pub enum CommitterLeafPrefix {
Expand Down Expand Up @@ -39,7 +44,7 @@ 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) -> DbValue {
let json_string = format!(r#"{{"compiled_class_hash": "{}"}}"#, self.0.to_hex());
let json_string = format!(r#"{{"compiled_class_hash": "{}"}}"#, self.0.to_hex_string());
DbValue(json_string.into_bytes())
}
}
Expand All @@ -49,10 +54,10 @@ impl DBObject for ContractState {
fn serialize(&self) -> DbValue {
let json_string = format!(
r#"{{"contract_hash": "{}", "storage_commitment_tree": {{"root": "{}", "height": {}}}, "nonce": "{}"}}"#,
self.class_hash.0.to_fixed_hex_string(),
self.storage_root_hash.0.to_fixed_hex_string(),
fixed_hex_string_no_prefix(&self.class_hash.0),
fixed_hex_string_no_prefix(&self.storage_root_hash.0),
SubTreeHeight::ACTUAL_HEIGHT,
self.nonce.0.to_hex(),
self.nonce.0.to_hex_string(),
);
DbValue(json_string.into_bytes())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt::Debug;

use rstest::rstest;
use starknet_patricia::felt::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;
use starknet_patricia_storage::storage_trait::DbValue;
use starknet_types_core::felt::Felt;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
Expand Down
7 changes: 5 additions & 2 deletions crates/starknet_committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::collections::HashMap;

use starknet_patricia::felt::Felt;
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;
use starknet_types_core::felt::FromStrError;
use starknet_types_core::felt::{Felt, FromStrError};

use crate::block_committer::input::{ContractAddress, StarknetStorageValue};
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;

pub fn fixed_hex_string_no_prefix(felt: &Felt) -> String {
felt.to_fixed_hex_string().strip_prefix("0x").unwrap_or("0").to_string()
}

// 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rstest::rstest;
use starknet_patricia::felt::Felt;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_types_core::felt::Felt;

use crate::block_committer::input::{ContractAddress, StarknetStorageKey};

Expand Down
1 change: 1 addition & 0 deletions crates/starknet_committer_and_os_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Cli for the committer package."
workspace = true

[dev-dependencies]
assert_matches.workspace = true
criterion = { workspace = true, features = ["html_reports"] }
futures.workspace = true
pretty_assertions.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl SerializedForest {
let compiled_class_root_hash = self.0.get_compiled_class_root_hash().0;
Output {
storage,
contract_storage_root_hash: contract_storage_root_hash.to_hex(),
compiled_class_root_hash: compiled_class_root_hash.to_hex(),
contract_storage_root_hash: contract_storage_root_hash.to_hex_string(),
compiled_class_root_hash: compiled_class_root_hash.to_hex_string(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use starknet_committer::block_committer::input::{
StateDiff,
};
use starknet_committer::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce};
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;

use crate::committer_cli::parse_input::raw_input::RawInput;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use assert_matches::assert_matches;
use pretty_assertions::assert_eq;
use starknet_committer::block_committer::input::{
ConfigImpl,
Expand All @@ -10,10 +11,10 @@ use starknet_committer::block_committer::input::{
StateDiff,
};
use starknet_committer::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce};
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;

use super::parse_input;
Expand Down Expand Up @@ -276,9 +277,9 @@ fn test_input_parsing_with_mapping_key_duplicate() {
"#;
let expected_error =
"address to class hash: ContractAddress(6646139978924584093298644040422522880)";
assert!(matches!(
"address to class hash: ContractAddress(0x5000000000001005900000000000000)";
assert_matches!(
parse_input(input).unwrap_err(),
DeserializationError::KeyDuplicate(key) if key == expected_error
));
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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_patricia::felt::Felt;
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 All @@ -30,6 +29,7 @@ use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue, Storage};
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Pedersen, StarkHash};
use thiserror;
use tracing::{debug, error, info, warn};
Expand Down Expand Up @@ -236,7 +236,7 @@ pub(crate) fn test_hash_function(hash_input: HashMap<String, u128>) -> String {
let y_felt = Felt::from(*y);

// Compute the hash.
let hash_result = Pedersen::hash(&x_felt.into(), &y_felt.into());
let hash_result = Pedersen::hash(&x_felt, &y_felt);

// Serialize the hash result.
serde_json::to_string(&hash_result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::collections::HashMap;
use ethnum::U256;
use serde::{Deserialize, Deserializer};
use starknet_committer::block_committer::input::StarknetStorageValue;
use starknet_patricia::felt::Felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;

use crate::committer_cli::parse_input::cast::add_unique;
use crate::committer_cli::parse_input::raw_input::RawStorageEntry;
Expand Down
Loading

0 comments on commit 564f9e5

Please sign in to comment.