Skip to content

Commit 00a550f

Browse files
committed
starknet_committer,starknet_patricia: add serialization utilities to node layout
1 parent 8628308 commit 00a550f

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ 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;
1010
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{
11+
Leaf,
1112
LeafModifications,
1213
LeafWithEmptyKeyContext,
1314
};
1415
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
15-
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
16+
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext, HasStaticPrefix};
1617
use starknet_patricia_storage::errors::SerializationResult;
1718
use starknet_patricia_storage::map_storage::MapStorage;
18-
use starknet_patricia_storage::storage_trait::{DbHashMap, Storage};
19+
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1920

2021
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
2122
use crate::db::facts_db::types::{FactsDbInitialRead, FactsSubTree};
@@ -46,6 +47,18 @@ 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<LeafBase: Leaf + Into<L>>(
52+
_node_index: NodeIndex,
53+
key_context: &<L as HasStaticPrefix>::KeyContext,
54+
filled_node: FilledNode<LeafBase, HashOutput>,
55+
) -> (DbKey, Self::NodeDbObject) {
56+
let db_filled_node = Self::convert_node_data_and_leaf(filled_node);
57+
58+
let key = db_filled_node.get_db_key(key_context, &db_filled_node.hash.0.to_bytes_be());
59+
60+
(key, db_filled_node)
61+
}
4962
}
5063

5164
pub struct FactsDb<S: Storage> {

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ 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;
79
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
810
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
911
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
10-
use starknet_patricia_storage::db_object::{EmptyKeyContext, HasStaticPrefix};
12+
use starknet_patricia_storage::db_object::{DBObject, EmptyKeyContext, HasStaticPrefix};
1113
use starknet_patricia_storage::errors::SerializationResult;
12-
use starknet_patricia_storage::storage_trait::{DbHashMap, Storage};
14+
use starknet_patricia_storage::storage_trait::{DbHashMap, DbKey, Storage};
1315

1416
use crate::block_committer::input::{ReaderConfig, StarknetStorageValue};
1517
use crate::db::facts_db::types::FactsDbInitialRead;
@@ -58,6 +60,20 @@ where
5860
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext {
5961
trie_type
6062
}
63+
64+
fn get_db_object<LeafBase: Leaf + Into<L>>(
65+
node_index: NodeIndex,
66+
key_context: &<L as HasStaticPrefix>::KeyContext,
67+
filled_node: FilledNode<LeafBase, HashOutput>,
68+
) -> (DbKey, Self::NodeDbObject) {
69+
let filled_node = Self::convert_node_data_and_leaf(filled_node);
70+
71+
let db_filled_node = IndexFilledNode(filled_node);
72+
73+
let key = db_filled_node.get_db_key(key_context, &node_index.0.to_be_bytes());
74+
75+
(key, db_filled_node)
76+
}
6177
}
6278

6379
// TODO(Ariel): define an IndexDbInitialRead empty type, and check whether each tree is empty inside

crates/starknet_patricia/src/db_layout.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use starknet_api::core::ContractAddress;
22
use starknet_api::hash::HashOutput;
33
use starknet_patricia_storage::db_object::{DBObject, HasStaticPrefix};
4+
use starknet_patricia_storage::storage_trait::DbKey;
45

56
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
7+
use crate::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData, NodeData};
68
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
79
use crate::patricia_merkle_tree::traversal::SubTreeTrait;
10+
use crate::patricia_merkle_tree::types::NodeIndex;
811

912
// TODO(Ariel): Delete this enum and use `CommitmentType` instead.
1013
#[derive(Debug, PartialEq)]
@@ -31,8 +34,10 @@ pub trait NodeLayout<'a, L: Leaf> {
3134
type DeserializationContext;
3235

3336
/// The storage representation of the node.
34-
type NodeDbObject: DBObject<DeserializeContext = Self::DeserializationContext>
35-
+ Into<FilledNode<L, Self::NodeData>>;
37+
type NodeDbObject: DBObject<
38+
DeserializeContext = Self::DeserializationContext,
39+
KeyContext = <L as HasStaticPrefix>::KeyContext,
40+
> + Into<FilledNode<L, Self::NodeData>>;
3641

3742
/// The type of the subtree that is used to traverse the trie.
3843
type SubTree: SubTreeTrait<
@@ -44,4 +49,37 @@ pub trait NodeLayout<'a, L: Leaf> {
4449
/// Generates the key context for the given trie type. Used for reading nodes of a specific
4550
/// tree (contracts, classes, or storage), to construct a skeleton tree.
4651
fn generate_key_context(trie_type: TrieType) -> <L as HasStaticPrefix>::KeyContext;
52+
53+
/// Converts `FilledTree` nodes to db objects.
54+
///
55+
/// During the construction of a `FilledTree` we computee the hashes and carry `FilledNode<L,
56+
/// HashOutput>`, hence, the `FilledNode` type is not necessarily what we want to store.
57+
fn get_db_object<LeafBase: Leaf + Into<L>>(
58+
node_index: NodeIndex,
59+
key_context: &<L as HasStaticPrefix>::KeyContext,
60+
filled_node: FilledNode<LeafBase, HashOutput>,
61+
) -> (DbKey, Self::NodeDbObject);
62+
63+
/// A utility function to convert a `FilledNode<LeafBase, HashOutput>` to a `FilledNode<L,
64+
/// Self::NodeData>`. Used during the serialization of a `FilledTree`.
65+
///
66+
/// LeafBase is one of StarknetStorageValue, CompiledClassHash, or ContractState, while L can be
67+
/// layout-dependent wrappers.
68+
fn convert_node_data_and_leaf<LeafBase: Leaf + Into<L>>(
69+
filled_node: FilledNode<LeafBase, HashOutput>,
70+
) -> FilledNode<L, Self::NodeData> {
71+
let data: NodeData<L, Self::NodeData> = match filled_node.data {
72+
NodeData::Binary(data) => NodeData::Binary(BinaryData {
73+
left_data: data.left_data.into(),
74+
right_data: data.right_data.into(),
75+
}),
76+
NodeData::Edge(data) => NodeData::Edge(EdgeData {
77+
bottom_data: data.bottom_data.into(),
78+
path_to_bottom: data.path_to_bottom.into(),
79+
}),
80+
NodeData::Leaf(leaf) => NodeData::Leaf(leaf.into()),
81+
};
82+
83+
FilledNode { hash: filled_node.hash, data }
84+
}
4785
}

0 commit comments

Comments
 (0)