From 6e12429c61fd810517165dccd3172eed7e9e8162 Mon Sep 17 00:00:00 2001 From: amosStarkware <88497213+amosStarkware@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:23:47 +0200 Subject: [PATCH] chore: add node_data/leaf (#62) --- .../src/patricia_merkle_tree/errors.rs | 3 +- .../patricia_merkle_tree/filled_tree/node.rs | 32 +--------------- .../filled_tree/node_serde.rs | 3 +- .../patricia_merkle_tree/filled_tree/tree.rs | 3 +- .../src/patricia_merkle_tree/node_data.rs | 1 + .../node_data/inner_node.rs | 3 +- .../patricia_merkle_tree/node_data/leaf.rs | 37 +++++++++++++++++++ .../original_skeleton_tree/node.rs | 2 +- .../original_skeleton_calc.rs | 2 +- .../original_skeleton_tree/tree.rs | 3 +- .../src/patricia_merkle_tree/types.rs | 7 +--- .../updated_skeleton_tree/node.rs | 2 +- .../updated_skeleton_tree/tree.rs | 3 +- .../updated_skeleton_tree/tree_test.rs | 3 +- 14 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 crates/committer/src/patricia_merkle_tree/node_data/leaf.rs diff --git a/crates/committer/src/patricia_merkle_tree/errors.rs b/crates/committer/src/patricia_merkle_tree/errors.rs index 180be360..34ef57ec 100644 --- a/crates/committer/src/patricia_merkle_tree/errors.rs +++ b/crates/committer/src/patricia_merkle_tree/errors.rs @@ -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 { diff --git a/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs b/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs index ad338b65..146225bb 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs @@ -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. @@ -27,36 +27,6 @@ pub(crate) struct FilledNode { pub(crate) data: NodeData, } -#[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: diff --git a/crates/committer/src/patricia_merkle_tree/filled_tree/node_serde.rs b/crates/committer/src/patricia_merkle_tree/filled_tree/node_serde.rs index e556bdd3..dc21d912 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree/node_serde.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree/node_serde.rs @@ -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, StoragePrefix, StorageValue}; use crate::types::Felt; diff --git a/crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs b/crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs index e4050a4b..0f212da0 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs @@ -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; diff --git a/crates/committer/src/patricia_merkle_tree/node_data.rs b/crates/committer/src/patricia_merkle_tree/node_data.rs index b445ca14..87006a34 100644 --- a/crates/committer/src/patricia_merkle_tree/node_data.rs +++ b/crates/committer/src/patricia_merkle_tree/node_data.rs @@ -1 +1,2 @@ pub mod inner_node; +pub mod leaf; diff --git a/crates/committer/src/patricia_merkle_tree/node_data/inner_node.rs b/crates/committer/src/patricia_merkle_tree/node_data/inner_node.rs index de2441ea..048fa4ac 100644 --- a/crates/committer/src/patricia_merkle_tree/node_data/inner_node.rs +++ b/crates/committer/src/patricia_merkle_tree/node_data/inner_node.rs @@ -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)] diff --git a/crates/committer/src/patricia_merkle_tree/node_data/leaf.rs b/crates/committer/src/patricia_merkle_tree/node_data/leaf.rs new file mode 100644 index 00000000..6e8be8cd --- /dev/null +++ b/crates/committer/src/patricia_merkle_tree/node_data/leaf.rs @@ -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 + } + } + } +} diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/node.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/node.rs index 299879c5..927b2a03 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/node.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/node.rs @@ -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)] diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs index 71b6cf3d..8dc8a1ad 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs @@ -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; diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/tree.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/tree.rs index ff129797..757aac0a 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/tree.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/tree.rs @@ -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; diff --git a/crates/committer/src/patricia_merkle_tree/types.rs b/crates/committer/src/patricia_merkle_tree/types.rs index 2a76c719..32332f3e 100644 --- a/crates/committer/src/patricia_merkle_tree/types.rs +++ b/crates/committer/src/patricia_merkle_tree/types.rs @@ -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)] @@ -94,8 +94,3 @@ impl From 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; -} diff --git a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/node.rs b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/node.rs index 546791a4..29e59da0 100644 --- a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/node.rs +++ b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/node.rs @@ -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. diff --git a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree.rs b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree.rs index d67e78f3..cbf2fc4b 100644 --- a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree.rs +++ b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree.rs @@ -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; diff --git a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree_test.rs b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree_test.rs index 7a29c962..ee09fad3 100644 --- a/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree_test.rs +++ b/crates/committer/src/patricia_merkle_tree/updated_skeleton_tree/tree_test.rs @@ -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::{