Skip to content

refactor: remove dependency on tree_height parameter on forest impl (… #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions crates/committer/src/block_committer/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,27 @@ pub async fn commit_block(input: Input) -> BlockCommitmentResult<FilledForestImp
MapStorage::from(input.storage),
input.contracts_trie_root_hash,
input.classes_trie_root_hash,
input.tree_heights,
&input.current_contracts_trie_leaves,
&input.state_diff,
)?;

let updated_forest = UpdatedSkeletonForestImpl::<UpdatedSkeletonTreeImpl>::create(
&mut original_forest,
&StateDiff::skeleton_classes_updates(
&input.state_diff.class_hash_to_compiled_class_hash,
input.tree_heights,
),
&input
.state_diff
.skeleton_storage_updates(input.tree_heights),
&StateDiff::skeleton_classes_updates(&input.state_diff.class_hash_to_compiled_class_hash),
&input.state_diff.skeleton_storage_updates(),
&input.current_contracts_trie_leaves,
&input.state_diff.address_to_class_hash,
&input.state_diff.address_to_nonce,
input.tree_heights,
)?;

Ok(
FilledForestImpl::create::<UpdatedSkeletonTreeImpl, TreeHashFunctionImpl>(
updated_forest,
input.state_diff.actual_storage_updates(input.tree_heights),
StateDiff::actual_classes_updates(
&input.state_diff.class_hash_to_compiled_class_hash,
input.tree_heights,
),
input.state_diff.actual_storage_updates(),
StateDiff::actual_classes_updates(&input.state_diff.class_hash_to_compiled_class_hash),
&input.current_contracts_trie_leaves,
&input.state_diff.address_to_class_hash,
&input.state_diff.address_to_nonce,
input.tree_heights,
)
.await?,
)
Expand Down
12 changes: 4 additions & 8 deletions crates/committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl StateDiff {
/// For each modified contract calculates it's actual storage updates.
pub(crate) fn skeleton_storage_updates(
&self,
tree_height: TreeHeight,
) -> HashMap<ContractAddress, LeafModifications<SkeletonLeaf>> {
self.accessed_addresses()
.iter()
Expand All @@ -66,7 +65,7 @@ impl StateDiff {
.iter()
.map(|(key, value)| {
(
NodeIndex::from_starknet_storage_key(key, &tree_height),
NodeIndex::from_starknet_storage_key(key),
SkeletonLeaf::from(value.0),
)
})
Expand All @@ -80,13 +79,12 @@ impl StateDiff {

pub(crate) fn skeleton_classes_updates(
class_hash_to_compiled_class_hash: &HashMap<ClassHash, CompiledClassHash>,
tree_height: TreeHeight,
) -> LeafModifications<SkeletonLeaf> {
class_hash_to_compiled_class_hash
.iter()
.map(|(class_hash, compiled_class_hash)| {
(
NodeIndex::from_class_hash(class_hash, &tree_height),
NodeIndex::from_class_hash(class_hash),
SkeletonLeaf::from(compiled_class_hash.0),
)
})
Expand All @@ -95,7 +93,6 @@ impl StateDiff {

pub(crate) fn actual_storage_updates(
&self,
tree_height: TreeHeight,
) -> HashMap<ContractAddress, LeafModifications<LeafDataImpl>> {
self.accessed_addresses()
.iter()
Expand All @@ -105,7 +102,7 @@ impl StateDiff {
.iter()
.map(|(key, value)| {
(
NodeIndex::from_starknet_storage_key(key, &tree_height),
NodeIndex::from_starknet_storage_key(key),
LeafDataImpl::StorageValue(value.0),
)
})
Expand All @@ -119,13 +116,12 @@ impl StateDiff {

pub(crate) fn actual_classes_updates(
class_hash_to_compiled_class_hash: &HashMap<ClassHash, CompiledClassHash>,
tree_height: TreeHeight,
) -> LeafModifications<LeafDataImpl> {
class_hash_to_compiled_class_hash
.iter()
.map(|(class_hash, compiled_class_hash)| {
(
NodeIndex::from_class_hash(class_hash, &tree_height),
NodeIndex::from_class_hash(class_hash),
LeafDataImpl::CompiledClassHash(*compiled_class_hash),
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::patricia_merkle_tree::filled_tree::tree::{FilledTree, FilledTreeImpl}
use crate::patricia_merkle_tree::node_data::leaf::{
ContractState, LeafData, LeafDataImpl, LeafModifications,
};
use crate::patricia_merkle_tree::types::{NodeIndex, TreeHeight};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
use crate::patricia_merkle_tree::updated_skeleton_tree::skeleton_forest::UpdatedSkeletonForestImpl;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;
Expand Down Expand Up @@ -66,7 +66,6 @@ impl FilledForestImpl {
current_contracts_trie_leaves: &HashMap<ContractAddress, ContractState>,
address_to_class_hash: &HashMap<ContractAddress, ClassHash>,
address_to_nonce: &HashMap<ContractAddress, Nonce>,
tree_heights: TreeHeight,
) -> ForestResult<Self> {
let classes_trie =
FilledTreeImpl::create::<TH>(updated_forest.classes_trie, classes_updates).await?;
Expand Down Expand Up @@ -100,7 +99,7 @@ impl FilledForestImpl {
while let Some(result) = tasks.join_next().await {
let (address, new_contract_state, filled_storage_trie) = result??;
contracts_trie_modifications.insert(
NodeIndex::from_contract_address(&address, &tree_heights),
NodeIndex::from_contract_address(&address),
LeafDataImpl::ContractState(new_contract_state),
);
filled_storage_tries.insert(address, filled_storage_trie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::node_data::leaf::ContractState;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTree;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::types::TreeHeight;
use crate::storage::storage_trait::Storage;
use std::collections::HashMap;
use std::collections::HashSet;
Expand All @@ -24,7 +23,6 @@ pub(crate) trait OriginalSkeletonForest {
storage: impl Storage,
contracts_trie_root_hash: HashOutput,
classes_trie_root_hash: HashOutput,
tree_heights: TreeHeight,
current_contracts_trie_leaves: &HashMap<ContractAddress, ContractState>,
state_diff: &StateDiff,
) -> ForestResult<Self>
Expand All @@ -44,32 +42,25 @@ impl<T: OriginalSkeletonTree> OriginalSkeletonForest for OriginalSkeletonForestI
storage: impl Storage,
contracts_trie_root_hash: HashOutput,
classes_trie_root_hash: HashOutput,
tree_heights: TreeHeight,
current_contracts_trie_leaves: &HashMap<ContractAddress, ContractState>,
state_diff: &StateDiff,
) -> ForestResult<Self>
where
Self: std::marker::Sized,
{
let accessed_addresses = state_diff.accessed_addresses();
let global_state_tree = Self::create_contracts_trie(
&accessed_addresses,
contracts_trie_root_hash,
&storage,
tree_heights,
)?;
let global_state_tree =
Self::create_contracts_trie(&accessed_addresses, contracts_trie_root_hash, &storage)?;
let contract_states = Self::create_storage_tries(
&accessed_addresses,
current_contracts_trie_leaves,
&state_diff.storage_updates,
&storage,
tree_heights,
)?;
let classes_tree = Self::create_classes_trie(
&state_diff.class_hash_to_compiled_class_hash,
classes_trie_root_hash,
&storage,
tree_heights,
)?;

Ok(OriginalSkeletonForestImpl::new(
Expand Down Expand Up @@ -97,11 +88,10 @@ impl<T: OriginalSkeletonTree> OriginalSkeletonForestImpl<T> {
accessed_addresses: &HashSet<&ContractAddress>,
contracts_trie_root_hash: HashOutput,
storage: &impl Storage,
tree_height: TreeHeight,
) -> ForestResult<T> {
let mut sorted_leaf_indices: Vec<NodeIndex> = accessed_addresses
.iter()
.map(|address| NodeIndex::from_contract_address(address, &tree_height))
.map(|address| NodeIndex::from_contract_address(address))
.collect();
sorted_leaf_indices.sort();
Ok(T::create(
Expand All @@ -119,15 +109,14 @@ impl<T: OriginalSkeletonTree> OriginalSkeletonForestImpl<T> {
HashMap<StarknetStorageKey, StarknetStorageValue>,
>,
storage: &impl Storage,
tree_height: TreeHeight,
) -> ForestResult<HashMap<ContractAddress, T>> {
let mut storage_tries = HashMap::new();
for address in accessed_addresses {
let mut sorted_leaf_indices: Vec<NodeIndex> = storage_updates
.get(address)
.unwrap_or(&HashMap::new())
.keys()
.map(|key| NodeIndex::from_starknet_storage_key(key, &tree_height))
.map(NodeIndex::from_starknet_storage_key)
.collect();
sorted_leaf_indices.sort();
let contract_state = current_contracts_trie_leaves
Expand All @@ -147,11 +136,10 @@ impl<T: OriginalSkeletonTree> OriginalSkeletonForestImpl<T> {
class_hash_to_compiled_class_hash: &HashMap<ClassHash, CompiledClassHash>,
classes_trie_root_hash: HashOutput,
storage: &impl Storage,
tree_height: TreeHeight,
) -> ForestResult<T> {
let mut sorted_leaf_indices: Vec<NodeIndex> = class_hash_to_compiled_class_hash
.keys()
.map(|class_hash| NodeIndex::from_class_hash(class_hash, &tree_height))
.map(NodeIndex::from_class_hash)
.collect();
sorted_leaf_indices.sort();
Ok(T::create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ fn test_create_original_skeleton_forest(
MapStorage::from(input.storage),
input.contracts_trie_root_hash,
input.classes_trie_root_hash,
TreeHeight::MAX,
&input.current_contracts_trie_leaves,
&input.state_diff,
)
Expand Down
22 changes: 8 additions & 14 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,20 @@ impl NodeIndex {
}
}

pub(crate) fn from_starknet_storage_key(
key: &StarknetStorageKey,
tree_height: &TreeHeight,
) -> Self {
Self::from_leaf_felt(&key.0, tree_height)
pub(crate) fn from_starknet_storage_key(key: &StarknetStorageKey) -> Self {
Self::from_leaf_felt(&key.0)
}

pub(crate) fn from_contract_address(
address: &ContractAddress,
tree_height: &TreeHeight,
) -> Self {
Self::from_leaf_felt(&address.0, tree_height)
pub(crate) fn from_contract_address(address: &ContractAddress) -> Self {
Self::from_leaf_felt(&address.0)
}

pub(crate) fn from_class_hash(class_hash: &ClassHash, tree_height: &TreeHeight) -> Self {
Self::from_leaf_felt(&class_hash.0, tree_height)
pub(crate) fn from_class_hash(class_hash: &ClassHash) -> Self {
Self::from_leaf_felt(&class_hash.0)
}

fn from_leaf_felt(felt: &Felt, tree_height: &TreeHeight) -> Self {
Self(U256::from(1_u8) << tree_height.0) + Self::from_felt_value(felt)
fn from_leaf_felt(felt: &Felt) -> Self {
Self::FIRST_LEAF + Self::from_felt_value(felt)
}

fn from_felt_value(felt: &Felt) -> Self {
Expand Down
25 changes: 5 additions & 20 deletions crates/committer/src/patricia_merkle_tree/types_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::felt::Felt;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};
use crate::patricia_merkle_tree::test_utils::{get_random_u256, random};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::types::TreeHeight;
use rand::rngs::ThreadRng;

use ethnum::U256;
Expand Down Expand Up @@ -33,31 +32,17 @@ fn test_compute_bottom_index(
}

#[rstest]
#[case(0, 127, 2_u128.pow(127))]
#[case(15, 118, 2_u128.pow(118) | 15)]
#[case(0xDEADBEEF, 60, 2_u128.pow(60) | 0xDEADBEEF)]
fn test_cast_to_node_index(
#[case] leaf_index: u128,
#[case] tree_height: u8,
#[case] expected_node_index: u128,
#[values(0, 15, 0xDEADBEEF)] leaf_index: u128,
#[values(true, false)] from_contract_address: bool,
) {
assert!(
leaf_index < 2_u128.pow(tree_height.into()),
"Invalid test arguments. The node index must be smaller than the number of nodes."
);
let expected_node_index = NodeIndex::FIRST_LEAF + leaf_index.into();
let actual = if from_contract_address {
NodeIndex::from_contract_address(
&ContractAddress(Felt::from(leaf_index)),
&TreeHeight::new(tree_height),
)
NodeIndex::from_contract_address(&ContractAddress(Felt::from(leaf_index)))
} else {
NodeIndex::from_starknet_storage_key(
&StarknetStorageKey(Felt::from(leaf_index)),
&TreeHeight::new(tree_height),
)
NodeIndex::from_starknet_storage_key(&StarknetStorageKey(Felt::from(leaf_index)))
};
assert_eq!(actual, expected_node_index.into());
assert_eq!(actual, expected_node_index);
}

#[rstest]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::patricia_merkle_tree::node_data::leaf::{
};
use crate::patricia_merkle_tree::original_skeleton_tree::skeleton_forest::OriginalSkeletonForestImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTree;
use crate::patricia_merkle_tree::types::{NodeIndex, TreeHeight};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;

pub(crate) struct UpdatedSkeletonForestImpl<T: UpdatedSkeletonTree> {
Expand All @@ -26,7 +26,6 @@ pub(crate) trait UpdatedSkeletonForest<T: OriginalSkeletonTree> {
current_contract_state_leaves: &HashMap<ContractAddress, ContractState>,
address_to_class_hash: &HashMap<ContractAddress, ClassHash>,
address_to_nonce: &HashMap<ContractAddress, Nonce>,
tree_heights: TreeHeight,
) -> ForestResult<Self>
where
Self: std::marker::Sized;
Expand All @@ -42,7 +41,6 @@ impl<T: OriginalSkeletonTree, U: UpdatedSkeletonTree> UpdatedSkeletonForest<T>
current_contracts_trie_leaves: &HashMap<ContractAddress, ContractState>,
address_to_class_hash: &HashMap<ContractAddress, ClassHash>,
address_to_nonce: &HashMap<ContractAddress, Nonce>,
tree_heights: TreeHeight,
) -> ForestResult<Self>
where
Self: std::marker::Sized,
Expand Down Expand Up @@ -78,10 +76,7 @@ impl<T: OriginalSkeletonTree, U: UpdatedSkeletonTree> UpdatedSkeletonForest<T>
current_leaf,
storage_trie_becomes_empty,
);
contracts_trie_leaves.insert(
NodeIndex::from_contract_address(address, &tree_heights),
skeleton_leaf,
);
contracts_trie_leaves.insert(NodeIndex::from_contract_address(address), skeleton_leaf);
}

// Contracts trie.
Expand Down
Loading