Skip to content

Commit 96db662

Browse files
committed
starknet_committer: db layout trait
1 parent d34cbc2 commit 96db662

File tree

5 files changed

+57
-33
lines changed

5 files changed

+57
-33
lines changed

crates/starknet_committer/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod db_layout;
12
#[cfg(any(feature = "testing", test))]
23
pub mod external_test_utils;
34
pub mod facts_db;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use starknet_api::core::ContractAddress;
2+
use starknet_patricia::db_layout::NodeLayoutFor;
3+
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
4+
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
5+
6+
use crate::block_committer::input::StarknetStorageValue;
7+
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
8+
use crate::patricia_merkle_tree::types::CompiledClassHash;
9+
10+
/// A trait that specifies, for every leaf type, its db representation and the node layout for it.
11+
pub trait DbLayout {
12+
type ContractStateDbLeaf: Leaf + HasStaticPrefix<KeyContext = EmptyKeyContext>;
13+
14+
type CompiledClassHashDbLeaf: Leaf + HasStaticPrefix<KeyContext = EmptyKeyContext>;
15+
16+
type StarknetStorageValueDbLeaf: Leaf + HasStaticPrefix<KeyContext = ContractAddress>;
17+
18+
type NodeLayout: NodeLayoutFor<ContractState, DbLeaf = Self::ContractStateDbLeaf>
19+
+ NodeLayoutFor<CompiledClassHash, DbLeaf = Self::CompiledClassHashDbLeaf>
20+
+ NodeLayoutFor<StarknetStorageValue, DbLeaf = Self::StarknetStorageValueDbLeaf>;
21+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use starknet_patricia_storage::map_storage::MapStorage;
1414
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1515

1616
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
17+
use crate::db::db_layout::DbLayout;
1718
use crate::db::facts_db::types::{FactsDbInitialRead, FactsSubTree};
1819
use crate::db::forest_trait::{read_forest, serialize_forest, ForestReader, ForestWriter};
1920
use crate::forest::filled_forest::FilledForest;
@@ -64,6 +65,13 @@ impl NodeLayoutFor<CompiledClassHash> for FactsNodeLayout {
6465
type DbLeaf = CompiledClassHash;
6566
}
6667

68+
impl DbLayout for FactsNodeLayout {
69+
type ContractStateDbLeaf = ContractState;
70+
type CompiledClassHashDbLeaf = CompiledClassHash;
71+
type StarknetStorageValueDbLeaf = StarknetStorageValue;
72+
type NodeLayout = FactsNodeLayout;
73+
}
74+
6775
pub struct FactsDb<S: Storage> {
6876
// TODO(Yoav): Define StorageStats trait and impl it here. Then, make the storage field
6977
// private.

crates/starknet_committer/src/db/forest_trait.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use std::collections::HashMap;
33
use async_trait::async_trait;
44
use serde::{Deserialize, Serialize};
55
use starknet_api::core::ContractAddress;
6-
use starknet_patricia::db_layout::NodeLayoutFor;
76
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
87
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
98
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
10-
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
9+
use starknet_patricia_storage::db_object::EmptyKeyContext;
1110
use starknet_patricia_storage::errors::SerializationResult;
1211
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, DbValue, Storage};
1312

1413
use crate::block_committer::input::{InputContext, ReaderConfig, StarknetStorageValue};
14+
use crate::db::db_layout::DbLayout;
1515
use crate::db::facts_db::types::FactsDbInitialRead;
1616
use crate::db::serde_db_utils::DbBlockNumber;
1717
use crate::db::trie_traversal::{create_classes_trie, create_contracts_trie, create_storage_tries};
@@ -81,30 +81,24 @@ pub(crate) async fn read_forest<'a, S, Layout>(
8181
) -> ForestResult<(OriginalSkeletonForest<'a>, HashMap<NodeIndex, ContractState>)>
8282
where
8383
S: Storage,
84-
Layout: NodeLayoutFor<StarknetStorageValue>
85-
+ NodeLayoutFor<ContractState>
86-
+ NodeLayoutFor<CompiledClassHash>,
87-
<Layout as NodeLayoutFor<StarknetStorageValue>>::DbLeaf:
88-
HasStaticPrefix<KeyContext = ContractAddress>,
89-
<Layout as NodeLayoutFor<ContractState>>::DbLeaf: HasStaticPrefix<KeyContext = EmptyKeyContext>,
90-
<Layout as NodeLayoutFor<CompiledClassHash>>::DbLeaf:
91-
HasStaticPrefix<KeyContext = EmptyKeyContext>,
84+
Layout: DbLayout,
9285
{
93-
let (contracts_trie, original_contracts_trie_leaves) = create_contracts_trie::<Layout>(
94-
storage,
95-
context.0.contracts_trie_root_hash,
96-
forest_sorted_indices.contracts_trie_sorted_indices,
97-
)
98-
.await?;
99-
let storage_tries = create_storage_tries::<Layout>(
86+
let (contracts_trie, original_contracts_trie_leaves) =
87+
create_contracts_trie::<Layout::NodeLayout>(
88+
storage,
89+
context.0.contracts_trie_root_hash,
90+
forest_sorted_indices.contracts_trie_sorted_indices,
91+
)
92+
.await?;
93+
let storage_tries = create_storage_tries::<Layout::NodeLayout>(
10094
storage,
10195
storage_updates,
10296
&original_contracts_trie_leaves,
10397
&config,
10498
&forest_sorted_indices.storage_tries_sorted_indices,
10599
)
106100
.await?;
107-
let classes_trie = create_classes_trie::<Layout>(
101+
let classes_trie = create_classes_trie::<Layout::NodeLayout>(
108102
storage,
109103
classes_updates,
110104
context.0.classes_trie_root_hash,
@@ -120,31 +114,23 @@ where
120114
}
121115

122116
/// Helper function containing layout-common write logic.
123-
pub(crate) fn serialize_forest<Layout>(
117+
pub(crate) fn serialize_forest<Layout: DbLayout>(
124118
filled_forest: &FilledForest,
125-
) -> SerializationResult<DbHashMap>
126-
where
127-
Layout: NodeLayoutFor<StarknetStorageValue>
128-
+ NodeLayoutFor<ContractState>
129-
+ NodeLayoutFor<CompiledClassHash>,
130-
<Layout as NodeLayoutFor<StarknetStorageValue>>::DbLeaf:
131-
HasStaticPrefix<KeyContext = ContractAddress>,
132-
<Layout as NodeLayoutFor<ContractState>>::DbLeaf: HasStaticPrefix<KeyContext = EmptyKeyContext>,
133-
<Layout as NodeLayoutFor<CompiledClassHash>>::DbLeaf:
134-
HasStaticPrefix<KeyContext = EmptyKeyContext>,
135-
{
119+
) -> SerializationResult<DbHashMap> {
136120
let mut serialized_forest = DbHashMap::new();
137121

138122
// Storage tries.
139123
for (contract_address, tree) in &filled_forest.storage_tries {
140-
serialized_forest.extend(tree.serialize::<Layout>(contract_address)?);
124+
serialized_forest.extend(tree.serialize::<Layout::NodeLayout>(contract_address)?);
141125
}
142126

143127
// Contracts trie.
144-
serialized_forest.extend(filled_forest.contracts_trie.serialize::<Layout>(&EmptyKeyContext)?);
128+
serialized_forest
129+
.extend(filled_forest.contracts_trie.serialize::<Layout::NodeLayout>(&EmptyKeyContext)?);
145130

146131
// Classes trie.
147-
serialized_forest.extend(filled_forest.classes_trie.serialize::<Layout>(&EmptyKeyContext)?);
132+
serialized_forest
133+
.extend(filled_forest.classes_trie.serialize::<Layout::NodeLayout>(&EmptyKeyContext)?);
148134

149135
Ok(serialized_forest)
150136
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use starknet_patricia_storage::errors::SerializationResult;
1313
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1414

1515
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
16+
use crate::db::db_layout::DbLayout;
1617
use crate::db::facts_db::types::FactsDbInitialRead;
1718
use crate::db::forest_trait::{read_forest, serialize_forest, ForestReader, ForestWriter};
1819
use crate::db::index_db::leaves::{
@@ -83,6 +84,13 @@ impl NodeLayoutFor<CompiledClassHash> for IndexNodeLayout {
8384
type DbLeaf = IndexLayoutCompiledClassHash;
8485
}
8586

87+
impl DbLayout for IndexNodeLayout {
88+
type ContractStateDbLeaf = IndexLayoutContractState;
89+
type CompiledClassHashDbLeaf = IndexLayoutCompiledClassHash;
90+
type StarknetStorageValueDbLeaf = IndexLayoutStarknetStorageValue;
91+
type NodeLayout = IndexNodeLayout;
92+
}
93+
8694
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside
8795
// create_xxx_trie.
8896
#[async_trait]

0 commit comments

Comments
 (0)