Skip to content
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

build: add storage trait and FilledTree::serialize #26

Merged
merged 1 commit into from
Apr 10, 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
1 change: 1 addition & 0 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod hash;
pub mod patricia_merkle_tree;
pub mod storage;
pub mod types;
19 changes: 15 additions & 4 deletions crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
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<T> = Result<T, CurrentSkeletonTreeError>;

/// 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
/// 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 CurrentSkeletonTree<L: LeafDataTrait, H: HashFunction, TH: TreeHashFunction<L, H>>
{
fn compute_current_skeleton_tree(
storage: impl Storage,
leaf_indices: &[NodeIndex],
root_hash: HashOutput,
tree_height: TreeHeight,
) -> CurrentSkeletonTreeResult<Box<Self>>;

/// Computes and returns updated skeleton tree.
fn compute_updated_skeleton_tree(
&self,
index_to_updated_leaf: HashMap<NodeIndex, L>,
) -> Result<impl UpdatedSkeletonTree<L, H, TH>, SkeletonTreeError>;
) -> CurrentSkeletonTreeResult<impl UpdatedSkeletonTree<L, H, TH>>;
}
8 changes: 7 additions & 1 deletion crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
@@ -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 {}
13 changes: 8 additions & 5 deletions crates/committer/src/patricia_merkle_tree/filled_tree.rs
Original file line number Diff line number Diff line change
@@ -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<L: LeafDataTrait> {
fn get_all_nodes(&self) -> &HashMap<NodeIndex, Mutex<FilledNode<L>>>;
/// Serializes the tree into storage. Returns hash set of keys of the serialized nodes,
/// if successful.
fn serialize(&self, storage: impl Storage) -> Result<HashSet<&[u8]>, FilledTreeError>;
}

pub(crate) struct FilledTreeImpl<L: LeafDataTrait> {
Expand All @@ -20,9 +26,6 @@ impl<L: LeafDataTrait> FilledTreeImpl<L> {
pub(crate) fn new(tree_map: HashMap<NodeIndex, Mutex<FilledNode<L>>>) -> Self {
Self { tree_map }
}
}

impl<L: LeafDataTrait> FilledTree<L> for FilledTreeImpl<L> {
fn get_all_nodes(&self) -> &HashMap<NodeIndex, Mutex<FilledNode<L>>> {
&self.tree_map
}
Expand Down
3 changes: 3 additions & 0 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -10,5 +10,5 @@ use crate::patricia_merkle_tree::types::{LeafDataTrait, TreeHashFunction};
pub(crate) trait UpdatedSkeletonTree<L: LeafDataTrait, H: HashFunction, TH: TreeHashFunction<L, H>>
{
/// Computes and returns the filled tree.
fn compute_filled_tree(&self) -> Result<impl FilledTree<L>, SkeletonTreeError>;
fn compute_filled_tree(&self) -> Result<impl FilledTree<L>, UpdatedSkeletonTreeError>;
}
2 changes: 2 additions & 0 deletions crates/committer/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) mod errors;
pub(crate) mod storage_trait;
2 changes: 2 additions & 0 deletions crates/committer/src/storage/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug)]
pub(crate) enum StorageError {}
22 changes: 22 additions & 0 deletions crates/committer/src/storage/storage_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::storage::errors::StorageError;
use std::collections::HashMap;

#[allow(dead_code)]
pub(crate) struct StorageKey(Vec<u8>);

#[allow(dead_code)]
pub(crate) struct StorageValue(Vec<u8>);

pub(crate) trait Storage {
/// Returns value from storage, if it exists.
fn get(&self, key: &StorageKey) -> Option<StorageValue>;
/// 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<StorageValue>];
/// Sets values in storage.
fn mset(&mut self, key_to_value: &HashMap<StorageKey, StorageValue>);
/// Deletes value from storage. Returns error if key does not exist.
fn delete(&mut self, key: &StorageKey) -> Result<(), StorageError>;
}
Loading