Skip to content

Commit f50d827

Browse files
committed
starknet_committer,starknet_patricia: move db_layout
1 parent e9626b7 commit f50d827

File tree

8 files changed

+36
-42
lines changed

8 files changed

+36
-42
lines changed

crates/starknet_committer/src/db.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod db_layout;
21
#[cfg(any(feature = "testing", test))]
32
pub mod external_test_utils;
43
pub mod facts_db;

crates/starknet_committer/src/db/facts_db/db.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashMap;
33
use async_trait::async_trait;
44
use starknet_api::core::ContractAddress;
55
use starknet_api::hash::HashOutput;
6+
use starknet_patricia::db_layout::{NodeLayout, TrieType};
67
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
78
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FactNodeDeserializationContext;
89
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
@@ -14,10 +15,8 @@ use starknet_patricia_storage::map_storage::MapStorage;
1415
use starknet_patricia_storage::storage_trait::{DbHashMap, Storage};
1516

1617
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
17-
use crate::db::db_layout::NodeLayout;
1818
use crate::db::facts_db::types::{FactsDbInitialRead, FactsSubTree};
1919
use crate::db::forest_trait::{ForestReader, ForestWriter};
20-
use crate::db::index_db::leaves::TrieType;
2120
use crate::db::trie_traversal::{create_classes_trie, create_contracts_trie, create_storage_tries};
2221
use crate::forest::filled_forest::FilledForest;
2322
use crate::forest::forest_errors::ForestResult;

crates/starknet_committer/src/db/index_db/db.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@ use std::collections::HashMap;
22

33
use async_trait::async_trait;
44
use starknet_api::core::ContractAddress;
5-
use starknet_api::hash::HashOutput;
6-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
5+
use starknet_patricia::db_layout::{NodeLayout, TrieType};
76
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
87
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
9-
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
8+
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
109
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
1110
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
1211
use starknet_patricia_storage::errors::SerializationResult;
1312
use starknet_patricia_storage::storage_trait::{DbHashMap, Storage};
1413

1514
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
16-
use crate::db::db_layout::NodeLayout;
1715
use crate::db::facts_db::types::FactsDbInitialRead;
1816
use crate::db::forest_trait::{ForestReader, ForestWriter};
1917
use crate::db::index_db::leaves::{
2018
IndexLayoutCompiledClassHash,
2119
IndexLayoutContractState,
2220
IndexLayoutStarknetStorageValue,
23-
TrieType,
2421
};
25-
use crate::db::index_db::types::{IndexFilledNode, IndexLayoutSubTree, IndexNodeContext};
22+
use crate::db::index_db::types::{
23+
EmptyNodeData,
24+
IndexFilledNode,
25+
IndexLayoutSubTree,
26+
IndexNodeContext,
27+
};
2628
use crate::db::trie_traversal::{create_classes_trie, create_contracts_trie, create_storage_tries};
2729
use crate::forest::filled_forest::FilledForest;
2830
use crate::forest::forest_errors::ForestResult;
@@ -48,18 +50,14 @@ where
4850
L: HasStaticPrefix<KeyContext = TrieType>,
4951
TreeHashFunctionImpl: TreeHashFunction<L>,
5052
{
51-
type NodeData = ();
53+
type NodeData = EmptyNodeData;
5254
type NodeDbObject = IndexFilledNode<L>;
5355
type DeserializationContext = IndexNodeContext;
5456
type SubTree = IndexLayoutSubTree<'a>;
5557

5658
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext {
5759
trie_type
5860
}
59-
60-
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData> {
61-
node_db_object.0
62-
}
6361
}
6462

6563
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside

crates/starknet_committer/src/db/index_db/leaves.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::sync::LazyLock;
22

