Skip to content

Commit dd67d8d

Browse files
committed
starknet_committer: define trait ForestMetadata
1 parent af7a564 commit dd67d8d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

crates/starknet_committer/src/db/forest_trait.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::collections::HashMap;
22
use std::future::Future;
33

4+
use starknet_api::block::BlockNumber;
45
use starknet_api::core::ContractAddress;
56
use starknet_api::hash::HashOutput;
67
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
78
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
9+
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue, Storage};
810

911
use crate::block_committer::input::{ConfigImpl, StarknetStorageValue};
1012
use crate::forest::filled_forest::FilledForest;
@@ -13,6 +15,39 @@ use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkele
1315
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1416
use crate::patricia_merkle_tree::types::CompiledClassHash;
1517

18+
pub enum ForestMetadataType {
19+
CommitmentOffset,
20+
StateDiffHash(BlockNumber),
21+
}
22+
23+
pub trait ForestMetadata {
24+
/// Returns the db key for the metadata type.
25+
fn metadata_key(metadata_type: ForestMetadataType) -> DbKey;
26+
27+
/// Reads the metadata from the storage.
28+
fn read_metadata(
29+
&self,
30+
storage: &mut impl Storage,
31+
metadata_type: ForestMetadataType,
32+
) -> impl Future<Output = ForestResult<Option<DbValue>>> + Send {
33+
async {
34+
let db_key = Self::metadata_key(metadata_type);
35+
Ok(storage.get(&db_key).await?)
36+
}
37+
}
38+
39+
/// Sets the metadata in the storage. Returns the previous value if it existed.
40+
fn set_metadata(
41+
updates: &mut DbHashMap,
42+
metadata_type: ForestMetadataType,
43+
value: DbValue,
44+
) -> Option<DbValue> {
45+
let db_key = Self::metadata_key(metadata_type);
46+
updates.insert(db_key, value)
47+
}
48+
}
49+
50+
1651
/// Trait for reading an original skeleton forest from some storage.
1752
/// The implementation may depend on the underlying storage layout.
1853
pub trait ForestReader<'a> {

crates/starknet_committer/src/forest/forest_errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use starknet_api::core::ContractAddress;
22
use starknet_patricia::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
33
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
44
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::errors::UpdatedSkeletonTreeError;
5+
use starknet_patricia_storage::storage_trait::PatriciaStorageError;
56
use thiserror::Error;
67
use tokio::task::JoinError;
78

@@ -12,6 +13,8 @@ pub enum ForestError {
1213
#[error(transparent)]
1314
OriginalSkeleton(#[from] OriginalSkeletonTreeError),
1415
#[error(transparent)]
16+
PatriciaStorage(#[from] PatriciaStorageError),
17+
#[error(transparent)]
1518
UpdatedSkeleton(#[from] UpdatedSkeletonTreeError),
1619
#[error("Couldn't create Classes Trie: {0}")]
1720
ClassesTrie(#[source] FilledTreeError),

0 commit comments

Comments
 (0)