Skip to content

feat: added OriginalSkeletonInputNode enum to remove generic leaf fro… #168

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

Merged
merged 1 commit into from
Jun 2, 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
61 changes: 26 additions & 35 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::inner_node::{
BinaryData, EdgeData, EdgePathLength, NodeData, PathToBottom,
};
use crate::patricia_merkle_tree::node_data::leaf::{LeafData, LeafDataImpl};
use crate::patricia_merkle_tree::node_data::leaf::LeafData;
use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonInputNode;
use crate::storage::db_object::{DBObject, Deserializable};
use crate::storage::errors::{DeserializationError, SerializationError};
use crate::storage::errors::DeserializationError;
use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue};
use ethnum::U256;
use serde::{Deserialize, Serialize};
Expand All @@ -27,11 +28,6 @@ pub(crate) struct LeafCompiledClassToSerialize {
pub(crate) compiled_class_hash: Felt,
}

/// Alias for serialization and deserialization results of filled nodes.
#[allow(dead_code)]
type FilledNodeSerializationResult = Result<StorageValue, SerializationError>;
type FilledNodeDeserializationResult = Result<FilledNode<LeafDataImpl>, DeserializationError>;

impl<L: LeafData> FilledNode<L> {
pub fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
self.hash.0.to_bytes_be()
Expand Down Expand Up @@ -89,47 +85,42 @@ impl<L: LeafData> DBObject for FilledNode<L> {
}
}

