From 57bf31c24811fe701fab750f5276b071d89bab47 Mon Sep 17 00:00:00 2001 From: amos rothberg Date: Tue, 23 Apr 2024 19:56:35 +0300 Subject: [PATCH] chore: add leaf serde --- .../patricia_merkle_tree/filled_tree/node.rs | 40 +--------------- .../filled_tree/node_serde.rs | 10 +--- .../src/patricia_merkle_tree/node_data.rs | 1 + .../node_data/leaf_serde.rs | 47 +++++++++++++++++++ 4 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs 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 146225bb..f4731964 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree/node.rs @@ -1,10 +1,6 @@ use crate::hash::hash_trait::HashOutput; -use crate::patricia_merkle_tree::errors::FilledTreeError; -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::node_data::leaf::{LeafData, LeafDataTrait}; +use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait; use crate::types::Felt; // TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented. @@ -26,37 +22,3 @@ pub(crate) struct FilledNode { pub(crate) hash: HashOutput, pub(crate) data: NodeData, } - -impl LeafData { - /// Serializes the leaf data into a byte vector. - /// The serialization is done as follows: - /// - For storage values: serializes the value into a 32-byte vector. - /// - For compiled class hashes or state tree tuples: creates a json string - /// describing the leaf and cast it into a byte vector. - pub(crate) fn serialize(&self) -> Result { - match &self { - LeafData::StorageValue(value) => { - Ok(SerializeNode::StorageLeaf(value.as_bytes().to_vec())) - } - - LeafData::CompiledClassHash(class_hash) => { - // Create a temporary object to serialize the leaf into a JSON. - let temp_object_to_json = LeafCompiledClassToSerialize { - compiled_class_hash: class_hash.0, - }; - - // Serialize the leaf into a JSON. - let json = serde_json::to_string(&temp_object_to_json)?; - - // Serialize the json into a byte vector. - Ok(SerializeNode::CompiledClassLeaf( - json.into_bytes().to_owned(), - )) - } - - LeafData::StateTreeTuple { .. } => { - todo!("implement."); - } - } - } -} 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 dc21d912..80429aa8 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 @@ -8,7 +8,6 @@ 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; -use serde::{Deserialize, Serialize}; // Const describe the size of the serialized node. pub(crate) const SERIALIZE_HASH_BYTES: usize = 32; @@ -22,6 +21,7 @@ pub(crate) const STORAGE_LEAF_SIZE: usize = SERIALIZE_HASH_BYTES; // TODO(Aviv, 17/4/2024): add CompiledClassLeaf size. // TODO(Aviv, 17/4/2024): add StateTreeLeaf size. +// TODO(Amos, 1/5/2024): Delete SerializeNode, as it's no longer needed. /// Enum to describe the serialized node. #[allow(dead_code)] pub(crate) enum SerializeNode { @@ -32,13 +32,6 @@ pub(crate) enum SerializeNode { StateTreeLeaf(Vec), } -/// Temporary struct to serialize the leaf CompiledClass. -/// Required to comply to existing storage layout. -#[derive(Serialize, Deserialize)] -pub(crate) struct LeafCompiledClassToSerialize { - pub(crate) compiled_class_hash: Felt, -} - impl FilledNode { /// This method serializes the filled node into a byte vector, where: /// - For binary nodes: Concatenates left and right hashes. @@ -84,6 +77,7 @@ impl FilledNode { self.hash.0.as_bytes() } + // TODO(Amos, 1/5/2024): move leaf logic to leaf_serde. /// Returns the db key of the filled node. #[allow(dead_code)] pub(crate) fn db_key(&self) -> StorageKey { diff --git a/crates/committer/src/patricia_merkle_tree/node_data.rs b/crates/committer/src/patricia_merkle_tree/node_data.rs index 87006a34..0a6cc76b 100644 --- a/crates/committer/src/patricia_merkle_tree/node_data.rs +++ b/crates/committer/src/patricia_merkle_tree/node_data.rs @@ -1,2 +1,3 @@ pub mod inner_node; pub mod leaf; +pub mod leaf_serde; diff --git a/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs b/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs new file mode 100644 index 00000000..22e8c0ff --- /dev/null +++ b/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs @@ -0,0 +1,47 @@ +use serde::{Deserialize, Serialize}; + +use crate::patricia_merkle_tree::errors::FilledTreeError; +use crate::patricia_merkle_tree::filled_tree::node_serde::SerializeNode; + +/// Temporary struct to serialize the leaf CompiledClass. +/// Required to comply to existing storage layout. +#[derive(Serialize, Deserialize)] +pub(crate) struct LeafCompiledClassToSerialize { + pub(crate) compiled_class_hash: Felt, +} +use crate::patricia_merkle_tree::node_data::leaf::LeafData; +use crate::types::Felt; + +impl LeafData { + /// Serializes the leaf data into a byte vector. + /// The serialization is done as follows: + /// - For storage values: serializes the value into a 32-byte vector. + /// - For compiled class hashes or state tree tuples: creates a json string + /// describing the leaf and cast it into a byte vector. + pub(crate) fn serialize(&self) -> Result { + match &self { + LeafData::StorageValue(value) => { + Ok(SerializeNode::StorageLeaf(value.as_bytes().to_vec())) + } + + LeafData::CompiledClassHash(class_hash) => { + // Create a temporary object to serialize the leaf into a JSON. + let temp_object_to_json = LeafCompiledClassToSerialize { + compiled_class_hash: class_hash.0, + }; + + // Serialize the leaf into a JSON. + let json = serde_json::to_string(&temp_object_to_json)?; + + // Serialize the json into a byte vector. + Ok(SerializeNode::CompiledClassLeaf( + json.into_bytes().to_owned(), + )) + } + + LeafData::StateTreeTuple { .. } => { + todo!("implement."); + } + } + } +}