Skip to content

Commit

Permalink
chore: rename create to create_with_existing_leaves and remove Arcs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Aug 15, 2024
1 parent 64ce427 commit 4d4094c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
15 changes: 7 additions & 8 deletions crates/starknet_committer/src/forest/filled_forest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;

use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
Expand Down Expand Up @@ -61,9 +60,9 @@ impl FilledForest {
address_to_class_hash: &HashMap<ContractAddress, ClassHash>,
address_to_nonce: &HashMap<ContractAddress, Nonce>,
) -> ForestResult<Self> {
let classes_trie_task = tokio::spawn(ClassesTrie::create::<TH>(
Arc::new(updated_forest.classes_trie),
Arc::new(classes_updates),
let classes_trie_task = tokio::spawn(ClassesTrie::create_with_existing_leaves::<TH>(
updated_forest.classes_trie,
classes_updates,
));
let mut contracts_trie_modifications = HashMap::new();
let mut filled_storage_tries = HashMap::new();
Expand Down Expand Up @@ -95,9 +94,9 @@ impl FilledForest {
filled_storage_tries.insert(address, filled_storage_trie);
}

let contracts_trie_task = tokio::spawn(ContractsTrie::create::<TH>(
Arc::new(updated_forest.contracts_trie),
Arc::new(contracts_trie_modifications),
let contracts_trie_task = tokio::spawn(ContractsTrie::create_with_existing_leaves::<TH>(
updated_forest.contracts_trie,
contracts_trie_modifications,
));

let contracts_trie = contracts_trie_task.await?.map_err(ForestError::ContractsTrie)?;
Expand All @@ -114,7 +113,7 @@ impl FilledForest {
inner_updates: LeafModifications<StarknetStorageValue>,
) -> ForestResult<(ContractAddress, ContractState, StorageTrie)> {
let filled_storage_trie =
StorageTrie::create::<TH>(Arc::new(updated_storage_trie), Arc::new(inner_updates))
StorageTrie::create_with_existing_leaves::<TH>(updated_storage_trie, inner_updates)
.await
.map_err(ForestError::StorageTrie)?;
let new_root_hash = filled_storage_trie.get_root_hash();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
)
.expect("Failed to create the updated skeleton tree");

FilledTreeImpl::<L>::create::<TH>(updated_skeleton.into(), leaf_modifications.into())
FilledTreeImpl::<L>::create_with_existing_leaves::<TH>(updated_skeleton, leaf_modifications)
.await
.expect("Failed to create the filled tree")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub(crate) type FilledTreeResult<T> = Result<T, FilledTreeError>;
/// data and hashes.
pub trait FilledTree<L: Leaf>: Sized {
/// Computes and returns the filled tree.
fn create<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: Arc<impl UpdatedSkeletonTree<'a> + 'static>,
leaf_modifications: Arc<LeafModifications<L>>,
fn create_with_existing_leaves<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: impl UpdatedSkeletonTree<'a> + 'static,
leaf_modifications: LeafModifications<L>,
) -> impl Future<Output = FilledTreeResult<Self>> + Send;

/// Serializes the current state of the tree into a hashmap,
Expand All @@ -48,7 +48,7 @@ pub struct FilledTreeImpl<L: Leaf> {

impl<L: Leaf + 'static> FilledTreeImpl<L> {
fn initialize_with_placeholders<'a>(
updated_skeleton: &Arc<impl UpdatedSkeletonTree<'a>>,
updated_skeleton: &impl UpdatedSkeletonTree<'a>,
) -> HashMap<NodeIndex, Mutex<Option<FilledNode<L>>>> {
let mut filled_tree_map = HashMap::new();
for (index, node) in updated_skeleton.get_nodes() {
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
}

fn create_unmodified<'a>(
updated_skeleton: &Arc<impl UpdatedSkeletonTree<'a>>,
updated_skeleton: &impl UpdatedSkeletonTree<'a>,
) -> Result<Self, FilledTreeError> {
let root_node = updated_skeleton.get_node(NodeIndex::ROOT)?;
let UpdatedSkeletonNode::UnmodifiedSubTree(root_hash) = root_node else {
Expand All @@ -205,9 +205,9 @@ impl<L: Leaf + 'static> FilledTreeImpl<L> {
}

impl<L: Leaf + 'static> FilledTree<L> for FilledTreeImpl<L> {
async fn create<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: Arc<impl UpdatedSkeletonTree<'a> + 'static>,
leaf_modifications: Arc<LeafModifications<L>>,
async fn create_with_existing_leaves<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: impl UpdatedSkeletonTree<'a> + 'static,
leaf_modifications: LeafModifications<L>,
) -> Result<Self, FilledTreeError> {
// Compute the filled tree in two steps:
// 1. Create a map containing the tree structure without hash values.
Expand All @@ -222,9 +222,9 @@ impl<L: Leaf + 'static> FilledTree<L> for FilledTreeImpl<L> {

let filled_tree_map = Arc::new(Self::initialize_with_placeholders(&updated_skeleton));
let root_hash = Self::compute_filled_tree_rec::<TH>(
updated_skeleton,
Arc::new(updated_skeleton),
NodeIndex::ROOT,
leaf_modifications,
Arc::new(leaf_modifications),
Arc::clone(&filled_tree_map),
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;

use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
Expand Down Expand Up @@ -38,9 +37,9 @@ async fn test_filled_tree_sanity() {
skeleton_tree.insert(new_leaf_index, UpdatedSkeletonNode::Leaf);
let modifications = HashMap::from([(new_leaf_index, new_filled_leaf)]);
let updated_skeleton_tree = UpdatedSkeletonTreeImpl { skeleton_tree };
let root_hash = FilledTreeImpl::create::<TestTreeHashFunction>(
Arc::new(updated_skeleton_tree),
Arc::new(modifications),
let root_hash = FilledTreeImpl::create_with_existing_leaves::<TestTreeHashFunction>(
updated_skeleton_tree,
modifications,
)
.await
.unwrap()
Expand Down Expand Up @@ -92,9 +91,9 @@ async fn test_small_filled_tree() {
.collect();

// Compute the hash values.
let filled_tree = FilledTreeImpl::create::<TestTreeHashFunction>(
Arc::new(updated_skeleton_tree),
Arc::new(modifications),
let filled_tree = FilledTreeImpl::create_with_existing_leaves::<TestTreeHashFunction>(
updated_skeleton_tree,
modifications,
)
.await
.unwrap();
Expand Down Expand Up @@ -155,9 +154,9 @@ async fn test_small_tree_with_unmodified_nodes() {
)]);

// Compute the hash values.
let filled_tree = FilledTreeImpl::create::<TestTreeHashFunction>(
Arc::new(updated_skeleton_tree),
Arc::new(modifications),
let filled_tree = FilledTreeImpl::create_with_existing_leaves::<TestTreeHashFunction>(
updated_skeleton_tree,
modifications,
)
.await
.unwrap();
Expand Down Expand Up @@ -204,9 +203,9 @@ async fn test_delete_leaf_from_empty_tree() {

let leaf_modifications = HashMap::from([(NodeIndex::FIRST_LEAF, MockLeaf(Felt::ZERO))]);
// Compute the filled tree.
let filled_tree = FilledTreeImpl::create::<TestTreeHashFunction>(
updated_skeleton_tree.into(),
leaf_modifications.into(),
let filled_tree = FilledTreeImpl::create_with_existing_leaves::<TestTreeHashFunction>(
updated_skeleton_tree,
leaf_modifications,
)
.await
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::Arc;

use ethnum::{uint, U256};
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -507,7 +506,7 @@ async fn test_update_non_modified_storage_tree(#[case] root_hash: HashOutput) {
.unwrap();
let updated =
UpdatedSkeletonTreeImpl::create(&mut original_skeleton_tree, &HashMap::new()).unwrap();
let filled = MockTrie::create::<TestTreeHashFunction>(Arc::new(updated), Arc::new(empty_map))
let filled = MockTrie::create_with_existing_leaves::<TestTreeHashFunction>(updated, empty_map)
.await
.unwrap();
assert_eq!(root_hash, filled.get_root_hash());
Expand Down

0 comments on commit 4d4094c

Please sign in to comment.