Skip to content

Commit 9f2ba58

Browse files
committed
starknet_committer,starknet_patricia: add serialization utilities to node layout
1 parent 597a809 commit 9f2ba58

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
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::traversal::SubTreeTrait;
1213
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
@@ -65,6 +66,17 @@ where
6566
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData> {
6667
node_db_object
6768
}
69+
70+
fn get_db_object(
71+
hash: HashOutput,
72+
filled_node_data: NodeData<L, HashOutput>,
73+
) -> Self::NodeDbObject {
74+
FilledNode { hash, data: filled_node_data }
75+
}
76+
77+
fn get_node_suffix(_index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8> {
78+
node_db_object.hash.0.to_bytes_be().to_vec()
79+
}
6880
}
6981

7082
pub struct FactsDb<S: Storage> {

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ 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;
12-
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
17+
use starknet_patricia_storage::db_object::HasStaticPrefix;
1318
use starknet_patricia_storage::errors::SerializationResult;
1419
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1520

@@ -66,6 +71,17 @@ where
6671
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData> {
6772
node_db_object.0
6873
}
74+
75+
fn get_db_object(
76+
hash: HashOutput,
77+
filled_node_data: NodeData<L, HashOutput>,
78+
) -> Self::NodeDbObject {
79+
IndexFilledNode(FilledNode { hash, data: dump_child_hashes(filled_node_data) })
80+
}
81+
82+
fn get_node_suffix(index: NodeIndex, _node_db_object: &Self::NodeDbObject) -> Vec<u8> {
83+
index.0.to_be_bytes().to_vec()
84+
}
6985
}
7086

7187
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside
@@ -151,3 +167,15 @@ impl<S: Storage> ForestMetadata for IndexDb<S> {
151167
DbKey(vec![0])
152168
}
153169
}
170+
171+
fn dump_child_hashes<L: Leaf>(data: NodeData<L, HashOutput>) -> NodeData<L, ()> {
172+
match data {
173+
NodeData::Leaf(leaf) => NodeData::Leaf(leaf),
174+
175+
NodeData::Binary(_) => NodeData::Binary(BinaryData { left_data: (), right_data: () }),
176+
177+
NodeData::Edge(edge_data) => {
178+
NodeData::Edge(EdgeData { bottom_data: (), path_to_bottom: edge_data.path_to_bottom })
179+
}
180+
}
181+
}

crates/starknet_patricia/src/db_layout.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
44
use starknet_patricia_storage::storage_trait::DbKeyPrefix;
55

66
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
7+
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
78
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
89
use crate::patricia_merkle_tree::traversal::SubTreeTrait;
910
use crate::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
@@ -35,7 +36,10 @@ pub trait NodeLayout<'a, L: Leaf> {
3536
type NodeData: Clone;
3637

3738
/// The storage representation of the node.
38-
type NodeDbObject: DBObject<DeserializeContext = Self::DeserializationContext>;
39+
type NodeDbObject: DBObject<
40+
DeserializeContext = Self::DeserializationContext,
41+
KeyContext = <L as HasStaticPrefix>::KeyContext,
42+
>;
3943

4044
/// The context needed to deserialize the node from a raw
4145
/// [starknet_patricia_storage::storage_trait::DbValue].
@@ -57,4 +61,16 @@ pub trait NodeLayout<'a, L: Leaf> {
5761
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext;
5862

5963
fn get_filled_node(node_db_object: Self::NodeDbObject) -> FilledNode<L, Self::NodeData>;
64+
65+
/// Converts `FilledTree` nodes to db objects.
66+
///
67+
/// During the construction of a `FilledTree` we computee the hashes and carry `FilledNode<L,
68+
/// HashOutput>`, hence, the `FilledNode` type is not necessarily what we want to store.
69+
fn get_db_object(
70+
hash: HashOutput,
71+
filled_node_data: NodeData<L, HashOutput>,
72+
) -> Self::NodeDbObject;
73+
74+
/// Returns the db key suffix of the node db object.
75+
fn get_node_suffix(index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8>;
6076
}

0 commit comments

Comments
 (0)