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 6, 2025
1 parent 221fcc0 commit 2d8c7c7
Show file tree
Hide file tree
Showing 34 changed files with 77 additions and 173 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::{StorageKey, StorageValue};
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::{StorageKey, StorageValue};
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
4 changes: 2 additions & 2 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 @@ -127,7 +127,7 @@ fn test_tree_hash_function_impl_edge_node(
}),
);
let direct_hash_computation = HashOutput(
Felt::from(Pedersen::hash(&bottom_hash.into(), &edge_path.into())) + length.into(),
Felt::from(Pedersen::hash(&bottom_hash.into(), &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,10 +1,10 @@
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};
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;
use starknet_types_core::felt::Felt;

use crate::block_committer::input::StarknetStorageValue;
use crate::hash_function::hash::TreeHashFunctionImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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::{StarknetPrefix, StorageValue};
use starknet_types_core::felt::Felt;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
Expand All @@ -26,7 +26,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) -> StorageValue {
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());
StorageValue(json_string.into_bytes())
}

Expand All @@ -43,7 +43,7 @@ impl DBObject for ContractState {
self.class_hash.0.to_fixed_hex_string(),
self.storage_root_hash.0.to_fixed_hex_string(),
SubTreeHeight::ACTUAL_HEIGHT,
self.nonce.0.to_hex(),
self.nonce.0.to_hex_string(),
);
StorageValue(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::StorageValue;
use starknet_types_core::felt::Felt;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
Expand Down
3 changes: 1 addition & 2 deletions crates/starknet_committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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;
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::{StorageKey, StorageValue};
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::{StorageKey, StorageValue};
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::{Storage, StorageKey, StorageValue};
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Pedersen, StarkHash};
use thiserror;
use tracing::{debug, error, info, warn};
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::{StorageKey, StorageValue};
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ use starknet_committer::patricia_merkle_tree::types::{
StorageTrie,
StorageTrieMap,
};
use starknet_patricia::felt::Felt;
use starknet_patricia::felt::u256_from_felt;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::external_test_utils::get_random_u256;
use starknet_patricia::patricia_merkle_tree::external_test_utils::{
get_random_u256,
u256_try_into_felt,
};
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
BinaryData,
Expand All @@ -32,6 +35,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
PathToBottom,
};
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_types_core::felt::Felt;
use strum::IntoEnumIterator;

pub trait RandomValue {
Expand All @@ -44,7 +48,7 @@ pub trait DummyRandomValue {

impl RandomValue for Felt {
fn random<R: Rng>(rng: &mut R, _max: Option<U256>) -> Self {
Felt::try_from(&get_random_u256(rng, U256::ONE, U256::from(&Felt::MAX) + 1))
u256_try_into_felt(&get_random_u256(rng, U256::ONE, u256_from_felt(&Felt::MAX) + 1))
.expect("Failed to create a random Felt")
}
}
Expand Down
Loading

0 comments on commit 2d8c7c7

Please sign in to comment.