Skip to content

Commit

Permalink
refactor: removed duplicate definitions of storage prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Apr 21, 2024
1 parent 3da38c8 commit f263a47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
15 changes: 8 additions & 7 deletions crates/committer/src/patricia_merkle_tree/filled_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use crate::patricia_merkle_tree::errors::FilledTreeError;
use crate::patricia_merkle_tree::filled_tree::FilledTreeResult;
use crate::patricia_merkle_tree::original_skeleton_tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::serialized_node::{
LeafCompiledClassToSerialize, SerializeNode, COMPLIED_CLASS_PREFIX, INNER_NODE_PREFIX,
SERIALIZE_HASH_BYTES, STATE_TREE_LEAF_PREFIX, STORAGE_LEAF_PREFIX,
LeafCompiledClassToSerialize, SerializeNode, SERIALIZE_HASH_BYTES,
};
use crate::patricia_merkle_tree::serialized_node::{BINARY_BYTES, EDGE_BYTES, EDGE_PATH_BYTES};
use crate::patricia_merkle_tree::types::{EdgeData, LeafDataTrait};
use crate::patricia_merkle_tree::types::{EdgePath, EdgePathLength, PathToBottom};
use crate::storage::storage_trait::{create_db_key, StorageKey, StorageValue};
use crate::storage::storage_trait::{StorageKey, StoragePrefix, StorageValue};
use crate::types::Felt;

// TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented.
Expand Down Expand Up @@ -207,15 +206,17 @@ impl FilledNode<LeafData> {
let suffix = self.suffix();

match &self.data {
NodeData::Binary(_) | NodeData::Edge(_) => create_db_key(INNER_NODE_PREFIX, &suffix),
NodeData::Binary(_) | NodeData::Edge(_) => {
StorageKey::from_prefix_and_suffix(StoragePrefix::InnerNode, &suffix)
}
NodeData::Leaf(LeafData::StorageValue(_)) => {
create_db_key(STORAGE_LEAF_PREFIX, &suffix)
StorageKey::from_prefix_and_suffix(StoragePrefix::StorageLeaf, &suffix)
}
NodeData::Leaf(LeafData::CompiledClassHash(_)) => {
create_db_key(COMPLIED_CLASS_PREFIX, &suffix)
StorageKey::from_prefix_and_suffix(StoragePrefix::CompiledClassLeaf, &suffix)
}
NodeData::Leaf(LeafData::StateTreeTuple { .. }) => {
create_db_key(STATE_TREE_LEAF_PREFIX, &suffix)
StorageKey::from_prefix_and_suffix(StoragePrefix::StateTreeLeaf, &suffix)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ impl OriginalSkeletonTreeImpl {
) -> OriginalSkeletonTreeResult<Vec<FilledNode<LeafData>>> {
let mut subtrees_roots = vec![];
for subtree in subtrees.iter() {
let key =
StorageKey::from(subtree.root_hash.0).with_prefix(StoragePrefix::PatriciaNode);
let key = StorageKey::from(subtree.root_hash.0).with_prefix(StoragePrefix::InnerNode);
let val = storage.get(&key).ok_or(StorageError::MissingKey(key))?;
subtrees_roots.push(FilledNode::deserialize(
&StorageKey::from(subtree.root_hash.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fn create_32_bytes_entry(simple_val: u8) -> Vec<u8> {
}

fn create_patricia_key(val: u8) -> StorageKey {
StorageKey(create_32_bytes_entry(val)).with_prefix(StoragePrefix::PatriciaNode)
StorageKey(create_32_bytes_entry(val)).with_prefix(StoragePrefix::InnerNode)
}

fn create_binary_val(left: u8, right: u8) -> StorageValue {
Expand Down
6 changes: 0 additions & 6 deletions crates/committer/src/patricia_merkle_tree/serialized_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,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 {
Expand Down
22 changes: 14 additions & 8 deletions crates/committer/src/storage/storage_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,38 @@ 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] {
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();
let mut prefix = [prefix.to_bytes().to_vec(), b":".to_vec()].concat();
prefix.extend(&self.0);
StorageKey(prefix)
}

pub(crate) fn from_prefix_and_suffix(prefix: StoragePrefix, suffix: &[u8]) -> Self {
Self(suffix.to_vec()).with_prefix(prefix)
}
}

impl From<Felt> for StorageKey {
fn from(value: Felt) -> Self {
StorageKey(value.to_bytes_be().to_vec())
}
}

/// 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())
}

0 comments on commit f263a47

Please sign in to comment.