From 9a57bbd3c7592185ad803bf5b4d4f1b5994b2dc0 Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Wed, 24 Apr 2024 10:29:10 +0300 Subject: [PATCH] refactor: splitting serializable trait --- crates/committer/src/deserialization.rs | 1 - crates/committer/src/deserialization/cast.rs | 2 +- crates/committer/src/deserialization/errors.rs | 12 ------------ crates/committer/src/deserialization/read.rs | 2 +- crates/committer/src/deserialization/read_test.rs | 2 +- .../committer/src/patricia_merkle_tree/errors.rs | 4 ++-- .../patricia_merkle_tree/filled_tree/node_serde.rs | 10 +++++----- .../original_skeleton_calc.rs | 2 +- crates/committer/src/storage/errors.rs | 14 ++++++++------ crates/committer/src/storage/serde_trait.rs | 11 +++++++---- 10 files changed, 26 insertions(+), 34 deletions(-) delete mode 100644 crates/committer/src/deserialization/errors.rs diff --git a/crates/committer/src/deserialization.rs b/crates/committer/src/deserialization.rs index 03d76f0f..1f58b61f 100644 --- a/crates/committer/src/deserialization.rs +++ b/crates/committer/src/deserialization.rs @@ -1,4 +1,3 @@ pub mod cast; -pub mod errors; pub mod read; pub mod types; diff --git a/crates/committer/src/deserialization/cast.rs b/crates/committer/src/deserialization/cast.rs index 365fc183..65bfd63a 100644 --- a/crates/committer/src/deserialization/cast.rs +++ b/crates/committer/src/deserialization/cast.rs @@ -1,4 +1,3 @@ -use crate::deserialization::errors::DeserializationError; use crate::deserialization::types::{ContractAddress, ContractState}; use crate::deserialization::types::{ Input, RawInput, StarknetStorageKey, StarknetStorageValue, StateDiff, @@ -6,6 +5,7 @@ use crate::deserialization::types::{ use crate::hash::hash_trait::HashOutput; use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, CompiledClassHash, Nonce}; use crate::patricia_merkle_tree::types::TreeHeight; +use crate::storage::errors::DeserializationError; use crate::storage::storage_trait::{StorageKey, StorageValue}; use crate::types::Felt; diff --git a/crates/committer/src/deserialization/errors.rs b/crates/committer/src/deserialization/errors.rs deleted file mode 100644 index 2420d57b..00000000 --- a/crates/committer/src/deserialization/errors.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::fmt::Debug; - -use thiserror::Error; - -#[allow(dead_code)] -#[derive(Debug, Error)] -pub(crate) enum DeserializationError { - #[error("There is a key duplicate at {0} mapping.")] - KeyDuplicate(String), - #[error("Couldn't read and parse the given input JSON: {0}")] - ParsingError(#[from] serde_json::Error), -} diff --git a/crates/committer/src/deserialization/read.rs b/crates/committer/src/deserialization/read.rs index 0c4aa321..1ec53b04 100644 --- a/crates/committer/src/deserialization/read.rs +++ b/crates/committer/src/deserialization/read.rs @@ -1,6 +1,6 @@ -use crate::deserialization::errors::DeserializationError; use crate::deserialization::types::Input; use crate::deserialization::types::RawInput; +use crate::storage::errors::DeserializationError; #[cfg(test)] #[path = "read_test.rs"] diff --git a/crates/committer/src/deserialization/read_test.rs b/crates/committer/src/deserialization/read_test.rs index 0647246a..dc1d3fe2 100644 --- a/crates/committer/src/deserialization/read_test.rs +++ b/crates/committer/src/deserialization/read_test.rs @@ -1,13 +1,13 @@ use pretty_assertions::assert_eq; use std::collections::HashMap; -use crate::deserialization::errors::DeserializationError; use crate::deserialization::types::{ ContractAddress, ContractState, Input, StarknetStorageKey, StarknetStorageValue, StateDiff, }; use crate::hash::hash_trait::HashOutput; use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, CompiledClassHash, Nonce}; use crate::patricia_merkle_tree::types::TreeHeight; +use crate::storage::errors::DeserializationError; use crate::storage::storage_trait::{StorageKey, StorageValue}; use crate::types::Felt; diff --git a/crates/committer/src/patricia_merkle_tree/errors.rs b/crates/committer/src/patricia_merkle_tree/errors.rs index 586b3b5e..f92a45e8 100644 --- a/crates/committer/src/patricia_merkle_tree/errors.rs +++ b/crates/committer/src/patricia_merkle_tree/errors.rs @@ -2,7 +2,7 @@ use thiserror::Error; use crate::patricia_merkle_tree::node_data::leaf::LeafDataTrait; use crate::patricia_merkle_tree::types::NodeIndex; -use crate::storage::errors::{SerializationError, StorageError}; +use crate::storage::errors::{DeserializationError, StorageError}; use crate::patricia_merkle_tree::filled_tree::node::FilledNode; @@ -12,7 +12,7 @@ pub(crate) enum OriginalSkeletonTreeError { #[error( "Failed to deserialize the storage value: {0:?} while building the original skeleton tree." )] - Deserialization(#[from] SerializationError), + Deserialization(#[from] DeserializationError), #[error( "Unable to read from storage the storage key: {0:?} while building the \ original skeleton tree." 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 59f8599a..724d5667 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 @@ -4,8 +4,8 @@ use crate::patricia_merkle_tree::node_data::inner_node::{ BinaryData, EdgeData, EdgePath, EdgePathLength, NodeData, PathToBottom, }; use crate::patricia_merkle_tree::node_data::leaf::LeafData; -use crate::storage::errors::SerializationError; -use crate::storage::serde_trait::Serializable; +use crate::storage::errors::{DeserializationError, SerializationError}; +use crate::storage::serde_trait::{Deserializable, Serializable}; use crate::storage::storage_trait::{create_db_key, StorageKey, StoragePrefix, StorageValue}; use crate::types::Felt; use serde::{Deserialize, Serialize}; @@ -28,7 +28,7 @@ pub(crate) struct LeafCompiledClassToSerialize { /// Alias for serialization and deserialization results of filled nodes. type FilledNodeSerializationResult = Result; -type FilledNodeDeserializationResult = Result, SerializationError>; +type FilledNodeDeserializationResult = Result, DeserializationError>; impl FilledNode { pub(crate) fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] { @@ -41,7 +41,6 @@ impl Serializable for FilledNode { /// - 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)] fn serialize(&self) -> FilledNodeSerializationResult { match &self.data { NodeData::Binary(BinaryData { @@ -76,7 +75,6 @@ impl Serializable for FilledNode { } /// Returns the db key of the filled node - [prefix + b":" + suffix]. - #[allow(dead_code)] fn db_key(&self) -> StorageKey { let suffix = self.suffix(); @@ -95,7 +93,9 @@ impl Serializable for FilledNode { } } } +} +impl Deserializable for FilledNode { /// 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 { diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs index 89291f15..500f6098 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc.rs @@ -13,7 +13,7 @@ use crate::patricia_merkle_tree::{ original_skeleton_tree::node::OriginalSkeletonNode, types::NodeIndex, }; use crate::storage::errors::StorageError; -use crate::storage::serde_trait::Serializable; +use crate::storage::serde_trait::Deserializable; use crate::storage::storage_trait::create_db_key; use crate::storage::storage_trait::Storage; use crate::storage::storage_trait::StorageKey; diff --git a/crates/committer/src/storage/errors.rs b/crates/committer/src/storage/errors.rs index 13fb7ef7..2cffe67b 100644 --- a/crates/committer/src/storage/errors.rs +++ b/crates/committer/src/storage/errors.rs @@ -2,20 +2,22 @@ use crate::storage::storage_trait::StorageKey; use serde_json; use thiserror::Error; -use crate::storage::storage_trait::StorageValue; - #[derive(Debug, Error)] -#[allow(dead_code)] pub(crate) enum StorageError { #[error("The key {0:?} does not exist in storage.")] MissingKey(StorageKey), } #[derive(thiserror::Error, Debug)] -#[allow(dead_code)] pub(crate) enum SerializationError { - #[error("Failed to deserialize the storage value: {0:?}")] - DeserializeError(StorageValue), #[error("Serialize error: {0}")] SerializeError(#[from] serde_json::Error), } + +#[derive(thiserror::Error, Debug)] +pub(crate) enum DeserializationError { + #[error("There is a key duplicate at {0} mapping.")] + KeyDuplicate(String), + #[error("Couldn't read and parse the given input JSON: {0}")] + ParsingError(#[from] serde_json::Error), +} diff --git a/crates/committer/src/storage/serde_trait.rs b/crates/committer/src/storage/serde_trait.rs index e8c15c7c..ecb85077 100644 --- a/crates/committer/src/storage/serde_trait.rs +++ b/crates/committer/src/storage/serde_trait.rs @@ -1,13 +1,16 @@ -use crate::storage::errors::SerializationError; +use crate::storage::errors::{DeserializationError, SerializationError}; use crate::storage::storage_trait::{StorageKey, StorageValue}; pub(crate) trait Serializable { /// Serializes the given value. fn serialize(&self) -> Result; + /// Returns the key used to store self in storage. + fn db_key(&self) -> StorageKey; +} + +pub(crate) trait Deserializable: Sized { /// Deserializes the given value. - fn deserialize(key: &StorageKey, value: &StorageValue) -> Result + fn deserialize(key: &StorageKey, value: &StorageValue) -> Result where Self: Sized; - /// Returns the key used to store self in storage. - fn db_key(&self) -> StorageKey; }