Skip to content

Commit

Permalink
fix: split SkeletonNode into CurrentSkeletonNode and UpdatedSkeletonNode
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware committed Apr 8, 2024
1 parent df0c31f commit 272a0af
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 52 deletions.
6 changes: 4 additions & 2 deletions crates/committer/src/patricia_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod current_skeleton_node;
pub mod current_skeleton_tree;
pub mod errors;
pub mod filled_node;
pub mod filled_tree;
pub mod skeleton_node;
pub mod skeleton_tree;
pub mod types;
pub mod updated_skeleton_node;
pub mod updated_skeleton_tree;
15 changes: 15 additions & 0 deletions crates/committer/src/patricia_merkle_tree/current_skeleton_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::hash::types::HashOutput;
use crate::patricia_merkle_tree::types::{EdgeData, LeafDataTrait, PathToBottom};

#[allow(dead_code)]
/// A node in the structure of a Patricia-Merkle tree, before the update.
pub(crate) enum CurrentSkeletonNode<L: LeafDataTrait> {
Binary,
Edge { path_to_bottom: PathToBottom },
// Unmodified leaf / binary nodes on the merkle paths of modified leaves.
LeafOrBinarySibling(HashOutput),
// Unmodified edge nodes on the merkle paths of modified leaves.
EdgeSibling(EdgeData),
Leaf(L),
Empty,
}
19 changes: 19 additions & 0 deletions crates/committer/src/patricia_merkle_tree/current_skeleton_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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::patricia_merkle_tree::updated_skeleton_tree::UpdatedSkeletonTree;

/// 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>>
{
/// 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>;
}
8 changes: 1 addition & 7 deletions crates/committer/src/patricia_merkle_tree/filled_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::patricia_merkle_tree::types::{LeafDataTrait, PathToBottom};
use crate::patricia_merkle_tree::types::{EdgeData, LeafDataTrait};
use crate::{hash::types::HashOutput, types::Felt};
// TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented.
#[allow(dead_code)]
Expand Down Expand Up @@ -27,12 +27,6 @@ pub(crate) struct BinaryData {
right_hash: HashOutput,
}

#[allow(dead_code)]
pub(crate) struct EdgeData {
bottom_hash: HashOutput,
path_to_bottom: PathToBottom,
}

#[allow(dead_code)]
pub(crate) enum LeafData {
StorageValue(Felt),
Expand Down
13 changes: 0 additions & 13 deletions crates/committer/src/patricia_merkle_tree/skeleton_node.rs

This file was deleted.

30 changes: 0 additions & 30 deletions crates/committer/src/patricia_merkle_tree/skeleton_tree.rs

This file was deleted.

6 changes: 6 additions & 0 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub(crate) struct PathToBottom {
pub length: EdgePathLength,
}

#[allow(dead_code)]
pub(crate) struct EdgeData {
bottom_hash: HashOutput,
path_to_bottom: PathToBottom,
}

pub(crate) trait LeafDataTrait {
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;
Expand Down
12 changes: 12 additions & 0 deletions crates/committer/src/patricia_merkle_tree/updated_skeleton_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::hash::types::HashOutput;
use crate::patricia_merkle_tree::types::{LeafDataTrait, PathToBottom};

#[allow(dead_code)]
/// A node in the structure of a Patricia-Merkle tree, after the update.
pub(crate) enum UpdatedSkeletonNode<L: LeafDataTrait> {
Binary,
Edge { path_to_bottom: PathToBottom },
// All unmodified nodes on the merkle paths of modified leaves.
Sibling(HashOutput),
Leaf(L),
}
14 changes: 14 additions & 0 deletions crates/committer/src/patricia_merkle_tree/updated_skeleton_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::hash::types::HashFunction;
use crate::patricia_merkle_tree::errors::SkeletonTreeError;
use crate::patricia_merkle_tree::filled_tree::FilledTree;
use crate::patricia_merkle_tree::types::{LeafDataTrait, TreeHashFunction};

/// Consider a Patricia-Merkle Tree which has been updated with new leaves.
/// This trait represents the structure of the subtree which was modified in the update.
/// It also contains the hashes of the Sibling nodes on the Merkle paths from the updated leaves
/// to the root.
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>;
}

0 comments on commit 272a0af

Please sign in to comment.