diff --git a/Cargo.lock b/Cargo.lock index 23361f5299d..bacdc29ce4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12281,6 +12281,7 @@ dependencies = [ name = "starknet_committer" version = "0.17.0-rc.0" dependencies = [ + "async-trait", "csv", "ethnum", "hex", diff --git a/crates/starknet_committer/Cargo.toml b/crates/starknet_committer/Cargo.toml index ce572b00e0b..54761b4c175 100644 --- a/crates/starknet_committer/Cargo.toml +++ b/crates/starknet_committer/Cargo.toml @@ -10,6 +10,7 @@ description = "Computes and manages Starknet state." testing = ["starknet_patricia/testing"] [dependencies] +async-trait.workspace = true csv.workspace = true ethnum.workspace = true hex.workspace = true diff --git a/crates/starknet_committer/src/db/forest_trait.rs b/crates/starknet_committer/src/db/forest_trait.rs index b6f55741411..77c6c15d2df 100644 --- a/crates/starknet_committer/src/db/forest_trait.rs +++ b/crates/starknet_committer/src/db/forest_trait.rs @@ -1,10 +1,13 @@ use std::collections::HashMap; use std::future::Future; +use async_trait::async_trait; +use starknet_api::block::BlockNumber; use starknet_api::core::ContractAddress; use starknet_api::hash::HashOutput; use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications; use starknet_patricia::patricia_merkle_tree::types::NodeIndex; +use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue, Storage}; use crate::block_committer::input::{ConfigImpl, StarknetStorageValue}; use crate::forest::filled_forest::FilledForest; @@ -13,6 +16,37 @@ use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkele use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; use crate::patricia_merkle_tree::types::CompiledClassHash; +pub enum ForestMetadataType { + CommitmentOffset, + StateDiffHash(BlockNumber), +} + +#[async_trait] +pub trait ForestMetadata { + /// Returns the db key for the metadata type. + fn metadata_key(metadata_type: ForestMetadataType) -> DbKey; + + /// Reads the metadata from the storage. + async fn read_metadata( + &self, + storage: &mut impl Storage, + metadata_type: ForestMetadataType, + ) -> ForestResult> { + let db_key = Self::metadata_key(metadata_type); + Ok(storage.get(&db_key).await?) + } + + /// Adds the metadata to updates map. Returns the previous value if it existed. + fn insert_metadata( + updates: &mut DbHashMap, + metadata_type: ForestMetadataType, + value: DbValue, + ) -> Option { + let db_key = Self::metadata_key(metadata_type); + updates.insert(db_key, value) + } +} + /// Trait for reading an original skeleton forest from some storage. /// The implementation may depend on the underlying storage layout. pub trait ForestReader<'a> { diff --git a/crates/starknet_committer/src/forest/forest_errors.rs b/crates/starknet_committer/src/forest/forest_errors.rs index c4d52b65ef9..11dd1e2f64a 100644 --- a/crates/starknet_committer/src/forest/forest_errors.rs +++ b/crates/starknet_committer/src/forest/forest_errors.rs @@ -2,6 +2,7 @@ use starknet_api::core::ContractAddress; use starknet_patricia::patricia_merkle_tree::filled_tree::errors::FilledTreeError; use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError; use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::errors::UpdatedSkeletonTreeError; +use starknet_patricia_storage::storage_trait::PatriciaStorageError; use thiserror::Error; use tokio::task::JoinError; @@ -12,6 +13,8 @@ pub enum ForestError { #[error(transparent)] OriginalSkeleton(#[from] OriginalSkeletonTreeError), #[error(transparent)] + PatriciaStorage(#[from] PatriciaStorageError), + #[error(transparent)] UpdatedSkeleton(#[from] UpdatedSkeletonTreeError), #[error("Couldn't create Classes Trie: {0}")] ClassesTrie(#[source] FilledTreeError),