Skip to content

Commit 2d3d417

Browse files
committed
starknet_committer,starknet_patricia: move node_serde to facts db
1 parent 0bed88b commit 2d3d417

File tree

12 files changed

+86
-84
lines changed

12 files changed

+86
-84
lines changed

crates/starknet_committer/src/block_committer/random_structs.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use starknet_patricia::patricia_merkle_tree::external_test_utils::{
1919
get_random_u256,
2020
u256_try_into_felt,
2121
};
22-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
22+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
2323
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
2424
BinaryData,
2525
EdgeData,
@@ -34,6 +34,7 @@ use starknet_types_core::felt::Felt;
3434
use strum::IntoEnumIterator;
3535

3636
use crate::block_committer::input::StarknetStorageValue;
37+
use crate::db::facts_db::db::FactDbFilledNode;
3738
use crate::forest::filled_forest::FilledForest;
3839
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
3940
use crate::patricia_merkle_tree::types::{
@@ -180,7 +181,10 @@ macro_rules! random_filled_node {
180181
($leaf:ty) => {
181182
impl RandomValue for FactDbFilledNode<$leaf> {
182183
fn random<R: Rng>(rng: &mut R, max: Option<U256>) -> Self {
183-
Self { data: NodeData::random(rng, max), hash: HashOutput::random(rng, max) }
184+
Self(FilledNode {
185+
data: NodeData::random(rng, max),
186+
hash: HashOutput::random(rng, max),
187+
})
184188
}
185189
}
186190
};
@@ -225,7 +229,7 @@ macro_rules! random_filled_tree {
225229
nodes.push((NodeIndex::ROOT, FactDbFilledNode::random(rng, max_size)));
226230

227231
Self {
228-
tree_map: nodes.into_iter().collect(),
232+
tree_map: nodes.into_iter().map(|(index, node)| (index, node.0)).collect(),
229233
root_hash: HashOutput(Felt::random(rng, max_size)),
230234
}
231235
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod create_facts_tree;
22
pub mod db;
3+
pub mod node_serde;
34
pub mod traversal;
45
pub mod types;
56

6-
pub use db::{FactsDb, FactsNodeLayout};
7+
pub use db::{FactDbFilledNode, FactsDb, FactsNodeLayout};

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use async_trait::async_trait;
44
use starknet_api::core::ContractAddress;
55
use starknet_api::hash::HashOutput;
66
use starknet_patricia::db_layout::{NodeLayout, NodeLayoutFor};
7-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
8-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FactNodeDeserializationContext;
7+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
98
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
109
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
1110
use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
@@ -15,6 +14,7 @@ use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1514

1615
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
1716
use crate::db::db_layout::DbLayout;
17+
use crate::db::facts_db::node_serde::FactNodeDeserializationContext;
1818
use crate::db::facts_db::types::{FactsDbInitialRead, FactsSubTree};
1919
use crate::db::forest_trait::{read_forest, serialize_forest, ForestReader, ForestWriter};
2020
use crate::forest::filled_forest::FilledForest;
@@ -23,6 +23,10 @@ use crate::forest::original_skeleton_forest::{ForestSortedIndices, OriginalSkele
2323
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
2424
use crate::patricia_merkle_tree::types::CompiledClassHash;
2525

26+
/// DB representation of a trie node in facts layout.
27+
#[derive(PartialEq, Debug, derive_more::Into)]
28+
pub struct FactDbFilledNode<L: Leaf>(pub FilledNode<L, HashOutput>);
29+
2630
/// Facts DB node layout.
2731
///
2832
/// In a facts DB, the storage keys are node hashes and the values are preimages. In particular,
@@ -44,12 +48,13 @@ impl<'a, L: Leaf> NodeLayout<'a, L> for FactsNodeLayout {
4448
key_context: &<L as HasStaticPrefix>::KeyContext,
4549
filled_node: FilledNode<LeafBase, HashOutput>,
4650
) -> (DbKey, Self::NodeDbObject) {
47-
let db_filled_node = Self::convert_node_data_and_leaf(filled_node);
51+
let hash_filled_node = Self::convert_node_data_and_leaf(filled_node);
4852

49-
let suffix = &db_filled_node.hash.0.to_bytes_be();
50-
let key = db_filled_node.get_db_key(key_context, suffix);
53+
let suffix = &hash_filled_node.hash.0.to_bytes_be();
54+
let db_node = FactDbFilledNode(hash_filled_node);
55+
let key = db_node.get_db_key(key_context, suffix);
5156

52-
(key, db_filled_node)
57+
(key, db_node)
5358
}
5459
}
5560

crates/starknet_patricia/src/patricia_merkle_tree/filled_tree/node_serde.rs renamed to crates/starknet_committer/src/db/facts_db/node_serde.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
use ethnum::U256;
22
use starknet_api::hash::HashOutput;
3+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
4+
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
5+
BinaryData,
6+
EdgeData,
7+
EdgePathLength,
8+
NodeData,
9+
PathToBottom,
10+
};
11+
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
312
use starknet_patricia_storage::db_object::{
413
DBObject,
514
EmptyDeserializationContext,
@@ -10,15 +19,7 @@ use starknet_patricia_storage::errors::{DeserializationError, SerializationResul
1019
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix, DbValue};
1120
use starknet_types_core::felt::Felt;
1221

13-
use crate::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
14-
use crate::patricia_merkle_tree::node_data::inner_node::{
15-
BinaryData,
16-
EdgeData,
17-
EdgePathLength,
18-
NodeData,
19-
PathToBottom,
20-
};
21-
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
22+
use crate::db::facts_db::db::FactDbFilledNode;
2223

2324
// Const describe the size of the serialized node.
2425
pub const SERIALIZE_HASH_BYTES: usize = 32;
@@ -44,12 +45,12 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
4445
}
4546
}
4647

47-
impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
48+
impl<L: Leaf> HasDynamicPrefix for FactDbFilledNode<L> {
4849
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
4950
type KeyContext = <L as HasStaticPrefix>::KeyContext;
5051

5152
fn get_prefix(&self, key_context: &Self::KeyContext) -> DbKeyPrefix {
52-
match &self.data {
53+
match &self.0.data {
5354
NodeData::Binary(_) | NodeData::Edge(_) => PatriciaPrefix::InnerNode,
5455
NodeData::Leaf(_) => PatriciaPrefix::Leaf(L::get_static_prefix(key_context)),
5556
}
@@ -71,7 +72,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
7172
/// - For edge nodes: Concatenates bottom hash, path, and path length.
7273
/// - For leaf nodes: use leaf.serialize() method.
7374
fn serialize(&self) -> SerializationResult<DbValue> {
74-
match &self.data {
75+
match &self.0.data {
7576
NodeData::Binary(BinaryData { left_data: left_hash, right_data: right_hash }) => {
7677
// Serialize left and right hashes to byte arrays.
7778
let left: [u8; SERIALIZE_HASH_BYTES] = left_hash.0.to_bytes_be();
@@ -103,14 +104,14 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
103104
deserialize_context: &Self::DeserializeContext,
104105
) -> Result<Self, DeserializationError> {
105106
if deserialize_context.is_leaf {
106-
return Ok(Self {
107+
return Ok(Self(FilledNode {
107108
hash: deserialize_context.node_hash,
108109
data: NodeData::Leaf(L::deserialize(value, &EmptyDeserializationContext)?),
109-
});
110+
}));
110111
}
111112

112113
if value.0.len() == BINARY_BYTES {
113-
Ok(Self {
114+
Ok(Self(FilledNode {
114115
hash: deserialize_context.node_hash,
115116
data: NodeData::Binary(BinaryData {
116117
left_data: HashOutput(Felt::from_bytes_be_slice(
@@ -120,7 +121,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
120121
&value.0[SERIALIZE_HASH_BYTES..],
121122
)),
122123
}),
123-
})
124+
}))
124125
} else {
125126
assert_eq!(
126127
value.0.len(),
@@ -130,7 +131,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
130131
EDGE_BYTES,
131132
BINARY_BYTES
132133
);
133-
Ok(Self {
134+
Ok(Self(FilledNode {
134135
hash: deserialize_context.node_hash,
135136
data: NodeData::Edge(EdgeData {
136137
bottom_data: HashOutput(Felt::from_bytes_be_slice(
@@ -148,7 +149,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
148149
)
149150
.map_err(|error| DeserializationError::ValueError(Box::new(error)))?,
150151
}),
151-
})
152+
}))
152153
}
153154
}
154155

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use starknet_api::hash::{HashOutput, StateRoots};
2-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::{
3-
FactNodeDeserializationContext,
4-
PatriciaPrefix,
5-
};
62
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
73
use starknet_patricia::patricia_merkle_tree::traversal::{SubTreeTrait, UnmodifiedChildTraversal};
84
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
95
use starknet_patricia_storage::db_object::HasStaticPrefix;
106
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix};
117

128
use crate::block_committer::input::InputContext;
9+
use crate::db::facts_db::node_serde::{FactNodeDeserializationContext, PatriciaPrefix};
1310

1411
#[derive(Debug, PartialEq)]
1512
pub struct FactsSubTree<'a> {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use async_recursion::async_recursion;
22
use starknet_api::core::ContractAddress;
33
use starknet_api::hash::{HashOutput, StateRoots};
4-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
5-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::{
6-
FactNodeDeserializationContext,
7-
PatriciaPrefix,
8-
};
4+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
95
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
106
BinaryData,
117
EdgeData,
@@ -25,6 +21,8 @@ use starknet_patricia_storage::storage_trait::{
2521
};
2622

2723
use crate::block_committer::input::{try_node_index_into_contract_address, StarknetStorageValue};
24+
use crate::db::facts_db::db::FactDbFilledNode;
25+
use crate::db::facts_db::node_serde::{FactNodeDeserializationContext, PatriciaPrefix};
2826
use crate::db::index_db::leaves::{
2927
IndexLayoutCompiledClassHash,
3028
IndexLayoutContractState,
@@ -222,7 +220,7 @@ async fn traverse_and_convert<FactsLeaf, IndexLeaf, KeyContext>(
222220
)
223221
.unwrap();
224222

225-
let index_filled_node: IndexFilledNode<IndexLeaf> = match facts_filled_node.data {
223+
let index_filled_node: IndexFilledNode<IndexLeaf> = match facts_filled_node.0.data {
226224
NodeData::Binary(binary_data) => {
227225
let children_indices = current_index.get_children_indices();
228226
traverse_and_convert::<FactsLeaf, IndexLeaf, KeyContext>(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use ethnum::U256;
22
use starknet_api::hash::HashOutput;
33
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
4-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::SERIALIZE_HASH_BYTES;
54
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
65
BinaryData,
76
EdgeData,
@@ -23,6 +22,7 @@ use starknet_patricia_storage::errors::{DeserializationError, SerializationResul
2322
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix, DbValue};
2423
use starknet_types_core::felt::Felt;
2524

25+
use crate::db::facts_db::node_serde::SERIALIZE_HASH_BYTES;
2626
use crate::hash_function::hash::TreeHashFunctionImpl;
2727

2828
// In index layout, for binary nodes, only the hash is stored.

0 commit comments

Comments
 (0)