Skip to content

Commit

Permalink
build: add tentative tree API
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware committed Mar 31, 2024
1 parent 40cd251 commit 45e17c0
Show file tree
Hide file tree
Showing 13 changed files with 2,060 additions and 23 deletions.
1,923 changes: 1,915 additions & 8 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ workspace = true

[dev-dependencies]
pretty_assertions.workspace = true

[dependencies]
starknet-types-core = "0.0.11"
starknet_api = "0.12.0-dev.0"
1 change: 1 addition & 0 deletions crates/committer/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
17 changes: 17 additions & 0 deletions crates/committer/src/hash/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::types::Felt;

#[allow(dead_code)]
pub(crate) struct HashInputPair(pub Felt, pub Felt);

#[allow(dead_code)]
pub(crate) struct HashOutput(pub Felt);

#[allow(dead_code)]
impl HashOutput {
pub(crate) const ZERO: HashOutput = HashOutput(Felt::ZERO);
}

pub(crate) trait HashFunction {
/// Computes the hash of given input.
async fn compute_hash(i: HashInputPair) -> HashOutput;
}
18 changes: 3 additions & 15 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
// TODO(Dori, 3/3/2024): Delete this dummy code.
pub fn dummy() -> u8 {
7
}

#[cfg(test)]
pub mod test {
use super::dummy;
use pretty_assertions::assert_eq;

#[test]
fn test_dummy() {
assert_eq!(dummy(), 7);
}
}
pub mod hash;
pub mod patricia_merkle_tree;
pub mod types;
6 changes: 6 additions & 0 deletions crates/committer/src/patricia_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod errors;
pub mod filled_node;
pub mod filled_tree;
pub mod skeleton_node;
pub mod skeleton_tree;
pub mod types;
3 changes: 3 additions & 0 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO(Amos, 01/04/2024): Add error types.
#[derive(Debug)]
pub enum SkeletonTreeError {}
40 changes: 40 additions & 0 deletions crates/committer/src/patricia_merkle_tree/filled_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use starknet_api::core::{ClassHash, Nonce};

use crate::patricia_merkle_tree::types::{Leaf, PathToBottom};
use crate::{hash::types::HashOutput, types::Felt};

#[allow(dead_code)]
pub(crate) enum FilledNode<L: Leaf> {
Binary { data: BinaryData, hash: HashOutput },
Edge { data: EdgeData<L>, hash: HashOutput },
Leaf(L),
}

#[allow(dead_code)]
pub(crate) struct BinaryData {
left_hash: HashOutput,
right_hash: HashOutput,
}

#[allow(dead_code)]
pub(crate) struct EdgeData<L: Leaf> {
bottom_value: BottomData<L>,
path_to_bottom: PathToBottom,
}

#[allow(dead_code)]
pub(crate) enum BottomData<L: Leaf> {
BottomBinaryData(BinaryData),
BottomLeafData(L),
}

#[allow(dead_code)]
pub(crate) enum LeafEnum {
StorageValue(Felt),
CompiledClassHash(Felt),
StateTreeValue {
class_hash: ClassHash,
contract_state_root_hash: Felt,
nonce: Nonce,
},
}
8 changes: 8 additions & 0 deletions crates/committer/src/patricia_merkle_tree/filled_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::iter::Map;

use crate::patricia_merkle_tree::filled_node::FilledNode;
use crate::patricia_merkle_tree::types::{Leaf, NodeIndex};

pub(crate) trait FilledTree<L: Leaf> {
fn get_all_nodes(&self) -> Map<NodeIndex, &FilledNode<L>>;
}
10 changes: 10 additions & 0 deletions crates/committer/src/patricia_merkle_tree/skeleton_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::hash::types::HashOutput;
use crate::patricia_merkle_tree::types::{Leaf, PathToBottom};

#[allow(dead_code)]
pub(crate) enum SkeletonNode<L: Leaf> {
Binary,
Edge { path_to_bottom: PathToBottom },
Sibling(HashOutput),
Leaf(L),
}
19 changes: 19 additions & 0 deletions crates/committer/src/patricia_merkle_tree/skeleton_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::iter::Map;

use crate::patricia_merkle_tree::errors::SkeletonTreeError;
use crate::patricia_merkle_tree::filled_tree::FilledTree;
use crate::patricia_merkle_tree::skeleton_node::SkeletonNode;
use crate::patricia_merkle_tree::types::{Leaf, NodeIndex, TreeHashFunction};

pub(crate) trait CurrentSkeletonTree<L: Leaf, H: TreeHashFunction<L>> {
/// Computes and returns updated skeleton tree.
fn compute_updated_skeleton_tree(
&self,
index_to_updated_leaf: Map<NodeIndex, &SkeletonNode<L>>,
) -> Result<SkeletonTreeError, impl UpdatedSkeletonTree<L, H>>;
}

pub(crate) trait UpdatedSkeletonTree<L: Leaf, H: TreeHashFunction<L>> {
/// Computes and returns the filled tree.
fn compute_filled_tree(&self) -> Result<SkeletonTreeError, impl FilledTree<L>>;
}
31 changes: 31 additions & 0 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::hash::types::HashOutput;
use crate::patricia_merkle_tree::skeleton_node::SkeletonNode;
use crate::types::Felt;

pub(crate) trait TreeHashFunction<L: Leaf> {
/// Computes the hash of given input.
async fn compute_node_hash(skeleton_node: SkeletonNode<L>) -> HashOutput;
}

// TODO(Amos, 01/05/2024): Implement types for NodeIndex, EdgePath, EdgePathLength
#[allow(dead_code)]
pub(crate) struct NodeIndex(pub Felt);

#[allow(dead_code)]
pub(crate) struct EdgePath(pub Felt);

#[allow(dead_code)]
pub(crate) struct EdgePathLength(pub Felt);

#[allow(dead_code)]
pub(crate) struct PathToBottom {
pub path: EdgePath,
pub length: EdgePathLength,
}

pub(crate) trait Leaf {
/// Returns data of leaf.
fn get_data(&self) -> Self;
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;
}
3 changes: 3 additions & 0 deletions crates/committer/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use starknet_types_core::felt::Felt as StarknetTypesFelt;

pub(crate) type Felt = StarknetTypesFelt;

0 comments on commit 45e17c0

Please sign in to comment.