From 965844698c9bd357e2af9ed50f0ea948d4540cdf Mon Sep 17 00:00:00 2001 From: AvivYossef-starkware Date: Mon, 15 Jul 2024 11:11:01 +0300 Subject: [PATCH] refactor: remove dependency between storage and starknet prefix --- .../filled_tree/node_serde.rs | 8 +++--- .../node_data/leaf_serde.rs | 26 +++++++++---------- .../original_skeleton_tree/create_tree.rs | 4 +-- .../create_tree_test.rs | 9 ++++--- crates/committer/src/storage/db_object.rs | 15 +++-------- crates/committer/src/storage/errors.rs | 4 +-- crates/committer/src/storage/storage_trait.rs | 13 +++++++--- 7 files changed, 41 insertions(+), 38 deletions(-) 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 c8b641c1..9f631daa 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 @@ -7,7 +7,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::{ use crate::patricia_merkle_tree::node_data::leaf::Leaf; use crate::storage::db_object::DBObject; use crate::storage::errors::DeserializationError; -use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue}; +use crate::storage::storage_trait::{StarknetPrefix, StorageKey, StorageValue}; use ethnum::U256; use serde::{Deserialize, Serialize}; @@ -76,9 +76,11 @@ impl DBObject for FilledNode { } } - fn get_prefix(&self) -> StoragePrefix { + fn get_prefix(&self) -> Vec { match &self.data { - NodeData::Binary(_) | NodeData::Edge(_) => StoragePrefix::InnerNode, + NodeData::Binary(_) | NodeData::Edge(_) => { + StarknetPrefix::InnerNode.to_storage_prefix() + } NodeData::Leaf(leaf_data) => leaf_data.get_prefix(), } } diff --git a/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs b/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs index 6883d2b9..93ac61dc 100644 --- a/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs +++ b/crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs @@ -10,7 +10,7 @@ use crate::patricia_merkle_tree::node_data::leaf::ContractState; use crate::patricia_merkle_tree::types::SubTreeHeight; use crate::storage::db_object::{DBObject, Deserializable}; use crate::storage::errors::DeserializationError; -use crate::storage::storage_trait::{StoragePrefix, StorageValue}; +use crate::storage::storage_trait::{StarknetPrefix, StorageValue}; #[cfg(test)] #[path = "leaf_serde_test.rs"] @@ -22,8 +22,8 @@ impl DBObject for StarknetStorageValue { StorageValue(self.0.to_bytes_be().to_vec()) } - fn get_prefix(&self) -> StoragePrefix { - StoragePrefix::StorageLeaf + fn get_prefix(&self) -> Vec { + StarknetPrefix::StorageLeaf.to_storage_prefix() } } @@ -34,8 +34,8 @@ impl DBObject for CompiledClassHash { StorageValue(json_string.into_bytes()) } - fn get_prefix(&self) -> StoragePrefix { - StoragePrefix::CompiledClassLeaf + fn get_prefix(&self) -> Vec { + StarknetPrefix::CompiledClassLeaf.to_storage_prefix() } } @@ -52,8 +52,8 @@ impl DBObject for ContractState { StorageValue(json_string.into_bytes()) } - fn get_prefix(&self) -> StoragePrefix { - StoragePrefix::StateTreeLeaf + fn get_prefix(&self) -> Vec { + StarknetPrefix::StateTreeLeaf.to_storage_prefix() } } @@ -62,8 +62,8 @@ impl Deserializable for StarknetStorageValue { Ok(Self(Felt::from_bytes_be_slice(&value.0))) } - fn prefix() -> StoragePrefix { - StoragePrefix::StorageLeaf + fn prefix() -> Vec { + StarknetPrefix::StorageLeaf.to_storage_prefix() } } @@ -79,8 +79,8 @@ impl Deserializable for CompiledClassHash { Ok(Self::from_hex(hash_as_hex)?) } - fn prefix() -> StoragePrefix { - StoragePrefix::CompiledClassLeaf + fn prefix() -> Vec { + StarknetPrefix::CompiledClassLeaf.to_storage_prefix() } } @@ -109,8 +109,8 @@ impl Deserializable for ContractState { }) } - fn prefix() -> StoragePrefix { - StoragePrefix::StateTreeLeaf + fn prefix() -> Vec { + StarknetPrefix::StateTreeLeaf.to_storage_prefix() } } diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree.rs index 183c5363..b3b108df 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree.rs @@ -16,9 +16,9 @@ use crate::patricia_merkle_tree::{ }; use crate::storage::errors::StorageError; use crate::storage::storage_trait::create_db_key; +use crate::storage::storage_trait::StarknetPrefix; use crate::storage::storage_trait::Storage; use crate::storage::storage_trait::StorageKey; -use crate::storage::storage_trait::StoragePrefix; use log::warn; use std::borrow::Borrow; use std::collections::HashMap; @@ -235,7 +235,7 @@ impl<'a> OriginalSkeletonTreeImpl<'a> { if subtree.is_leaf() { L::prefix() } else { - StoragePrefix::InnerNode + StarknetPrefix::InnerNode.to_storage_prefix() }, &subtree.root_hash.0.to_bytes_be(), ) diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree_test.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree_test.rs index 4ab2aab9..f45e3911 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree_test.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/create_tree_test.rs @@ -15,7 +15,7 @@ use crate::patricia_merkle_tree::types::SubTreeHeight; use crate::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices}; use crate::storage::db_object::DBObject; use crate::storage::map_storage::MapStorage; -use crate::storage::storage_trait::{create_db_key, StorageKey, StoragePrefix, StorageValue}; +use crate::storage::storage_trait::{create_db_key, StarknetPrefix, StorageKey, StorageValue}; use ethnum::U256; use pretty_assertions::assert_eq; use rstest::rstest; @@ -439,7 +439,10 @@ pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (StorageKey, Storag } fn create_patricia_key(val: u128) -> StorageKey { - create_db_key(StoragePrefix::InnerNode, &U256::from(val).to_be_bytes()) + create_db_key( + StarknetPrefix::InnerNode.to_storage_prefix(), + &U256::from(val).to_be_bytes(), + ) } fn create_binary_val(left: u128, right: u128) -> StorageValue { @@ -541,7 +544,7 @@ pub(crate) fn create_root_edge_entry( let length = SubTreeHeight::ACTUAL_HEIGHT.0 - subtree_height.0; let new_root = old_root + u128::from(length); let key = create_db_key( - StoragePrefix::InnerNode, + StarknetPrefix::InnerNode.to_storage_prefix(), &Felt::from(new_root).to_bytes_be(), ); let value = StorageValue( diff --git a/crates/committer/src/storage/db_object.rs b/crates/committer/src/storage/db_object.rs index 1bc54558..d31bebfb 100644 --- a/crates/committer/src/storage/db_object.rs +++ b/crates/committer/src/storage/db_object.rs @@ -1,23 +1,16 @@ use crate::storage::errors::DeserializationError; -use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue}; +use crate::storage::storage_trait::{StorageKey, StorageValue}; pub trait DBObject { /// Serializes the given value. fn serialize(&self) -> StorageValue; /// Returns the storage key prefix of the DB object. - fn get_prefix(&self) -> StoragePrefix; + fn get_prefix(&self) -> Vec; /// Returns a `StorageKey` from a prefix and a suffix. fn get_db_key(&self, suffix: &[u8]) -> StorageKey { - StorageKey( - [ - self.get_prefix().to_bytes().to_vec(), - b":".to_vec(), - suffix.to_vec(), - ] - .concat(), - ) + StorageKey([self.get_prefix(), b":".to_vec(), suffix.to_vec()].concat()) } } @@ -26,5 +19,5 @@ pub trait Deserializable: Sized { fn deserialize(value: &StorageValue) -> Result; /// The prefix used to store in DB. - fn prefix() -> StoragePrefix; + fn prefix() -> Vec; } diff --git a/crates/committer/src/storage/errors.rs b/crates/committer/src/storage/errors.rs index e931e511..88406dc9 100644 --- a/crates/committer/src/storage/errors.rs +++ b/crates/committer/src/storage/errors.rs @@ -1,5 +1,5 @@ use crate::patricia_merkle_tree::node_data::errors::{EdgePathError, PathToBottomError}; -use crate::storage::storage_trait::{StorageKey, StoragePrefix}; +use crate::storage::storage_trait::StorageKey; use serde_json; use starknet_types_core::felt::FromStrError; @@ -30,7 +30,7 @@ pub enum DeserializationError { #[error(transparent)] PathToBottomError(#[from] PathToBottomError), #[error("Unexpected prefix ({0:?}) variant when deserializing a leaf.")] - LeafPrefixError(StoragePrefix), + LeafPrefixError(Vec), #[error(transparent)] StringConversionError(#[from] std::str::Utf8Error), #[error(transparent)] diff --git a/crates/committer/src/storage/storage_trait.rs b/crates/committer/src/storage/storage_trait.rs index 88b5f95d..7eec248f 100644 --- a/crates/committer/src/storage/storage_trait.rs +++ b/crates/committer/src/storage/storage_trait.rs @@ -30,8 +30,9 @@ pub trait Storage: From> { fn delete(&mut self, key: &StorageKey) -> Option; } +// TODO(Aviv, 17/07/2024); Split between Storage and node specific implementation. #[derive(Clone, Debug)] -pub enum StoragePrefix { +pub enum StarknetPrefix { InnerNode, StorageLeaf, StateTreeLeaf, @@ -39,7 +40,7 @@ pub enum StoragePrefix { } /// Describes a storage prefix as used in Aerospike DB. -impl StoragePrefix { +impl StarknetPrefix { pub(crate) fn to_bytes(&self) -> &'static [u8] { match self { Self::InnerNode => b"patricia_node", @@ -48,6 +49,10 @@ impl StoragePrefix { Self::CompiledClassLeaf => b"contract_class_leaf", } } + + pub(crate) fn to_storage_prefix(&self) -> Vec { + self.to_bytes().to_vec() + } } impl From for StorageKey { @@ -71,6 +76,6 @@ impl Serialize for StorageKey { } /// Returns a `StorageKey` from a prefix and a suffix. -pub(crate) fn create_db_key(prefix: StoragePrefix, suffix: &[u8]) -> StorageKey { - StorageKey([prefix.to_bytes().to_vec(), b":".to_vec(), suffix.to_vec()].concat()) +pub(crate) fn create_db_key(prefix: Vec, suffix: &[u8]) -> StorageKey { + StorageKey([prefix, b":".to_vec(), suffix.to_vec()].concat()) }