From e423f494c53565e4357d5dd93af782f2db5eb420 Mon Sep 17 00:00:00 2001 From: amos rothberg Date: Tue, 9 Apr 2024 14:16:05 +0300 Subject: [PATCH] build: add storage trait and FilledTree::serialize --- crates/committer/src/lib.rs | 1 + .../current_skeleton_tree.rs | 19 ++++++++++++---- .../src/patricia_merkle_tree/errors.rs | 8 ++++++- .../src/patricia_merkle_tree/filled_tree.rs | 13 ++++++----- .../src/patricia_merkle_tree/types.rs | 3 +++ .../updated_skeleton_tree.rs | 4 ++-- crates/committer/src/storage.rs | 2 ++ crates/committer/src/storage/errors.rs | 2 ++ crates/committer/src/storage/storage_trait.rs | 22 +++++++++++++++++++ 9 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 crates/committer/src/storage.rs create mode 100644 crates/committer/src/storage/errors.rs create mode 100644 crates/committer/src/storage/storage_trait.rs diff --git a/crates/committer/src/lib.rs b/crates/committer/src/lib.rs index c7796133..10bc3e47 100644 --- a/crates/committer/src/lib.rs +++ b/crates/committer/src/lib.rs @@ -1,3 +1,4 @@ pub mod hash; pub mod patricia_merkle_tree; +pub mod storage; pub mod types; diff --git a/crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs b/crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs index 470b7c01..aac8d292 100644 --- a/crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs +++ b/crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs @@ -1,9 +1,13 @@ use std::collections::HashMap; -use crate::hash::types::HashFunction; -use crate::patricia_merkle_tree::errors::SkeletonTreeError; -use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex, TreeHashFunction}; +use crate::hash::types::{HashFunction, HashOutput}; +use crate::patricia_merkle_tree::errors::CurrentSkeletonTreeError; +use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex, TreeHashFunction, TreeHeight}; use crate::patricia_merkle_tree::updated_skeleton_tree::UpdatedSkeletonTree; +use crate::storage::storage_trait::Storage; + +#[allow(dead_code)] +pub(crate) type CurrentSkeletonTreeResult = Result; /// Consider a Patricia-Merkle Tree which should be updated with new leaves. /// This trait represents the structure of the subtree which will be modified in the @@ -11,9 +15,16 @@ use crate::patricia_merkle_tree::updated_skeleton_tree::UpdatedSkeletonTree; /// nodes on the Merkle paths from the updated leaves to the root. pub(crate) trait CurrentSkeletonTree> { + fn compute_current_skeleton_tree( + storage: impl Storage, + leaf_indices: &[NodeIndex], + root_hash: HashOutput, + tree_height: TreeHeight, + ) -> CurrentSkeletonTreeResult>; + /// Computes and returns updated skeleton tree. fn compute_updated_skeleton_tree( &self, index_to_updated_leaf: HashMap, - ) -> Result, SkeletonTreeError>; + ) -> CurrentSkeletonTreeResult>; } diff --git a/crates/committer/src/patricia_merkle_tree/errors.rs b/crates/committer/src/patricia_merkle_tree/errors.rs index 7b1f2ec6..f276d5a3 100644 --- a/crates/committer/src/patricia_merkle_tree/errors.rs +++ b/crates/committer/src/patricia_merkle_tree/errors.rs @@ -1,3 +1,9 @@ // TODO(Amos, 01/04/2024): Add error types. #[derive(Debug)] -pub enum SkeletonTreeError {} +pub(crate) enum CurrentSkeletonTreeError {} + +#[derive(Debug)] +pub(crate) enum UpdatedSkeletonTreeError {} + +#[derive(Debug)] +pub(crate) enum FilledTreeError {} diff --git a/crates/committer/src/patricia_merkle_tree/filled_tree.rs b/crates/committer/src/patricia_merkle_tree/filled_tree.rs index 829043eb..612b5ddd 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree.rs @@ -1,14 +1,20 @@ use std::collections::HashMap; +use std::collections::HashSet; use std::sync::Mutex; +use crate::patricia_merkle_tree::errors::FilledTreeError; use crate::patricia_merkle_tree::filled_node::FilledNode; -use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex}; +use crate::patricia_merkle_tree::types::LeafDataTrait; +use crate::patricia_merkle_tree::types::NodeIndex; +use crate::storage::storage_trait::Storage; /// Consider a Patricia-Merkle Tree which has been updated with new leaves. /// FilledTree consists of all nodes which were modified in the update, including their updated /// data and hashes. pub(crate) trait FilledTree { - fn get_all_nodes(&self) -> &HashMap>>; + /// Serializes the tree into storage. Returns hash set of keys of the serialized nodes, + /// if successful. + fn serialize(&self, storage: impl Storage) -> Result, FilledTreeError>; } pub(crate) struct FilledTreeImpl { @@ -20,9 +26,6 @@ impl FilledTreeImpl { pub(crate) fn new(tree_map: HashMap>>) -> Self { Self { tree_map } } -} - -impl FilledTree for FilledTreeImpl { fn get_all_nodes(&self) -> &HashMap>> { &self.tree_map } diff --git a/crates/committer/src/patricia_merkle_tree/types.rs b/crates/committer/src/patricia_merkle_tree/types.rs index f37d9a9e..f18d4ba7 100644 --- a/crates/committer/src/patricia_merkle_tree/types.rs +++ b/crates/committer/src/patricia_merkle_tree/types.rs @@ -17,6 +17,9 @@ pub(crate) struct EdgePath(pub Felt); #[allow(dead_code)] pub(crate) struct EdgePathLength(pub u8); +#[allow(dead_code)] +pub(crate) struct TreeHeight(pub u8); + #[allow(dead_code)] pub(crate) struct PathToBottom { pub path: EdgePath, diff --git a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs index 110794f0..e556db0f 100644 --- a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs +++ b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs @@ -1,5 +1,5 @@ use crate::hash::types::HashFunction; -use crate::patricia_merkle_tree::errors::SkeletonTreeError; +use crate::patricia_merkle_tree::errors::UpdatedSkeletonTreeError; use crate::patricia_merkle_tree::filled_tree::FilledTree; use crate::patricia_merkle_tree::types::{LeafDataTrait, TreeHashFunction}; @@ -10,5 +10,5 @@ use crate::patricia_merkle_tree::types::{LeafDataTrait, TreeHashFunction}; pub(crate) trait UpdatedSkeletonTree> { /// Computes and returns the filled tree. - fn compute_filled_tree(&self) -> Result, SkeletonTreeError>; + fn compute_filled_tree(&self) -> Result, UpdatedSkeletonTreeError>; } diff --git a/crates/committer/src/storage.rs b/crates/committer/src/storage.rs new file mode 100644 index 00000000..496f41c2 --- /dev/null +++ b/crates/committer/src/storage.rs @@ -0,0 +1,2 @@ +pub(crate) mod errors; +pub(crate) mod storage_trait; diff --git a/crates/committer/src/storage/errors.rs b/crates/committer/src/storage/errors.rs new file mode 100644 index 00000000..8046fb88 --- /dev/null +++ b/crates/committer/src/storage/errors.rs @@ -0,0 +1,2 @@ +#[derive(Debug)] +pub(crate) enum StorageError {} diff --git a/crates/committer/src/storage/storage_trait.rs b/crates/committer/src/storage/storage_trait.rs new file mode 100644 index 00000000..788021bb --- /dev/null +++ b/crates/committer/src/storage/storage_trait.rs @@ -0,0 +1,22 @@ +use crate::storage::errors::StorageError; +use std::collections::HashMap; + +#[allow(dead_code)] +pub(crate) struct StorageKey(Vec); + +#[allow(dead_code)] +pub(crate) struct StorageValue(Vec); + +pub(crate) trait Storage { + /// Returns value from storage, if it exists. + fn get(&self, key: &StorageKey) -> Option; + /// Sets value in storage. + fn set(&mut self, key: &StorageKey, value: &StorageValue); + /// Returns values from storage in same order of given keys. If key does not exist, + /// value is None. + fn mget(&self, keys: &[StorageKey]) -> [Option]; + /// Sets values in storage. + fn mset(&mut self, key_to_value: &HashMap); + /// Deletes value from storage. Returns error if key does not exist. + fn delete(&mut self, key: &StorageKey) -> Result<(), StorageError>; +}