Skip to content

Commit

Permalink
chore: add node_data/leaf
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware committed Apr 24, 2024
1 parent dc7769f commit 31b0b61
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 48 deletions.
3 changes: 1 addition & 2 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use thiserror::Error;

use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::storage::errors::StorageError;
use crate::storage::storage_trait::StorageValue;

use crate::patricia_merkle_tree::filled_tree::node::FilledNode;

use super::types::LeafDataTrait;

#[allow(dead_code)]
#[derive(Debug, Error)]
pub(crate) enum OriginalSkeletonTreeError {
Expand Down
32 changes: 1 addition & 31 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::patricia_merkle_tree::filled_tree::node_serde::{
LeafCompiledClassToSerialize, SerializeNode,
};
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::types::LeafDataTrait;
use crate::patricia_merkle_tree::node_data::leaf::{LeafData, LeafDataTrait};
use crate::types::Felt;

// TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented.
Expand All @@ -27,36 +27,6 @@ pub(crate) struct FilledNode<L: LeafDataTrait> {
pub(crate) data: NodeData<L>,
}

#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum LeafData {
StorageValue(Felt),
CompiledClassHash(ClassHash),
StateTreeTuple {
class_hash: ClassHash,
contract_state_root_hash: Felt,
nonce: Nonce,
},
}

impl LeafDataTrait for LeafData {
fn is_empty(&self) -> bool {
match self {
LeafData::StorageValue(value) => *value == Felt::ZERO,
LeafData::CompiledClassHash(class_hash) => class_hash.0 == Felt::ZERO,
LeafData::StateTreeTuple {
class_hash,
contract_state_root_hash,
nonce,
} => {
nonce.0 == Felt::ZERO
&& class_hash.0 == Felt::ZERO
&& *contract_state_root_hash == Felt::ZERO
}
}
}
}

impl LeafData {
/// Serializes the leaf data into a byte vector.
/// The serialization is done as follows:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::{FilledNode, LeafData};
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::filled_tree::tree::FilledTreeResult;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, EdgePath, EdgePathLength, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::storage::storage_trait::{create_db_key, StorageKey, StorageValue};
use crate::types::Felt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::collections::HashSet;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::errors::FilledTreeError;
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex};
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::storage::storage_trait::Storage;
use crate::storage::storage_trait::StorageKey;

Expand Down
1 change: 1 addition & 0 deletions crates/committer/src/patricia_merkle_tree/node_data.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod inner_node;
pub mod leaf;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex};
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::types::Felt;

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
37 changes: 37 additions & 0 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, Nonce};
use crate::types::Felt;

pub(crate) trait LeafDataTrait {
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;
}

#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum LeafData {
StorageValue(Felt),
CompiledClassHash(ClassHash),
StateTreeTuple {
class_hash: ClassHash,
contract_state_root_hash: Felt,
nonce: Nonce,
},
}

impl LeafDataTrait for LeafData {
fn is_empty(&self) -> bool {
match self {
LeafData::StorageValue(value) => *value == Felt::ZERO,
LeafData::CompiledClassHash(class_hash) => class_hash.0 == Felt::ZERO,
LeafData::StateTreeTuple {
class_hash,
contract_state_root_hash,
nonce,
} => {
nonce.0 == Felt::ZERO
&& class_hash.0 == Felt::ZERO
&& *contract_state_root_hash == Felt::ZERO
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgeData, PathToBottom};
use crate::patricia_merkle_tree::types::LeafDataTrait;
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;

#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::filled_tree::node::LeafData;
use crate::patricia_merkle_tree::node_data::inner_node::BinaryData;
use crate::patricia_merkle_tree::node_data::inner_node::EdgeData;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTree;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::TreeHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::collections::HashMap;

use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::errors::OriginalSkeletonTreeError;
use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex, TreeHeight};
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;
use crate::patricia_merkle_tree::types::{NodeIndex, TreeHeight};
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTree;
use crate::storage::storage_trait::Storage;

Expand Down
7 changes: 1 addition & 6 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::marker::PhantomData;

use crate::hash::hash_trait::{HashFunction, HashInputPair, HashOutput};
use crate::patricia_merkle_tree::filled_tree::node::LeafData;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::{LeafData, LeafDataTrait};
use crate::types::Felt;

#[cfg(test)]
Expand Down Expand Up @@ -94,8 +94,3 @@ impl From<u128> for NodeIndex {
#[allow(dead_code)]
#[derive(Debug, Eq, PartialEq, derive_more::Sub)]
pub(crate) struct TreeHeight(pub u8);

pub(crate) trait LeafDataTrait {
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;
use crate::patricia_merkle_tree::types::LeafDataTrait;
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;

#[allow(dead_code)]
/// A node in the structure of a Patricia-Merkle tree, after the update.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::hash::hash_trait::{HashFunction, HashOutput};
use crate::patricia_merkle_tree::errors::UpdatedSkeletonTreeError;
use crate::patricia_merkle_tree::filled_tree::tree::FilledTree;
use crate::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData, NodeData};
use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex, TreeHashFunction};
use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait;
use crate::patricia_merkle_tree::types::{NodeIndex, TreeHashFunction};
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
use crate::types::Felt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::collections::HashMap;

use crate::hash::hash_trait::HashOutput;
use crate::hash::pedersen::PedersenHashFunction;
use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, FilledNode, LeafData};
use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, FilledNode};
use crate::patricia_merkle_tree::filled_tree::tree::FilledTree;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, EdgePath, EdgePathLength, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::types::{NodeIndex, TreeHashFunctionImpl};
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::{
Expand Down

0 comments on commit 31b0b61

Please sign in to comment.