Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add node_data/leaf_serde #63

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,37 +22,3 @@ pub(crate) struct FilledNode<L: LeafDataTrait> {
pub(crate) hash: HashOutput,
pub(crate) data: NodeData<L>,
}

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<SerializeNode, FilledTreeError> {
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.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -32,13 +32,6 @@ pub(crate) enum SerializeNode {
StateTreeLeaf(Vec<u8>),
}

/// 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<LeafData> {
/// This method serializes the filled node into a byte vector, where:
/// - For binary nodes: Concatenates left and right hashes.
Expand Down Expand Up @@ -84,6 +77,7 @@ impl FilledNode<LeafData> {
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 {
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,2 +1,3 @@
pub mod inner_node;
pub mod leaf;
pub mod leaf_serde;
47 changes: 47 additions & 0 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs
Original file line number Diff line number Diff line change
@@ -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<SerializeNode, FilledTreeError> {
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.");
}
}
}
}
Loading