Skip to content

Commit

Permalink
chore: implemented OriginalSkeletonTree trait (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware authored Apr 21, 2024
1 parent 482f825 commit 3da38c8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_node::BinaryData;
use crate::patricia_merkle_tree::filled_node::FilledNode;
use crate::patricia_merkle_tree::filled_node::NodeData;
use crate::patricia_merkle_tree::original_skeleton_tree::OriginalSkeletonTree;
use crate::patricia_merkle_tree::original_skeleton_tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::EdgeData;
use crate::patricia_merkle_tree::types::PathToBottom;
use crate::patricia_merkle_tree::types::TreeHeight;
use crate::patricia_merkle_tree::updated_skeleton_tree::UpdatedSkeletonTreeImpl;
use crate::patricia_merkle_tree::{
filled_node::LeafData, original_skeleton_node::OriginalSkeletonNode, types::NodeIndex,
};
Expand All @@ -23,9 +25,7 @@ pub mod original_skeleton_calc_test;

#[allow(dead_code)]
pub(crate) struct OriginalSkeletonTreeImpl {
nodes: HashMap<NodeIndex, OriginalSkeletonNode<LeafData>>,
// TODO(Nimrod, 30/4/2024): Remove `leaf_modifications` field.
leaf_modifications: HashMap<NodeIndex, LeafData>,
pub(crate) nodes: HashMap<NodeIndex, OriginalSkeletonNode<LeafData>>,
tree_height: TreeHeight,
}

Expand Down Expand Up @@ -199,3 +199,30 @@ impl OriginalSkeletonTreeImpl {
Ok(subtrees_roots)
}
}
impl OriginalSkeletonTree<LeafData> for OriginalSkeletonTreeImpl {
fn create_tree(
storage: &impl Storage,
sorted_leaf_indices: &[NodeIndex],
root_hash: HashOutput,
tree_height: TreeHeight,
) -> OriginalSkeletonTreeResult<Self> {
let main_subtree = SubTree {
sorted_leaf_indices,
root_index: NodeIndex::root_index(),
root_hash,
};
let mut skeleton_tree = Self {
nodes: HashMap::new(),
tree_height,
};
skeleton_tree.fetch_nodes(vec![main_subtree], storage)?;
Ok(skeleton_tree)
}

fn compute_updated_skeleton_tree(
&self,
_index_to_updated_leaf: HashMap<NodeIndex, LeafData>,
) -> OriginalSkeletonTreeResult<UpdatedSkeletonTreeImpl<LeafData>> {
todo!()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::original_skeleton_calc::LeafData;
use crate::patricia_merkle_tree::original_skeleton_node::OriginalSkeletonNode;
use crate::patricia_merkle_tree::original_skeleton_tree::OriginalSkeletonTree;
use crate::patricia_merkle_tree::types::{EdgePath, EdgePathLength, NodeIndex, PathToBottom};
use crate::storage::map_storage::MapStorage;
use crate::types::Felt;
Expand All @@ -12,7 +13,6 @@ use crate::patricia_merkle_tree::types::TreeHeight;
use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue};

use super::OriginalSkeletonTreeImpl;
use super::SubTree;

#[rstest]
// This test assumes for simplicity that hash is addition (i.e hash(a,b) = a + b).
Expand Down Expand Up @@ -238,20 +238,17 @@ fn test_fetch_nodes(
#[case] expected_nodes: HashMap<NodeIndex, OriginalSkeletonNode<LeafData>>,
#[case] tree_height: TreeHeight,
) {
let mut skeleton_tree = OriginalSkeletonTreeImpl {
nodes: HashMap::new(),
leaf_modifications,
tree_height,
};
let mut sorted_leaf_indices: Vec<NodeIndex> =
skeleton_tree.leaf_modifications.keys().copied().collect();
let mut sorted_leaf_indices: Vec<NodeIndex> = leaf_modifications.keys().copied().collect();
sorted_leaf_indices.sort();
let subtrees = vec![SubTree {
sorted_leaf_indices: &sorted_leaf_indices,
root_index: NodeIndex::root_index(),

let skeleton_tree = OriginalSkeletonTreeImpl::create_tree(
&storage,
&sorted_leaf_indices,
root_hash,
}];
assert!(skeleton_tree.fetch_nodes(subtrees, &storage).is_ok());
tree_height,
)
.unwrap();

assert_eq!(&skeleton_tree.nodes, &expected_nodes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub(crate) type OriginalSkeletonTreeResult<T> = Result<T, OriginalSkeletonTreeEr
/// update. It also contains the hashes (for edge siblings - also the edge data) of the Sibling
/// nodes on the Merkle paths from the updated leaves to the root.
pub(crate) trait OriginalSkeletonTree<L: LeafDataTrait + std::clone::Clone> {
fn compute_original_skeleton_tree(
fn create_tree(
storage: &impl Storage,
leaf_indices: [NodeIndex],
leaf_indices: &[NodeIndex],
root_hash: HashOutput,
tree_height: TreeHeight,
) -> OriginalSkeletonTreeResult<impl OriginalSkeletonTree<L>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) trait UpdatedSkeletonTree<L: LeafDataTrait + std::clone::Clone> {
) -> Result<impl FilledTree<L>, UpdatedSkeletonTreeError<L>>;
}

struct UpdatedSkeletonTreeImpl<L: LeafDataTrait + std::clone::Clone> {
pub(crate) struct UpdatedSkeletonTreeImpl<L: LeafDataTrait + std::clone::Clone> {
skeleton_tree: HashMap<NodeIndex, UpdatedSkeletonNode<L>>,
}

Expand Down

0 comments on commit 3da38c8

Please sign in to comment.