impl Deserializable for FilledNode<LeafDataImpl> {
impl Deserializable for OriginalSkeletonInputNode {
/// Deserializes non-leaf nodes; if a serialized leaf node is given, the hash
/// is used but the data is ignored.
fn deserialize(key: &StorageKey, value: &StorageValue) -> FilledNodeDeserializationResult {
fn deserialize(
key: &StorageKey,
value: &StorageValue,
) -> Result<OriginalSkeletonInputNode, DeserializationError> {
if value.0.len() == BINARY_BYTES {
Ok(Self {
Ok(Self::Binary {
hash: HashOutput(Felt::from_bytes_be_slice(&key.0)),
data: NodeData::Binary(BinaryData {
data: BinaryData {
left_hash: HashOutput(Felt::from_bytes_be_slice(
&value.0[..SERIALIZE_HASH_BYTES],
)),
right_hash: HashOutput(Felt::from_bytes_be_slice(
&value.0[SERIALIZE_HASH_BYTES..],
)),
}),
},
})
} else if value.0.len() == EDGE_BYTES {
return Ok(Self {
hash: HashOutput(Felt::from_bytes_be_slice(&key.0)),
data: NodeData::Edge(EdgeData {
bottom_hash: HashOutput(Felt::from_bytes_be_slice(
&value.0[..SERIALIZE_HASH_BYTES],
)),
path_to_bottom: PathToBottom {
path: U256::from_be_bytes(
value.0[SERIALIZE_HASH_BYTES..SERIALIZE_HASH_BYTES + EDGE_PATH_BYTES]
.try_into()
.expect("Slice with incorrect length."),
)
.into(),
length: EdgePathLength(value.0[EDGE_BYTES - 1]),
},
}),
});
return Ok(Self::Edge(EdgeData {
bottom_hash: HashOutput(Felt::from_bytes_be_slice(
&value.0[..SERIALIZE_HASH_BYTES],
)),
path_to_bottom: PathToBottom {
path: U256::from_be_bytes(
value.0[SERIALIZE_HASH_BYTES..SERIALIZE_HASH_BYTES + EDGE_PATH_BYTES]
.try_into()
.expect("Slice with incorrect length."),
)
.into(),
length: EdgePathLength(value.0[EDGE_BYTES - 1]),
},
}));
} else {
// TODO(Nimrod, 5/5/2024): See if deserializing leaves data is needed somewhere.
return Ok(Self {
hash: HashOutput(Felt::from_bytes_be_slice(&key.0)),
// Dummy value which will be ignored.
data: NodeData::Leaf(LeafDataImpl::StorageValue(Felt::ZERO)),
});
return Ok(Self::Leaf(HashOutput(Felt::from_bytes_be_slice(&key.0))));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
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::LeafDataImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonInputNode;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::original_skeleton_tree::utils::split_leaves;
Expand Down Expand Up @@ -113,17 +110,21 @@ impl OriginalSkeletonTreeImpl {
}
let mut next_subtrees = Vec::new();
let subtrees_roots = Self::calculate_subtrees_roots(&subtrees, storage, &self.tree_height)?;
for (filled_node, subtree) in subtrees_roots.into_iter().zip(subtrees.iter()) {
match filled_node.data {
for (skeleton_node_input, subtree) in subtrees_roots.into_iter().zip(subtrees.iter()) {
match skeleton_node_input {
// Binary node.
NodeData::Binary(BinaryData {
left_hash,
right_hash,
}) => {
OriginalSkeletonInputNode::Binary {
hash,
data:
BinaryData {
left_hash,
right_hash,
},
} => {
if subtree.is_sibling() {
self.nodes.insert(
subtree.root_index,
OriginalSkeletonNode::LeafOrBinarySibling(filled_node.hash),
OriginalSkeletonNode::LeafOrBinarySibling(hash),
);
continue;
}
Expand All @@ -134,7 +135,7 @@ impl OriginalSkeletonTreeImpl {
next_subtrees.extend(vec![left_subtree, right_subtree]);
}
// Edge node.
NodeData::Edge(EdgeData {
OriginalSkeletonInputNode::Edge(EdgeData {
bottom_hash,
path_to_bottom,
}) => {
Expand All @@ -155,11 +156,11 @@ impl OriginalSkeletonTreeImpl {
next_subtrees.push(bottom_subtree);
}
// Leaf node.
NodeData::Leaf(_) => {
OriginalSkeletonInputNode::Leaf(hash) => {
if subtree.is_sibling() {
self.nodes.insert(
subtree.root_index,
OriginalSkeletonNode::LeafOrBinarySibling(filled_node.hash),
OriginalSkeletonNode::LeafOrBinarySibling(hash),
);
}
}
Expand All @@ -172,20 +173,16 @@ impl OriginalSkeletonTreeImpl {
subtrees: &[SubTree<'_>],
storage: &impl Storage,
total_tree_height: &TreeHeight,
) -> OriginalSkeletonTreeResult<Vec<FilledNode<LeafDataImpl>>> {
) -> OriginalSkeletonTreeResult<Vec<OriginalSkeletonInputNode>> {
let mut subtrees_roots = vec![];
for subtree in subtrees.iter() {
if subtree.is_leaf(total_tree_height) {
subtrees_roots.push(FilledNode {
hash: subtree.root_hash,
// Dummy value that will be ignored.
data: NodeData::Leaf(LeafDataImpl::StorageValue(Felt::ZERO)),
});
subtrees_roots.push(OriginalSkeletonInputNode::Leaf(subtree.root_hash));
continue;
}
let key = create_db_key(StoragePrefix::InnerNode, &subtree.root_hash.0.to_bytes_be());
let val = storage.get(&key).ok_or(StorageError::MissingKey(key))?;
subtrees_roots.push(FilledNode::deserialize(
subtrees_roots.push(OriginalSkeletonInputNode::deserialize(
&StorageKey::from(subtree.root_hash.0),
val,
)?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};
use crate::patricia_merkle_tree::node_data::leaf::LeafDataImpl;
use crate::patricia_merkle_tree::node_data::leaf::LeafModifications;
use crate::patricia_merkle_tree::original_skeleton_tree::create_tree::LeafDataImpl;
use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonNode;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::{
OriginalSkeletonNodeMap, OriginalSkeletonTree,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;
use crate::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData, PathToBottom};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// A node in the structure of a Patricia-Merkle tree, before the update.
Expand All @@ -11,3 +11,10 @@ pub(crate) enum OriginalSkeletonNode {
// Unmodified edge siblings bottom nodes on the merkle paths of modified leaves.
UnmodifiedBottom(HashOutput),
}

/// A representation of the data required to build an original skeleton node.
pub(crate) enum OriginalSkeletonInputNode {
Binary { hash: HashOutput, data: BinaryData },
Edge(EdgeData),
Leaf(HashOutput),
}
Loading