Skip to content

Commit c6d0565

Browse files
committed
starknet_committer,starknet_patricia: move node_serde to facts db
1 parent 72cc49e commit c6d0565

File tree

13 files changed

+93
-89
lines changed

13 files changed

+93
-89
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
pub const FACTS_DB_KEY_SEPARATOR: &[u8] = b":";
2425

@@ -46,12 +47,12 @@ impl From<PatriciaPrefix> for DbKeyPrefix {
4647
}
4748
}
4849

49-
impl<L: Leaf> HasDynamicPrefix for FilledNode<L, HashOutput> {
50+
impl<L: Leaf> HasDynamicPrefix for FactDbFilledNode<L> {
5051
// Inherit the KeyContext from the HasStaticPrefix implementation of the leaf.
5152
type KeyContext = <L as HasStaticPrefix>::KeyContext;
5253

5354
fn get_prefix(&self, key_context: &Self::KeyContext) -> DbKeyPrefix {
54-
match &self.data {
55+
match &self.0.data {
5556
NodeData::Binary(_) | NodeData::Edge(_) => PatriciaPrefix::InnerNode,
5657
NodeData::Leaf(_) => PatriciaPrefix::Leaf(L::get_static_prefix(key_context)),
5758
}
@@ -73,7 +74,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
7374
/// - For edge nodes: Concatenates bottom hash, path, and path length.
7475
/// - For leaf nodes: use leaf.serialize() method.
7576
fn serialize(&self) -> SerializationResult<DbValue> {
76-
match &self.data {
77+
match &self.0.data {
7778
NodeData::Binary(BinaryData { left_data: left_hash, right_data: right_hash }) => {
7879
// Serialize left and right hashes to byte arrays.
7980
let left: [u8; SERIALIZE_HASH_BYTES] = left_hash.0.to_bytes_be();
@@ -105,14 +106,14 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
105106
deserialize_context: &Self::DeserializeContext,
106107
) -> Result<Self, DeserializationError> {
107108
if deserialize_context.is_leaf {
108-
return Ok(Self {
109+
return Ok(Self(FilledNode {
109110
hash: deserialize_context.node_hash,
110111
data: NodeData::Leaf(L::deserialize(value, &EmptyDeserializationContext)?),
111-
});
112+
}));
112113
}
113114

114115
if value.0.len() == BINARY_BYTES {
115-
Ok(Self {
116+
Ok(Self(FilledNode {
116117
hash: deserialize_context.node_hash,
117118
data: NodeData::Binary(BinaryData {
118119
left_data: HashOutput(Felt::from_bytes_be_slice(
@@ -122,7 +123,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
122123
&value.0[SERIALIZE_HASH_BYTES..],
123124
)),
124125
}),
125-
})
126+
}))
126127
} else {
127128
assert_eq!(
128129
value.0.len(),
@@ -132,7 +133,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
132133
EDGE_BYTES,
133134
BINARY_BYTES
134135
);
135-
Ok(Self {
136+
Ok(Self(FilledNode {
136137
hash: deserialize_context.node_hash,
137138
data: NodeData::Edge(EdgeData {
138139
bottom_data: HashOutput(Felt::from_bytes_be_slice(
@@ -150,7 +151,7 @@ impl<L: Leaf> DBObject for FactDbFilledNode<L> {
150151
)
151152
.map_err(|error| DeserializationError::ValueError(Box::new(error)))?,
152153
}),
153-
})
154+
}))
154155
}
155156
}
156157

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
use starknet_api::hash::{HashOutput, StateRoots};
2+
<<<<<<< HEAD
23
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::{
34
FactNodeDeserializationContext,
45
PatriciaPrefix,
56
FACTS_DB_KEY_SEPARATOR,
67
};
8+
=======
9+
>>>>>>> 2d3d417c5d (starknet_committer,starknet_patricia: move node_serde to facts db)
710
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
811
use starknet_patricia::patricia_merkle_tree::traversal::{SubTreeTrait, UnmodifiedChildTraversal};
912
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
1013
use starknet_patricia_storage::db_object::HasStaticPrefix;
1114
use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix};
1215

1316
use crate::block_committer::input::InputContext;
17+
use crate::db::facts_db::node_serde::{FactNodeDeserializationContext, PatriciaPrefix};
1418

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

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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-
FACTS_DB_KEY_SEPARATOR,
9-
};
4+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
105
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
116
BinaryData,
127
EdgeData,
@@ -25,6 +20,12 @@ use starknet_patricia_storage::map_storage::MapStorage;
2520
use starknet_patricia_storage::storage_trait::{create_db_key, DbHashMap, DbValue, Storage};
2621

2722
use crate::block_committer::input::{try_node_index_into_contract_address, StarknetStorageValue};
23+
use crate::db::facts_db::db::FactDbFilledNode;
24+
use crate::db::facts_db::node_serde::{
25+
FactNodeDeserializationContext,
26+
PatriciaPrefix,
27+
FACTS_DB_KEY_SEPARATOR,
28+
};
2829
use crate::db::index_db::leaves::{
2930
IndexLayoutCompiledClassHash,
3031
IndexLayoutContractState,
@@ -223,7 +224,7 @@ async fn traverse_and_convert<FactsLeaf, IndexLeaf, KeyContext>(
223224
)
224225
.unwrap();
225226

226-
let index_filled_node: IndexFilledNode<IndexLeaf> = match facts_filled_node.data {
227+
let index_filled_node: IndexFilledNode<IndexLeaf> = match facts_filled_node.0.data {
227228
NodeData::Binary(binary_data) => {
228229
let children_indices = current_index.get_children_indices();
229230
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.

crates/starknet_committer/src/patricia_merkle_tree/leaf/leaf_serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::collections::HashMap;
33
use serde_json::Value;
44
use starknet_api::core::{ClassHash, Nonce};
55
use starknet_api::hash::HashOutput;
6-
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FACTS_DB_KEY_SEPARATOR;
76
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
87
use starknet_patricia_storage::db_object::{
98
DBObject,
@@ -15,6 +14,7 @@ use starknet_patricia_storage::storage_trait::{create_db_key, DbKey, DbKeyPrefix
1514
use starknet_types_core::felt::Felt;
1615

1716
use crate::block_committer::input::StarknetStorageValue;
17+
use crate::db::facts_db::node_serde::FACTS_DB_KEY_SEPARATOR;
1818
use crate::db::serde_db_utils::{deserialize_felt_no_packing, serialize_felt_no_packing};
1919
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
2020
use crate::patricia_merkle_tree::types::{fixed_hex_string_no_prefix, CompiledClassHash};

0 commit comments

Comments
 (0)