3-
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PATRICIA_KEY_UPPER_BOUND};
3+
use starknet_api::core::{ClassHash, Nonce, PATRICIA_KEY_UPPER_BOUND};
44
use starknet_api::hash::HashOutput;
5+
use starknet_patricia::db_layout::TrieType;
56
use starknet_patricia::patricia_merkle_tree::node_data::errors::LeafResult;
67
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
78
use starknet_patricia_storage::db_object::{
@@ -16,7 +17,6 @@ use starknet_types_core::felt::Felt;
1617
use crate::block_committer::input::StarknetStorageValue;
1718
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1819
use crate::patricia_merkle_tree::types::CompiledClassHash;
19-
2020
// Wrap the leaves types so that we can implement the [DBObject] trait differently in index
2121
// layout.
2222
#[derive(
@@ -46,23 +46,13 @@ static CONTRACTS_TREE_PREFIX: LazyLock<[u8; 32]> =
4646
static CLASSES_TREE_PREFIX: LazyLock<[u8; 32]> =
4747
LazyLock::new(|| (*FIRST_AVAILABLE_PREFIX_FELT + Felt::ONE).to_bytes_be());
4848

49-
// TODO(Ariel): Delete this enum and use `CommitmentType` instead.
50-
#[derive(Debug, PartialEq)]
51-
pub enum TrieType {
52-
ContractsTrie,
53-
ClassesTrie,
54-
StorageTrie(ContractAddress),
55-
}
56-
57-
impl TrieType {
58-
fn db_prefix(&self) -> DbKeyPrefix {
59-
match self {
60-
Self::ContractsTrie => DbKeyPrefix::new((&CONTRACTS_TREE_PREFIX[..]).into()),
61-
Self::ClassesTrie => DbKeyPrefix::new((&CLASSES_TREE_PREFIX[..]).into()),
62-
Self::StorageTrie(contract_address) => {
63-
let prefix = contract_address.to_bytes_be().to_vec();
64-
DbKeyPrefix::new(prefix.into())
65-
}
49+
fn db_prefix(trie_type: &TrieType) -> DbKeyPrefix {
50+
match trie_type {
51+
TrieType::ContractsTrie => DbKeyPrefix::new((&CONTRACTS_TREE_PREFIX[..]).into()),
52+
TrieType::ClassesTrie => DbKeyPrefix::new((&CLASSES_TREE_PREFIX[..]).into()),
53+
TrieType::StorageTrie(contract_address) => {
54+
let prefix = contract_address.to_bytes_be().to_vec();
55+
DbKeyPrefix::new(prefix.into())
6656
}
6757
}
6858
}
@@ -73,7 +63,7 @@ macro_rules! impl_has_static_prefix_for_index_layouts {
7363
impl HasStaticPrefix for $ty {
7464
type KeyContext = TrieType;
7565
fn get_static_prefix(key_context: &Self::KeyContext) -> DbKeyPrefix {
76-
key_context.db_prefix()
66+
db_prefix(key_context)
7767
}
7868
}
7969
)*

crates/starknet_committer/src/db/index_db/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl From<HashOutput> for EmptyNodeData {
3737
}
3838
}
3939

40-
#[derive(PartialEq, Debug)]
40+
#[derive(PartialEq, Debug, derive_more::Into)]
4141
pub struct IndexFilledNode<L: Leaf>(pub FilledNode<L, EmptyNodeData>);
4242

4343
pub struct IndexNodeContext {
@@ -148,7 +148,7 @@ pub struct IndexLayoutSubTree<'a> {
148148
}
149149

150150
impl<'a> SubTreeTrait<'a> for IndexLayoutSubTree<'a> {
151-
type NodeData = ();
151+
type NodeData = EmptyNodeData;
152152
type NodeDeserializeContext = IndexNodeContext;
153153

154154
fn create(

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt::Debug;
44

55
use starknet_api::core::ContractAddress;
66
use starknet_api::hash::HashOutput;
7+
use starknet_patricia::db_layout::{NodeLayout, TrieType};
78
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
89
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
910
BinaryData,
@@ -33,8 +34,6 @@ use crate::block_committer::input::{
3334
ReaderConfig,
3435
StarknetStorageValue,
3536
};
36-
use crate::db::db_layout::NodeLayout;
37-
use crate::db::index_db::leaves::TrieType;
3837
use crate::forest::forest_errors::{ForestError, ForestResult};
3938
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
4039
use crate::patricia_merkle_tree::tree::OriginalSkeletonTrieConfig;

crates/starknet_committer/src/db/db_layout.rs renamed to crates/starknet_patricia/src/db_layout.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
use starknet_api::core::ContractAddress;
12
use starknet_api::hash::HashOutput;
2-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
3-
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
4-
use starknet_patricia::patricia_merkle_tree::traversal::SubTreeTrait;
53
use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
64

7-
use crate::db::index_db::leaves::TrieType;
5+
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
6+
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
7+
use crate::patricia_merkle_tree::traversal::SubTreeTrait;
8+
9+
// TODO(Ariel): Delete this enum and use `CommitmentType` instead.
10+
#[derive(Debug, PartialEq)]
11+
pub enum TrieType {
12+
ContractsTrie,
13+
ClassesTrie,
14+
StorageTrie(ContractAddress),
15+
}
816

917
/// Specifies the trie db layout.
1018
pub trait NodeLayout<'a, L: Leaf> {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod db_layout;
12
pub mod felt;
23
pub mod patricia_merkle_tree;

0 commit comments

Comments
 (0)