Skip to content

build: defining skeletons forest input and introducing updated skeleton forest #110

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

Merged
merged 1 commit into from
May 15, 2024
Merged
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
31 changes: 31 additions & 0 deletions crates/committer/src/block_committer/commit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
use crate::block_committer::errors::BlockCommitmentError;
use crate::block_committer::input::{Input, StateDiff};
use crate::patricia_merkle_tree::node_data::leaf::UpdatedSkeletonLeafDataImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::skeleton_forest::OriginalSkeletonForest;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;
use crate::storage::map_storage::MapStorage;

#[allow(dead_code)]
type BlockCommitmentResult<T> = Result<T, BlockCommitmentError>;
#[allow(dead_code)]
pub(crate) fn commit_block(input: Input) -> BlockCommitmentResult<()> {
let original_forest = OriginalSkeletonForest::<
UpdatedSkeletonLeafDataImpl,
OriginalSkeletonTreeImpl<UpdatedSkeletonLeafDataImpl>,
>::create_original_skeleton_forest::<MapStorage>(
MapStorage::from(input.storage),
input.global_tree_root_hash,
input.classes_tree_root_hash,
input.tree_heights,
&input.current_contract_state_leaves,
&input.state_diff,
)?;
let _updated_forest = original_forest
.compute_updated_skeleton_forest::<UpdatedSkeletonTreeImpl<UpdatedSkeletonLeafDataImpl>>(
StateDiff::actual_classes_updates(
&input.state_diff.class_hash_to_compiled_class_hash,
input.tree_heights,
),
&input.state_diff.accessed_addresses(),
&input.state_diff.actual_storage_updates(input.tree_heights),
)?;

todo!()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ContractState {
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LeafDataImpl {
StorageValue(Felt),
// TODO(Nimrod, 30/5/2024): Change the inner type to CompiledClassHash.
CompiledClassHash(ClassHash),
ContractState(ContractState),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::block_committer::input::ContractAddress;
use crate::block_committer::input::Input;
use crate::block_committer::input::StarknetStorageKey;
use crate::block_committer::input::StarknetStorageValue;
use crate::block_committer::input::StateDiff;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::ClassHash;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
Expand All @@ -12,6 +12,8 @@ use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonT
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::types::TreeHeight;
use crate::patricia_merkle_tree::updated_skeleton_tree::skeleton_forest::UpdatedSkeletonForest;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;
use crate::storage::storage_trait::Storage;
use core::marker::PhantomData;
use std::collections::HashMap;
Expand Down Expand Up @@ -51,28 +53,32 @@ impl<L: LeafData + std::clone::Clone, T: OriginalSkeletonTree<L>> OriginalSkelet

#[allow(dead_code)]
pub(crate) fn create_original_skeleton_forest<S: Storage>(
input: Input,
storage: S,
global_tree_root_hash: HashOutput,
classes_tree_root_hash: HashOutput,
tree_heights: TreeHeight,
current_contract_state_leaves: &HashMap<ContractAddress, ContractState>,
state_diff: &StateDiff,
) -> OriginalSkeletonTreeResult<OriginalSkeletonForest<L, T>> {
let storage = S::from(input.storage);
let accessed_addresses = input.state_diff.accessed_addresses();
let accessed_addresses = state_diff.accessed_addresses();
let global_state_tree = Self::create_global_state_tree(
&accessed_addresses,
input.global_tree_root_hash,
global_tree_root_hash,
&storage,
input.tree_heights,
tree_heights,
)?;
let contract_states = Self::create_lower_trees_skeleton(
accessed_addresses,
&input.current_contract_state_leaves,
&input.state_diff.storage_updates,
&accessed_addresses,
current_contract_state_leaves,
&state_diff.storage_updates,
&storage,
input.tree_heights,
tree_heights,
)?;
let classes_tree = Self::create_classes_tree(
input.state_diff.class_hash_to_compiled_class_hash,
input.classes_tree_root_hash,
&state_diff.class_hash_to_compiled_class_hash,
classes_tree_root_hash,
&storage,
input.tree_heights,
tree_heights,
)?;

Ok(OriginalSkeletonForest::new(
Expand Down Expand Up @@ -102,7 +108,7 @@ impl<L: LeafData + std::clone::Clone, T: OriginalSkeletonTree<L>> OriginalSkelet
}

fn create_lower_trees_skeleton<S: Storage>(
accessed_addresses: HashSet<&ContractAddress>,
accessed_addresses: &HashSet<&ContractAddress>,
current_contract_state_leaves: &HashMap<ContractAddress, ContractState>,
storage_updates: &HashMap<
ContractAddress,
Expand All @@ -122,20 +128,20 @@ impl<L: LeafData + std::clone::Clone, T: OriginalSkeletonTree<L>> OriginalSkelet
sorted_leaf_indices.sort();
let contract_state = current_contract_state_leaves
.get(address)
.ok_or_else(|| OriginalSkeletonTreeError::LowerTreeCommitmentError(*address))?;
.ok_or_else(|| OriginalSkeletonTreeError::LowerTreeCommitmentError(**address))?;
let original_skeleton = T::create_tree(
storage,
&sorted_leaf_indices,
contract_state.storage_root_hash,
tree_height,
)?;
contract_states.insert(*address, original_skeleton);
contract_states.insert(**address, original_skeleton);
}
Ok(contract_states)
}

fn create_classes_tree<S: Storage>(
class_hash_to_compiled_class_hash: HashMap<ClassHash, CompiledClassHash>,
class_hash_to_compiled_class_hash: &HashMap<ClassHash, CompiledClassHash>,
classes_tree_root_hash: HashOutput,
storage: &S,
tree_height: TreeHeight,
Expand All @@ -152,4 +158,14 @@ impl<L: LeafData + std::clone::Clone, T: OriginalSkeletonTree<L>> OriginalSkelet
tree_height,
)
}

#[allow(dead_code)]
pub(crate) fn compute_updated_skeleton_forest<U: UpdatedSkeletonTree<L>>(
&self,
_class_hash_to_compiled_class_hash: HashMap<NodeIndex, L>,
_contracts_to_commit: &HashSet<&ContractAddress>,
_storage_updates: &HashMap<ContractAddress, HashMap<NodeIndex, L>>,
) -> OriginalSkeletonTreeResult<UpdatedSkeletonForest<L, U>> {
todo!()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,15 @@ fn test_create_original_skeleton_forest(
let actual_forest: OriginalSkeletonForest<
LeafDataImpl,
OriginalSkeletonTreeImpl<LeafDataImpl>,
> = OriginalSkeletonForest::create_original_skeleton_forest::<MapStorage>(input).unwrap();
> = OriginalSkeletonForest::create_original_skeleton_forest::<MapStorage>(
MapStorage::from(input.storage),
input.global_tree_root_hash,
input.classes_tree_root_hash,
input.tree_heights,
&input.current_contract_state_leaves,
&input.state_diff,
)
.unwrap();

assert_eq!(
actual_forest.global_state_tree,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod errors;
pub mod hash_function;
pub mod node;
pub mod skeleton_forest;
pub mod tree;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::collections::HashMap;
use std::marker::PhantomData;

use crate::block_committer::input::ContractAddress;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;

#[allow(dead_code)]
pub(crate) struct UpdatedSkeletonForest<L: LeafData + std::clone::Clone, T: UpdatedSkeletonTree<L>>
{
#[allow(dead_code)]
classes_tree: T,
#[allow(dead_code)]
global_state_tree: T,
#[allow(dead_code)]
contract_states: HashMap<ContractAddress, T>,
leaf_data: PhantomData<L>,
}
Loading