Skip to content

Commit

Permalink
chore: delete SerializeNode (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Apr 24, 2024
1 parent d318e38 commit 15f5469
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,13 @@ 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 {
Binary(Vec<u8>),
Edge(Vec<u8>),
CompiledClassLeaf(Vec<u8>),
StorageLeaf(Vec<u8>),
StateTreeLeaf(Vec<u8>),
}

impl FilledNode<LeafData> {
/// This method serializes the filled node into a byte vector, where:
/// - For binary nodes: Concatenates left and right hashes.
/// - For edge nodes: Concatenates bottom hash, path, and path length.
/// - For leaf nodes: use leaf.serialize() method.
#[allow(dead_code)]
pub(crate) fn serialize(&self) -> FilledTreeResult<SerializeNode> {
pub(crate) fn serialize(&self) -> FilledTreeResult<StorageValue> {
match &self.data {
NodeData::Binary(BinaryData {
left_hash,
Expand All @@ -50,7 +39,7 @@ impl FilledNode<LeafData> {

// Concatenate left and right hashes.
let serialized = [left, right].concat();
Ok(SerializeNode::Binary(serialized))
Ok(StorageValue(serialized))
}

NodeData::Edge(EdgeData {
Expand All @@ -64,7 +53,7 @@ impl FilledNode<LeafData> {

// Concatenate bottom hash, path, and path length.
let serialized = [bottom.to_vec(), path.to_vec(), length.to_vec()].concat();
Ok(SerializeNode::Edge(serialized))
Ok(StorageValue(serialized))
}

NodeData::Leaf(leaf_data) => leaf_data.serialize(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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.
Expand All @@ -10,6 +9,7 @@ pub(crate) struct LeafCompiledClassToSerialize {
pub(crate) compiled_class_hash: Felt,
}
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::storage::storage_trait::StorageValue;
use crate::types::Felt;

impl LeafData {
Expand All @@ -18,11 +18,9 @@ impl LeafData {
/// - 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> {
pub(crate) fn serialize(&self) -> Result<StorageValue, FilledTreeError> {
match &self {
LeafData::StorageValue(value) => {
Ok(SerializeNode::StorageLeaf(value.as_bytes().to_vec()))
}
LeafData::StorageValue(value) => Ok(StorageValue(value.as_bytes().to_vec())),

LeafData::CompiledClassHash(class_hash) => {
// Create a temporary object to serialize the leaf into a JSON.
Expand All @@ -34,9 +32,7 @@ impl LeafData {
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(),
))
Ok(StorageValue(json.into_bytes().to_owned()))
}

LeafData::StateTreeTuple { .. } => {
Expand Down

0 comments on commit 15f5469

Please sign in to comment.