11use starknet_api:: core:: ContractAddress ;
22use starknet_api:: hash:: HashOutput ;
33use starknet_patricia_storage:: db_object:: { DBObject , HasStaticPrefix } ;
4+ use starknet_patricia_storage:: storage_trait:: DbKey ;
45
56use crate :: patricia_merkle_tree:: filled_tree:: node:: FilledNode ;
7+ use crate :: patricia_merkle_tree:: node_data:: inner_node:: { BinaryData , EdgeData , NodeData } ;
68use crate :: patricia_merkle_tree:: node_data:: leaf:: Leaf ;
79use 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