Skip to content

Commit fc7cbdf

Browse files
committed
starknet_committer,starknet_patricia: add serialization utilities to node layout
1 parent 559cb65 commit fc7cbdf

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use async_trait::async_trait;
44
use starknet_api::core::ContractAddress;
55
use starknet_api::hash::HashOutput;
66
use starknet_patricia::db_layout::{NodeLayout, TrieType};
7-
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FactDbFilledNode;
7+
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::{
1112
LeafModifications,
1213
LeafWithEmptyKeyContext,
@@ -46,6 +47,17 @@ impl<'a, L: LeafWithEmptyKeyContext> NodeLayout<'a, L> for FactsNodeLayout {
4647
fn generate_key_context(_trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext {
4748
EmptyKeyContext
4849
}
50+
51+
fn get_db_object(
52+
hash: HashOutput,
53+
filled_node_data: NodeData<L, HashOutput>,
54+
) -> Self::NodeDbObject {
55+
FilledNode { hash, data: filled_node_data }
56+
}
57+
58+
fn get_node_suffix(_index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8> {
59+
node_db_object.hash.0.to_bytes_be().to_vec()
60+
}
4961
}
5062

5163
pub struct FactsDb<S: Storage> {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ use std::collections::HashMap;
22

33
use async_trait::async_trait;
44
use starknet_api::core::ContractAddress;
5+
use starknet_api::hash::HashOutput;
56
use starknet_patricia::db_layout::{NodeLayout, TrieType};
7+
use starknet_patricia::patricia_merkle_tree::filled_tree::node::FilledNode;
68
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+
};
714
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
815
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
916
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
@@ -58,6 +65,17 @@ where
5865
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext {
5966
trie_type
6067
}
68+
69+
fn get_db_object(
70+
hash: HashOutput,
71+
filled_node_data: NodeData<L, HashOutput>,
72+
) -> Self::NodeDbObject {
73+
IndexFilledNode(FilledNode { hash, data: dump_child_hashes(filled_node_data) })
74+
}
75+
76+
fn get_node_suffix(index: NodeIndex, _node_db_object: &Self::NodeDbObject) -> Vec<u8> {
77+
index.0.to_be_bytes().to_vec()
78+
}
6179
}
6280

6381
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside
@@ -133,3 +151,18 @@ impl<S: Storage> ForestWriter for IndexDb<S> {
133151
n_updates
134152
}
135153
}
154+
155+
fn dump_child_hashes<L: Leaf>(data: NodeData<L, HashOutput>) -> NodeData<L, EmptyNodeData> {
156+
match data {
157+
NodeData::Leaf(leaf) => NodeData::Leaf(leaf),
158+
159+
NodeData::Binary(_) => {
160+
NodeData::Binary(BinaryData { left_data: EmptyNodeData, right_data: EmptyNodeData })
161+
}
162+
163+
NodeData::Edge(edge_data) => NodeData::Edge(EdgeData {
164+
bottom_data: EmptyNodeData,
165+
path_to_bottom: edge_data.path_to_bottom,
166+
}),
167+
}
168+
}

crates/starknet_patricia/src/db_layout.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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;
9+
use crate::patricia_merkle_tree::types::NodeIndex;
810

911
// TODO(Ariel): Delete this enum and use `CommitmentType` instead.
1012
#[derive(Debug, PartialEq)]
@@ -24,8 +26,10 @@ pub trait NodeLayout<'a, L: Leaf> {
2426
type DeserializationContext;
2527

2628
/// The storage representation of the node.
27-
type NodeDbObject: DBObject<DeserializeContext = Self::DeserializationContext>
28-
+ Into<FilledNode<L, Self::NodeData>>;
29+
type NodeDbObject: DBObject<
30+
DeserializeContext = Self::DeserializationContext,
31+
KeyContext = <L as HasStaticPrefix>::KeyContext,
32+
> + Into<FilledNode<L, Self::NodeData>>;
2933

3034
/// The type of the subtree that is used to traverse the trie.
3135
type SubTree: SubTreeTrait<
@@ -37,4 +41,16 @@ pub trait NodeLayout<'a, L: Leaf> {
3741
/// Generates the key context for the given trie type. Used for reading nodes of a specific
3842
/// tree (contracts, classes, or storage), to construct a skeleton tree.
3943
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext;
44+
45+
/// Converts `FilledTree` nodes to db objects.
46+
///
47+
/// During the construction of a `FilledTree` we computee the hashes and carry `FilledNode<L,
48+
/// HashOutput>`, hence, the `FilledNode` type is not necessarily what we want to store.
49+
fn get_db_object(
50+
hash: HashOutput,
51+
filled_node_data: NodeData<L, HashOutput>,
52+
) -> Self::NodeDbObject;
53+
54+
/// Returns the db key suffix of the node db object.
55+
fn get_node_suffix(index: NodeIndex, node_db_object: &Self::NodeDbObject) -> Vec<u8>;
4056
}

0 commit comments

Comments
 (0)