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 acbbec4a..e556bdd3 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 @@ -5,19 +5,15 @@ use crate::patricia_merkle_tree::node_data::inner_node::{ BinaryData, EdgeData, EdgePath, EdgePathLength, NodeData, PathToBottom, }; use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult; -use crate::storage::storage_trait::{create_db_key, StorageKey, StorageValue}; +use crate::storage::storage_trait::{create_db_key, StorageKey, StoragePrefix, StorageValue}; use crate::types::Felt; use serde::{Deserialize, Serialize}; // Const describe the size of the serialized node. pub(crate) const SERIALIZE_HASH_BYTES: usize = 32; -#[allow(dead_code)] pub(crate) const BINARY_BYTES: usize = 2 * SERIALIZE_HASH_BYTES; -#[allow(dead_code)] pub(crate) const EDGE_LENGTH_BYTES: usize = 1; -#[allow(dead_code)] pub(crate) const EDGE_PATH_BYTES: usize = 32; -#[allow(dead_code)] pub(crate) const EDGE_BYTES: usize = SERIALIZE_HASH_BYTES + EDGE_PATH_BYTES + EDGE_LENGTH_BYTES; #[allow(dead_code)] pub(crate) const STORAGE_LEAF_SIZE: usize = SERIALIZE_HASH_BYTES; @@ -25,12 +21,6 @@ pub(crate) const STORAGE_LEAF_SIZE: usize = SERIALIZE_HASH_BYTES; // TODO(Aviv, 17/4/2024): add CompiledClassLeaf size. // TODO(Aviv, 17/4/2024): add StateTreeLeaf size. -// Const describe the prefix of the serialized node. -pub(crate) const STORAGE_LEAF_PREFIX: &[u8; 21] = b"starknet_storage_leaf"; -pub(crate) const STATE_TREE_LEAF_PREFIX: &[u8; 14] = b"contract_state"; -pub(crate) const COMPLIED_CLASS_PREFIX: &[u8; 19] = b"contract_class_leaf"; -pub(crate) const INNER_NODE_PREFIX: &[u8; 13] = b"patricia_node"; - /// Enum to describe the serialized node. #[allow(dead_code)] pub(crate) enum SerializeNode { @@ -93,21 +83,23 @@ impl FilledNode { self.hash.0.as_bytes() } - /// Returns the db key of the filled node - [prefix + b":" + suffix]. + /// Returns the db key of the filled node. #[allow(dead_code)] pub(crate) fn db_key(&self) -> StorageKey { let suffix = self.suffix(); match &self.data { - NodeData::Binary(_) | NodeData::Edge(_) => create_db_key(INNER_NODE_PREFIX, &suffix), + NodeData::Binary(_) | NodeData::Edge(_) => { + create_db_key(StoragePrefix::InnerNode, &suffix) + } NodeData::Leaf(LeafData::StorageValue(_)) => { - create_db_key(STORAGE_LEAF_PREFIX, &suffix) + create_db_key(StoragePrefix::StorageLeaf, &suffix) } NodeData::Leaf(LeafData::CompiledClassHash(_)) => { - create_db_key(COMPLIED_CLASS_PREFIX, &suffix) + create_db_key(StoragePrefix::CompiledClassLeaf, &suffix) } NodeData::Leaf(LeafData::StateTreeTuple { .. }) => { - create_db_key(STATE_TREE_LEAF_PREFIX, &suffix) + create_db_key(StoragePrefix::StateTreeLeaf, &suffix) } } } 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 577577da..71b6cf3d 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,6 +13,7 @@ use crate::patricia_merkle_tree::{ original_skeleton_tree::node::OriginalSkeletonNode, types::NodeIndex, }; use crate::storage::errors::StorageError; +use crate::storage::storage_trait::create_db_key; use crate::storage::storage_trait::Storage; use crate::storage::storage_trait::StorageKey; use crate::storage::storage_trait::StoragePrefix; @@ -205,8 +206,7 @@ impl OriginalSkeletonTreeImpl { }); continue; } - let key = - StorageKey::from(subtree.root_hash.0).with_prefix(StoragePrefix::PatriciaNode); + let key = create_db_key(StoragePrefix::InnerNode, &subtree.root_hash.0.as_bytes()); let val = storage.get(&key).ok_or(StorageError::MissingKey(key))?; subtrees_roots.push(FilledNode::deserialize( &StorageKey::from(subtree.root_hash.0), diff --git a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc_test.rs b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc_test.rs index f89ea06e..e36f8112 100644 --- a/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc_test.rs +++ b/crates/committer/src/patricia_merkle_tree/original_skeleton_tree/original_skeleton_calc_test.rs @@ -11,7 +11,7 @@ use rstest::rstest; use std::collections::HashMap; use crate::patricia_merkle_tree::types::TreeHeight; -use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue}; +use crate::storage::storage_trait::{create_db_key, StorageKey, StoragePrefix, StorageValue}; use super::OriginalSkeletonTreeImpl; @@ -245,7 +245,7 @@ fn create_32_bytes_entry(simple_val: u8) -> Vec { } fn create_patricia_key(val: u8) -> StorageKey { - StorageKey(create_32_bytes_entry(val)).with_prefix(StoragePrefix::PatriciaNode) + create_db_key(StoragePrefix::InnerNode, &create_32_bytes_entry(val)) } fn create_binary_val(left: u8, right: u8) -> StorageValue { diff --git a/crates/committer/src/storage/storage_trait.rs b/crates/committer/src/storage/storage_trait.rs index f143b501..856f5c7a 100644 --- a/crates/committer/src/storage/storage_trait.rs +++ b/crates/committer/src/storage/storage_trait.rs @@ -29,25 +29,24 @@ pub(crate) trait Storage { } pub(crate) enum StoragePrefix { - PatriciaNode, + InnerNode, + StorageLeaf, + StateTreeLeaf, + CompiledClassLeaf, } +/// Describes a storage prefix as used in Aerospike DB. impl StoragePrefix { - pub(crate) fn to_bytes(&self) -> &[u8] { + pub(crate) fn to_bytes(&self) -> &'static [u8] { match self { - Self::PatriciaNode => "patricia_node:".as_bytes(), + Self::InnerNode => b"patricia_node", + Self::StorageLeaf => b"starknet_storage_leaf", + Self::StateTreeLeaf => b"contract_state", + Self::CompiledClassLeaf => b"contract_class_leaf", } } } -impl StorageKey { - pub(crate) fn with_prefix(&self, prefix: StoragePrefix) -> Self { - let mut prefix = prefix.to_bytes().to_vec(); - prefix.extend(&self.0); - StorageKey(prefix) - } -} - impl From for StorageKey { fn from(value: Felt) -> Self { StorageKey(value.to_bytes_be().to_vec()) @@ -55,6 +54,6 @@ impl From for StorageKey { } /// Returns a `StorageKey` from a prefix and a suffix. -pub(crate) fn create_db_key(prefix: &[u8], suffix: &[u8]) -> StorageKey { - StorageKey([prefix.to_vec(), b":".to_vec(), suffix.to_vec()].concat()) +pub(crate) fn create_db_key(prefix: StoragePrefix, suffix: &[u8]) -> StorageKey { + StorageKey([prefix.to_bytes().to_vec(), b":".to_vec(), suffix.to_vec()].concat()) }