Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
NodeData,
PathToBottom,
};
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext};
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext, HasStaticPrefix};
use starknet_patricia_storage::errors::{DeserializationError, SerializationError};
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue, Storage};
Expand Down Expand Up @@ -426,6 +427,14 @@ fn xor_hash(x: &[u8], y: &[u8]) -> Vec<u8> {
x.iter().zip(y.iter()).map(|(a, b)| a ^ b).collect()
}

fn db_key<L: Leaf>(
node: &FactDbFilledNode<L>,
key_context: &<L as HasStaticPrefix>::KeyContext,
) -> DbKey {
let suffix = node.hash.0.to_bytes_be();
node.get_db_key(key_context, &suffix)
}

/// Creates and serializes storage keys for different node types.
///
/// This function generates and serializes storage keys for various node types, including binary
Expand All @@ -445,17 +454,17 @@ pub(crate) fn test_node_db_key() -> String {
data: NodeData::Binary(BinaryData { left_data: hash, right_data: hash }),
hash,
};
let binary_node_key = binary_node.db_key(&dummy_contract_address).0;
let binary_node_key = db_key(&binary_node, &dummy_contract_address).0;

let edge_node: FactDbFilledNode<StarknetStorageValue> = FactDbFilledNode {
data: NodeData::Edge(EdgeData { bottom_data: hash, path_to_bottom: Default::default() }),
hash,
};

let edge_node_key = edge_node.db_key(&dummy_contract_address).0;
let edge_node_key = db_key(&edge_node, &dummy_contract_address).0;

let storage_leaf = FactDbFilledNode { data: NodeData::Leaf(StarknetStorageValue(zero)), hash };
let storage_leaf_key = storage_leaf.db_key(&dummy_contract_address).0;
let storage_leaf_key = db_key(&storage_leaf, &dummy_contract_address).0;

let state_tree_leaf = FactDbFilledNode {
data: NodeData::Leaf(ContractState {
Expand All @@ -465,11 +474,11 @@ pub(crate) fn test_node_db_key() -> String {
}),
hash,
};
let state_tree_leaf_key = state_tree_leaf.db_key(&EmptyKeyContext).0;
let state_tree_leaf_key = db_key(&state_tree_leaf, &EmptyKeyContext).0;

let compiled_class_leaf =
FactDbFilledNode { data: NodeData::Leaf(CompiledClassHash(zero)), hash };
let compiled_class_leaf_key = compiled_class_leaf.db_key(&EmptyKeyContext).0;
let compiled_class_leaf_key = db_key(&compiled_class_leaf, &EmptyKeyContext).0;

// Store keys in a HashMap.
let mut map: HashMap<String, Vec<u8>> = HashMap::new();
Expand Down Expand Up @@ -544,7 +553,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Store the binary node in the storage.
rust_fact_storage
.set(
binary_rust.db_key(&dummy_contract_address),
db_key(&binary_rust, &dummy_contract_address),
binary_rust.serialize().map_err(|error| {
PythonTestError::SpecificError(CommitterSpecificTestError::Serialization(error))
})?,
Expand Down Expand Up @@ -578,7 +587,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Store the edge node in the storage.
rust_fact_storage
.set(
edge_rust.db_key(&dummy_contract_address),
db_key(&edge_rust, &dummy_contract_address),
edge_rust.serialize().map_err(|error| {
PythonTestError::SpecificError(CommitterSpecificTestError::Serialization(error))
})?,
Expand All @@ -601,7 +610,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Store the storage leaf node in the storage.
rust_fact_storage
.set(
storage_leaf_rust.db_key(&dummy_contract_address),
db_key(&storage_leaf_rust, &dummy_contract_address),
storage_leaf_rust.serialize().map_err(|error| {
PythonTestError::SpecificError(CommitterSpecificTestError::Serialization(error))
})?,
Expand Down Expand Up @@ -633,7 +642,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Store the contract state leaf node in the storage.
rust_fact_storage
.set(
contract_state_leaf_rust.db_key(&EmptyKeyContext),
db_key(&contract_state_leaf_rust, &EmptyKeyContext),
contract_state_leaf_rust.serialize().map_err(|error| {
PythonTestError::SpecificError(CommitterSpecificTestError::Serialization(error))
})?,
Expand All @@ -657,7 +666,7 @@ async fn test_storage_node(data: HashMap<String, String>) -> CommitterPythonTest
// Store the compiled class leaf node in the storage.
rust_fact_storage
.set(
compiled_class_leaf_rust.db_key(&EmptyKeyContext),
db_key(&compiled_class_leaf_rust, &EmptyKeyContext),
compiled_class_leaf_rust.serialize().map_err(|error| {
PythonTestError::SpecificError(CommitterSpecificTestError::Serialization(error))
})?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_patricia_storage::db_object::{
HasStaticPrefix,
};
use starknet_patricia_storage::errors::{DeserializationError, SerializationResult};
use starknet_patricia_storage::storage_trait::{DbKey, DbKeyPrefix, DbValue};
use starknet_patricia_storage::storage_trait::{DbKeyPrefix, DbValue};
use starknet_types_core::felt::Felt;

use crate::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
Expand Down Expand Up @@ -44,18 +44,6 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
}
}

// TODO(Ariel, 14/12/2025): generalize this to both layouts (e.g. via a new trait). ATM db_key is
// only used in the filled tree serialize function, which assumes facts layout.
impl<L: Leaf> FactDbFilledNode<L> {
pub fn suffix(&self) -> [u8; SERIALIZE_HASH_BYTES] {
self.hash.0.to_bytes_be()
}

pub fn db_key(&self, key_context: &<L as HasStaticPrefix>::KeyContext) -> DbKey {
self.get_db_key(key_context, &self.suffix())
}
}

impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
type KeyContext = <L as HasStaticPrefix>::KeyContext;
Expand Down
Loading