-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e12429
commit 57bf31c
Showing
4 changed files
with
51 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} | ||
} |