Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/starknet_committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions crates/starknet_committer/src/db/facts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeImpl;
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
use starknet_patricia_storage::map_storage::MapStorage;
use starknet_patricia_storage::storage_trait::Storage;
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix, Storage};

use crate::block_committer::input::{
contract_address_into_node_index,
Expand All @@ -18,7 +18,7 @@ use crate::db::create_facts_tree::{
create_original_skeleton_tree,
create_original_skeleton_tree_and_get_previous_leaves,
};
use crate::db::forest_trait::{ForestReader, ForestWriter};
use crate::db::forest_trait::{ForestMetadata, ForestMetadataType, ForestReader, ForestWriter};
use crate::forest::filled_forest::FilledForest;
use crate::forest::forest_errors::{ForestError, ForestResult};
use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkeletonForest};
Expand All @@ -37,6 +37,9 @@ pub struct FactsDb<S: Storage> {
}

impl<S: Storage> FactsDb<S> {
pub const COMMITMENT_OFFSET_KEY: &[u8; 17] = b"commitment_offset";
pub const STATE_DIFF_HASH_PREFIX: &[u8; 15] = b"state_diff_hash";

pub fn new(storage: S) -> Self {
Self { storage }
}
Expand Down Expand Up @@ -163,3 +166,18 @@ impl<S: Storage> ForestWriter for FactsDb<S> {
filled_forest.write_to_storage(&mut self.storage).await
}
}

impl<S: Storage> ForestMetadata for FactsDb<S> {
/// Returns the db key for the metadata type.
/// The data keys in a facts DB are the result of a hash function; therefore, they are not
/// expected to collide with these metadata keys.
fn metadata_key(metadata_type: ForestMetadataType) -> DbKey {
match metadata_type {
ForestMetadataType::CommitmentOffset => DbKey(Self::COMMITMENT_OFFSET_KEY.to_vec()),
ForestMetadataType::StateDiffHash(block_number) => {
let state_diff_hash_key_prefix = DbKeyPrefix::new(Self::STATE_DIFF_HASH_PREFIX);
create_db_key(state_diff_hash_key_prefix, &block_number.0.to_be_bytes())
}
}
}
}
34 changes: 34 additions & 0 deletions crates/starknet_committer/src/db/forest_trait.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Option<DbValue>> {
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<DbValue> {
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> {
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_committer/src/forest/forest_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down
Loading