Skip to content

Commit 83700ae

Browse files
committed
starknet_committer,starknet_patricia: add serialization utilities to node layout
1 parent 32689fd commit 83700ae

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use starknet_patricia::db_layout::{NodeLayout, TrieType};
77
use starknet_patricia::patricia_merkle_tree::filled_tree::node::{FactDbFilledNode, FilledNode};
88
use starknet_patricia::patricia_merkle_tree::filled_tree::node_serde::FactNodeDeserializationContext;
99
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
10+
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::NodeData;
1011
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
1112
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
1213
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
@@ -50,6 +51,17 @@ where
5051
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData> {
5152
node_db_object
5253
}
54+
55+
fn get_db_object(
56+
hash: HashOutput,
57+
filled_node_data: NodeData<L, HashOutput>,
58+
) -> Self::NodeDbObject {
59+
FilledNode { hash, data: filled_node_data }
60+
}
61+
62+
fn get_node_suffix(_index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8> {
63+
node_db_object.hash.0.to_bytes_be().to_vec()
64+
}
5365
}
5466

5567
pub struct FactsDb<S: Storage> {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use starknet_api::hash::HashOutput;
66
use starknet_patricia::db_layout::{NodeLayout, TrieType};
77
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
88
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
9+
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
10+
BinaryData,
11+
EdgeData,
12+
NodeData,
13+
};
914
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
1015
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
1116
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
@@ -67,6 +72,17 @@ where
6772
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData> {
6873
node_db_object.0
6974
}
75+
76+
fn get_db_object(
77+
hash: HashOutput,
78+
filled_node_data: NodeData<L, HashOutput>,
79+
) -> Self::NodeDbObject {
80+
IndexFilledNode(FilledNode { hash, data: dump_child_hashes(filled_node_data) })
81+
}
82+
83+
fn get_node_suffix(index: NodeIndex, _node_db_object: &Self::NodeDbObject) -> Vec<u8> {
84+
index.0.to_be_bytes().to_vec()
85+
}
7086
}
7187

7288
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside
@@ -142,3 +158,15 @@ impl<S: Storage> ForestWriter for IndexDb<S> {
142158
n_updates
143159
}
144160
}
161+
162+
fn dump_child_hashes<L: Leaf>(data: NodeData<L, HashOutput>) -> NodeData<L, ()> {
163+
match data {
164+
NodeData::Leaf(leaf) => NodeData::Leaf(leaf),
165+
166+
NodeData::Binary(_) => NodeData::Binary(BinaryData { left_data: (), right_data: () }),
167+
168+
NodeData::Edge(edge_data) => {
169+
NodeData::Edge(EdgeData { bottom_data: (), path_to_bottom: edge_data.path_to_bottom })
170+
}
171+
}
172+
}

crates/starknet_patricia/src/db_layout.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use starknet_api::hash::HashOutput;
33
use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
44

55
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
6+
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
67
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
78
use crate::patricia_merkle_tree::traversal::SubTreeTrait;
89

@@ -24,7 +25,10 @@ pub trait NodeLayout<'a, L: Leaf> {
2425
type DeserializationContext;
2526

2627
/// The storage representation of the node.
27-
type NodeDbObject: DBObject<DeserializeContext = Self::DeserializationContext>;
28+
type NodeDbObject: DBObject<
29+
DeserializeContext = Self::DeserializationContext,
30+
KeyContext = <L as HasStaticPrefix>::KeyContext,
31+
>;
2832

2933
/// The type of the subtree that is used to traverse the trie.
3034
type SubTree: SubTreeTrait<
@@ -40,4 +44,16 @@ pub trait NodeLayout<'a, L: Leaf> {
4044
/// Converts a node db object to a filled node. Used during the trie traversal for the skeleton
4145
/// construction.
4246
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData>;
47+
48+
/// Converts `FilledTree` nodes to db objects.
49+
///
50+
/// During the construction of a `FilledTree` we computee the hashes and carry `FilledNode<L,
51+
/// HashOutput>`, hence, the `FilledNode` type is not necessarily what we want to store.
52+
fn get_db_object(
53+
hash: HashOutput,
54+
filled_node_data: NodeData<L, HashOutput>,
55+
) -> Self::NodeDbObject;
56+
57+
/// Returns the db key suffix of the node db object.
58+
fn get_node_suffix(index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8>;
4359
}

0 commit comments

Comments
 (0)