Skip to content

starknet_committer: add alias for map storage #8316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
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
5 changes: 2 additions & 3 deletions crates/starknet_committer/src/block_committer/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::collections::HashMap;

use starknet_api::core::{ClassHash, ContractAddress, Nonce};
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use tracing::{info, warn};

use crate::block_committer::errors::BlockCommitmentError;
Expand All @@ -25,7 +24,7 @@ type BlockCommitmentResult<T> = Result<T, BlockCommitmentError>;

pub async fn commit_block(
input: Input<ConfigImpl>,
storage: &mut HashMap<DbKey, DbValue>,
storage: &mut MapStorage,
) -> BlockCommitmentResult<FilledForest> {
let (mut storage_tries_indices, mut contracts_trie_indices, mut classes_trie_indices) =
get_all_modified_indices(&input.state_diff);
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_committer/src/forest/skeleton_forest_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use starknet_patricia::patricia_merkle_tree::external_test_utils::{
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices, SubTreeHeight};
use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;
use tracing::level_filters::LevelFilter;
Expand Down Expand Up @@ -292,7 +292,7 @@ pub(crate) fn create_contract_state_leaf_entry(val: u128) -> (DbKey, DbValue) {
)]
fn test_create_original_skeleton_forest(
#[case] input: Input<ConfigImpl>,
#[case] mut storage: HashMap<DbKey, DbValue>,
#[case] mut storage: MapStorage,
#[case] expected_forest: OriginalSkeletonForest<'_>,
#[case] expected_original_contracts_trie_leaves: HashMap<ContractAddress, ContractState>,
#[case] expected_storage_tries_sorted_indices: HashMap<u128, Vec<u128>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::collections::HashMap;

use starknet_committer::block_committer::commit::commit_block;
use starknet_committer::block_committer::input::Config;
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use tracing::info;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::reload::Handle;
Expand Down Expand Up @@ -34,7 +33,7 @@ pub async fn parse_and_commit(
commit(input, output_path, storage).await;
}

pub async fn commit(input: InputImpl, output_path: String, mut storage: HashMap<DbKey, DbValue>) {
pub async fn commit(input: InputImpl, output_path: String, mut storage: MapStorage) {
let serialized_filled_forest = SerializedForest(
commit_block(input, &mut storage).await.expect("Failed to commit the given block."),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet_committer::block_committer::input::{
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;

Expand All @@ -21,7 +22,7 @@ pub type InputImpl = Input<ConfigImpl>;
#[derive(Debug, PartialEq)]
pub struct CommitterInputImpl {
pub input: InputImpl,
pub storage: HashMap<DbKey, DbValue>,
pub storage: MapStorage,
}

impl TryFrom<RawInput> for CommitterInputImpl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::errors::DeserializationError;
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use starknet_patricia_storage::storage_trait::{DbKey, DbValue, Storage};
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Pedersen, StarkHash};
Expand Down Expand Up @@ -387,7 +387,7 @@ generate_storage_map_xor_hasher!(
generate_storage_map_xor_hasher!(hash_address_to_class_hash, ContractAddress, ClassHash);
generate_storage_map_xor_hasher!(hash_address_to_nonce, ContractAddress, Nonce);

fn hash_storage(storage: &HashMap<DbKey, DbValue>) -> (Vec<u8>, Vec<u8>) {
fn hash_storage(storage: &MapStorage) -> (Vec<u8>, Vec<u8>) {
let mut keys_hash = vec![0; 32];
let mut values_hash = vec![0; 32];
for (key, value) in storage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use starknet_committer::block_committer::input::{ConfigImpl, Input, StarknetStor
use starknet_committer::hash_function::hash::TreeHashFunctionImpl;
use starknet_committer::patricia_merkle_tree::tree::OriginalSkeletonStorageTrieConfig;
use starknet_patricia::patricia_merkle_tree::external_test_utils::single_tree_flow_test;
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use tempfile::NamedTempFile;

use super::utils::parse_from_python::parse_input_single_storage_tree_flow_test;
Expand Down Expand Up @@ -41,7 +40,7 @@ impl<'de> Deserialize<'de> for FactMap {
}

// TODO(Nimrod): Delete this struct and use `CommitterInputImpl` instead.
struct CommitterInput(Input<ConfigImpl>, HashMap<DbKey, DbValue>);
struct CommitterInput(Input<ConfigImpl>, MapStorage);

impl<'de> Deserialize<'de> for CommitterInput {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet_committer::block_committer::input::StarknetStorageValue;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;

Expand All @@ -14,7 +15,7 @@ use crate::committer_cli::parse_input::raw_input::RawStorageEntry;

pub struct TreeFlowInput {
pub leaf_modifications: LeafModifications<StarknetStorageValue>,
pub storage: HashMap<DbKey, DbValue>,
pub storage: MapStorage,
pub root_hash: HashOutput,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex};

use async_recursion::async_recursion;
use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_patricia_storage::map_storage::MapStorage;

use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
Expand Down Expand Up @@ -42,7 +42,7 @@ pub trait FilledTree<L: Leaf>: Sized + Send {
/// Serializes the current state of the tree into a hashmap,
/// where each key-value pair corresponds
/// to a storage key and its serialized storage value.
fn serialize(&self) -> HashMap<DbKey, DbValue>;
fn serialize(&self) -> MapStorage;

fn get_root_hash(&self) -> HashOutput;
}
Expand Down Expand Up @@ -363,7 +363,7 @@ impl<L: Leaf + 'static> FilledTree<L> for FilledTreeImpl<L> {
})
}

fn serialize(&self) -> HashMap<DbKey, DbValue> {
fn serialize(&self) -> MapStorage {
// This function iterates over each node in the tree, using the node's `db_key` as the
// hashmap key and the result of the node's `serialize` method as the value.
self.get_all_nodes().values().map(|node| (node.db_key(), node.serialize())).collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ethnum::U256;
use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_patricia_storage::db_object::DBObject;
use starknet_patricia_storage::map_storage::BorrowedMapStorage;
use starknet_patricia_storage::map_storage::{BorrowedMapStorage, MapStorage};
use starknet_patricia_storage::storage_trait::{DbKey, DbValue};
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -202,7 +202,7 @@ use crate::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices, SubTreeHe
SubTreeHeight::new(4),
)]
fn test_create_tree(
#[case] mut storage: HashMap<DbKey, DbValue>,
#[case] mut storage: MapStorage,
#[case] leaf_modifications: LeafModifications<MockLeaf>,
#[case] root_hash: HashOutput,
#[case] expected_skeleton_nodes: HashMap<NodeIndex, OriginalSkeletonNode>,
Expand Down
8 changes: 5 additions & 3 deletions crates/starknet_patricia_storage/src/map_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ use std::collections::HashMap;
use serde::Serialize;

use crate::storage_trait::{DbKey, DbValue, Storage};
// TODO(Nimrod): Rename to 'BorrowedMapStorage' and define a type for HashMap<DbKey, DbValue>.

pub type MapStorage = HashMap<DbKey, DbValue>;

#[derive(Serialize, Debug)]
pub struct BorrowedMapStorage<'a> {
pub storage: &'a mut HashMap<DbKey, DbValue>,
pub storage: &'a mut MapStorage,
}

impl Storage for BorrowedMapStorage<'_> {
fn set(&mut self, key: DbKey, value: DbValue) -> Option<DbValue> {
self.storage.insert(key, value)
}

fn mset(&mut self, key_to_value: HashMap<DbKey, DbValue>) {
fn mset(&mut self, key_to_value: MapStorage) {
self.storage.extend(key_to_value);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_patricia_storage/src/storage_trait.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;

use serde::{Serialize, Serializer};
use starknet_types_core::felt::Felt;

use crate::map_storage::MapStorage;

#[derive(Debug, Eq, Hash, PartialEq)]
#[cfg_attr(any(test, feature = "testing"), derive(Clone))]
pub struct DbKey(pub Vec<u8>);
Expand All @@ -24,7 +24,7 @@ pub trait Storage {
fn mget(&self, keys: &[DbKey]) -> Vec<Option<&DbValue>>;

/// Sets values in storage.
fn mset(&mut self, key_to_value: HashMap<DbKey, DbValue>);
fn mset(&mut self, key_to_value: MapStorage);

/// Deletes value from storage and returns its value if it exists. Returns None if not.
fn delete(&mut self, key: &DbKey) -> Option<DbValue>;
Expand Down
Loading