Skip to content

Commit

Permalink
build: impl db key to filled node (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware authored Apr 18, 2024
1 parent d3cc8be commit 482f825
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
30 changes: 28 additions & 2 deletions crates/committer/src/patricia_merkle_tree/filled_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ 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, SERIALIZE_HASH_BYTES,
LeafCompiledClassToSerialize, SerializeNode, COMPLIED_CLASS_PREFIX, INNER_NODE_PREFIX,
SERIALIZE_HASH_BYTES, STATE_TREE_LEAF_PREFIX, STORAGE_LEAF_PREFIX,
};
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::{StorageKey, StorageValue};
use crate::storage::storage_trait::{create_db_key, StorageKey, StorageValue};
use crate::types::Felt;

// TODO(Nimrod, 1/6/2024): Swap to starknet-types-core types once implemented.
Expand Down Expand Up @@ -193,4 +194,29 @@ impl FilledNode<LeafData> {
NodeData::Leaf(leaf_data) => leaf_data.serialize(),
}
}

/// Returns the suffix of the filled node, represented by its hash as a byte array.
#[allow(dead_code)]
pub(crate) fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
self.hash.0.as_bytes()
}

/// Returns the db key of the filled node - [prefix + b":" + suffix].
#[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::Leaf(LeafData::StorageValue(_)) => {
create_db_key(STORAGE_LEAF_PREFIX, &suffix)
}
NodeData::Leaf(LeafData::CompiledClassHash(_)) => {
create_db_key(COMPLIED_CLASS_PREFIX, &suffix)
}
NodeData::Leaf(LeafData::StateTreeTuple { .. }) => {
create_db_key(STATE_TREE_LEAF_PREFIX, &suffix)
}
}
}
}
6 changes: 6 additions & 0 deletions crates/committer/src/patricia_merkle_tree/serialized_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ 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
5 changes: 5 additions & 0 deletions crates/committer/src/storage/storage_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ impl From<Felt> for StorageKey {
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 482f825

Please sign in to comment.