Skip to content

Commit

Permalink
refactor: remove dependency between storage and starknet prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Jul 17, 2024
1 parent 1d383c5 commit 9658446
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -76,9 +76,11 @@ impl<L: Leaf> DBObject for FilledNode<L> {
}
}

fn get_prefix(&self) -> StoragePrefix {
fn get_prefix(&self) -> Vec<u8> {
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(),
}
}
Expand Down
26 changes: 13 additions & 13 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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<u8> {
StarknetPrefix::StorageLeaf.to_storage_prefix()
}
}

Expand All @@ -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<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

Expand All @@ -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<u8> {
StarknetPrefix::StateTreeLeaf.to_storage_prefix()
}
}

Expand All @@ -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<u8> {
StarknetPrefix::StorageLeaf.to_storage_prefix()
}
}

Expand All @@ -79,8 +79,8 @@ impl Deserializable for CompiledClassHash {
Ok(Self::from_hex(hash_as_hex)?)
}

fn prefix() -> StoragePrefix {
StoragePrefix::CompiledClassLeaf
fn prefix() -> Vec<u8> {
StarknetPrefix::CompiledClassLeaf.to_storage_prefix()
}
}

Expand Down Expand Up @@ -109,8 +109,8 @@ impl Deserializable for ContractState {
})
}

fn prefix() -> StoragePrefix {
StoragePrefix::StateTreeLeaf
fn prefix() -> Vec<u8> {
StarknetPrefix::StateTreeLeaf.to_storage_prefix()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 4 additions & 11 deletions crates/committer/src/storage/db_object.rs
Original file line number Diff line number Diff line change
@@ -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<u8>;

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

Expand All @@ -26,5 +19,5 @@ pub trait Deserializable: Sized {
fn deserialize(value: &StorageValue) -> Result<Self, DeserializationError>;

/// The prefix used to store in DB.
fn prefix() -> StoragePrefix;
fn prefix() -> Vec<u8>;
}
4 changes: 2 additions & 2 deletions crates/committer/src/storage/errors.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<u8>),
#[error(transparent)]
StringConversionError(#[from] std::str::Utf8Error),
#[error(transparent)]
Expand Down
13 changes: 9 additions & 4 deletions crates/committer/src/storage/storage_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ pub trait Storage: From<HashMap<StorageKey, StorageValue>> {
fn delete(&mut self, key: &StorageKey) -> Option<StorageValue>;
}

// TODO(Aviv, 17/07/2024); Split between Storage and node specific implementation.
#[derive(Clone, Debug)]
pub enum StoragePrefix {
pub enum StarknetPrefix {
InnerNode,
StorageLeaf,
StateTreeLeaf,
CompiledClassLeaf,
}

/// 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",
Expand All @@ -48,6 +49,10 @@ impl StoragePrefix {
Self::CompiledClassLeaf => b"contract_class_leaf",
}
}

pub(crate) fn to_storage_prefix(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
}

impl From<Felt> for StorageKey {
Expand All @@ -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<u8>, suffix: &[u8]) -> StorageKey {
StorageKey([prefix, b":".to_vec(), suffix.to_vec()].concat())
}

0 comments on commit 9658446

Please sign in to comment.