Skip to content

Commit

Permalink
refactor: splitting serializable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Apr 24, 2024
1 parent 758a47a commit 9a57bbd
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 34 deletions.
1 change: 0 additions & 1 deletion crates/committer/src/deserialization.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod cast;
pub mod errors;
pub mod read;
pub mod types;
2 changes: 1 addition & 1 deletion crates/committer/src/deserialization/cast.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::deserialization::errors::DeserializationError;
use crate::deserialization::types::{ContractAddress, ContractState};
use crate::deserialization::types::{
Input, RawInput, 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;

Expand Down
12 changes: 0 additions & 12 deletions crates/committer/src/deserialization/errors.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/committer/src/deserialization/read.rs
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/committer/src/deserialization/read_test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -28,7 +28,7 @@ pub(crate) struct LeafCompiledClassToSerialize {

/// Alias for serialization and deserialization results of filled nodes.
type FilledNodeSerializationResult = Result<StorageValue, SerializationError>;
type FilledNodeDeserializationResult = Result<FilledNode<LeafData>, SerializationError>;
type FilledNodeDeserializationResult = Result<FilledNode<LeafData>, DeserializationError>;

impl FilledNode<LeafData> {
pub(crate) fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
Expand All @@ -41,7 +41,6 @@ impl Serializable for FilledNode<LeafData> {
/// - 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 {
Expand Down Expand Up @@ -76,7 +75,6 @@ impl Serializable for FilledNode<LeafData> {
}

/// Returns the db key of the filled node - [prefix + b":" + suffix].
#[allow(dead_code)]
fn db_key(&self) -> StorageKey {
let suffix = self.suffix();

Expand All @@ -95,7 +93,9 @@ impl Serializable for FilledNode<LeafData> {
}
}
}
}

impl Deserializable for FilledNode<LeafData> {
/// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions crates/committer/src/storage/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
11 changes: 7 additions & 4 deletions crates/committer/src/storage/serde_trait.rs
Original file line number Diff line number Diff line change
@@ -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<StorageValue, SerializationError>;
/// 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<Self, SerializationError>
fn deserialize(key: &StorageKey, value: &StorageValue) -> Result<Self, DeserializationError>
where
Self: Sized;
/// Returns the key used to store self in storage.
fn db_key(&self) -> StorageKey;
}

0 comments on commit 9a57bbd

Please sign in to comment.