@@ -6,9 +6,9 @@ use starknet_api::hash::HashOutput;
66use starknet_patricia:: patricia_merkle_tree:: filled_tree:: node_serde:: FactNodeDeserializationContext ;
77use starknet_patricia:: patricia_merkle_tree:: filled_tree:: tree:: FilledTree ;
88use starknet_patricia:: patricia_merkle_tree:: node_data:: leaf:: { Leaf , LeafModifications } ;
9- use starknet_patricia:: patricia_merkle_tree:: original_skeleton_tree :: tree :: OriginalSkeletonTreeImpl ;
9+ use starknet_patricia:: patricia_merkle_tree:: traversal :: SubTreeTrait ;
1010use starknet_patricia:: patricia_merkle_tree:: types:: { NodeIndex , SortedLeafIndices } ;
11- use starknet_patricia_storage:: db_object:: EmptyKeyContext ;
11+ use starknet_patricia_storage:: db_object:: { EmptyKeyContext , HasStaticPrefix } ;
1212use starknet_patricia_storage:: errors:: SerializationResult ;
1313use starknet_patricia_storage:: map_storage:: MapStorage ;
1414use starknet_patricia_storage:: storage_trait:: {
@@ -19,25 +19,16 @@ use starknet_patricia_storage::storage_trait::{
1919 Storage ,
2020} ;
2121
22- use crate :: block_committer:: input:: {
23- contract_address_into_node_index,
24- Config ,
25- FactsDbInitialRead ,
26- ReaderConfig ,
27- StarknetStorageValue ,
28- } ;
22+ use crate :: block_committer:: input:: { FactsDbInitialRead , ReaderConfig , StarknetStorageValue } ;
2923use crate :: db:: db_layout:: NodeLayout ;
30- use crate :: db:: facts_db:: create_facts_tree:: {
31- create_original_skeleton_tree,
32- create_original_skeleton_tree_and_get_previous_leaves,
33- } ;
3424use crate :: db:: facts_db:: types:: FactsSubTree ;
3525use crate :: db:: forest_trait:: { ForestMetadata , ForestMetadataType , ForestReader , ForestWriter } ;
26+ use crate :: db:: index_db:: leaves:: TrieType ;
27+ use crate :: db:: trie_traversal:: { create_classes_trie, create_contracts_trie, create_storage_tries} ;
3628use crate :: forest:: filled_forest:: FilledForest ;
37- use crate :: forest:: forest_errors:: { ForestError , ForestResult } ;
29+ use crate :: forest:: forest_errors:: ForestResult ;
3830use crate :: forest:: original_skeleton_forest:: { ForestSortedIndices , OriginalSkeletonForest } ;
3931use crate :: patricia_merkle_tree:: leaf:: leaf_impl:: ContractState ;
40- use crate :: patricia_merkle_tree:: tree:: OriginalSkeletonTrieConfig ;
4132use crate :: patricia_merkle_tree:: types:: CompiledClassHash ;
4233
4334/// Facts DB node layout.
@@ -47,12 +38,27 @@ use crate::patricia_merkle_tree::types::CompiledClassHash;
4738/// have the db keys of its children.
4839pub struct FactsNodeLayout { }
4940
50- impl < ' a , L : Leaf > NodeLayout < ' a , L > for FactsNodeLayout {
41+ impl < ' a , L : Leaf > NodeLayout < ' a , L > for FactsNodeLayout
42+ where
43+ L : HasStaticPrefix < KeyContext = EmptyKeyContext > ,
44+ {
5145 type NodeData = HashOutput ;
5246
5347 type DeserializationContext = FactNodeDeserializationContext ;
5448
5549 type SubTree = FactsSubTree < ' a > ;
50+
51+ fn create_subtree (
52+ sorted_leaf_indices : SortedLeafIndices < ' a > ,
53+ root_index : NodeIndex ,
54+ root_hash : HashOutput ,
55+ ) -> Self :: SubTree {
56+ FactsSubTree :: create ( sorted_leaf_indices, root_index, root_hash)
57+ }
58+
59+ fn generate_key_context ( _trie_type : TrieType ) -> <L as HasStaticPrefix >:: KeyContext {
60+ EmptyKeyContext
61+ }
5662}
5763
5864pub struct FactsDb < S : Storage > {
@@ -68,75 +74,6 @@ impl<S: Storage> FactsDb<S> {
6874 pub fn new ( storage : S ) -> Self {
6975 Self { storage }
7076 }
71-
72- /// Creates the contracts trie original skeleton.
73- /// Also returns the previous contracts state of the modified contracts.
74- async fn create_contracts_trie < ' a > (
75- & mut self ,
76- contracts_trie_root_hash : HashOutput ,
77- contracts_trie_sorted_indices : SortedLeafIndices < ' a > ,
78- ) -> ForestResult < ( OriginalSkeletonTreeImpl < ' a > , HashMap < NodeIndex , ContractState > ) > {
79- Ok ( create_original_skeleton_tree_and_get_previous_leaves (
80- & mut self . storage ,
81- contracts_trie_root_hash,
82- contracts_trie_sorted_indices,
83- & HashMap :: new ( ) ,
84- & OriginalSkeletonTrieConfig :: new ( true ) ,
85- & EmptyKeyContext ,
86- )
87- . await ?)
88- }
89-
90- async fn create_storage_tries < ' a > (
91- & mut self ,
92- actual_storage_updates : & HashMap < ContractAddress , LeafModifications < StarknetStorageValue > > ,
93- original_contracts_trie_leaves : & HashMap < NodeIndex , ContractState > ,
94- config : & impl Config ,
95- storage_tries_sorted_indices : & HashMap < ContractAddress , SortedLeafIndices < ' a > > ,
96- ) -> ForestResult < HashMap < ContractAddress , OriginalSkeletonTreeImpl < ' a > > > {
97- let mut storage_tries = HashMap :: new ( ) ;
98- for ( address, updates) in actual_storage_updates {
99- let sorted_leaf_indices = storage_tries_sorted_indices
100- . get ( address)
101- . ok_or ( ForestError :: MissingSortedLeafIndices ( * address) ) ?;
102- let contract_state = original_contracts_trie_leaves
103- . get ( & contract_address_into_node_index ( address) )
104- . ok_or ( ForestError :: MissingContractCurrentState ( * address) ) ?;
105- let config = OriginalSkeletonTrieConfig :: new ( config. warn_on_trivial_modifications ( ) ) ;
106-
107- let original_skeleton = create_original_skeleton_tree (
108- & mut self . storage ,
109- contract_state. storage_root_hash ,
110- * sorted_leaf_indices,
111- & config,
112- updates,
113- & EmptyKeyContext ,
114- )
115- . await ?;
116- storage_tries. insert ( * address, original_skeleton) ;
117- }
118- Ok ( storage_tries)
119- }
120-
121- async fn create_classes_trie < ' a > (
122- & mut self ,
123- actual_classes_updates : & LeafModifications < CompiledClassHash > ,
124- classes_trie_root_hash : HashOutput ,
125- config : & impl Config ,
126- contracts_trie_sorted_indices : SortedLeafIndices < ' a > ,
127- ) -> ForestResult < OriginalSkeletonTreeImpl < ' a > > {
128- let config = OriginalSkeletonTrieConfig :: new ( config. warn_on_trivial_modifications ( ) ) ;
129-
130- Ok ( create_original_skeleton_tree (
131- & mut self . storage ,
132- classes_trie_root_hash,
133- contracts_trie_sorted_indices,
134- & config,
135- actual_classes_updates,
136- & EmptyKeyContext ,
137- )
138- . await ?)
139- }
14077}
14178
14279impl FactsDb < MapStorage > {
@@ -158,28 +95,29 @@ impl<S: Storage> ForestReader<FactsDbInitialRead> for FactsDb<S> {
15895 forest_sorted_indices : & ' a ForestSortedIndices < ' a > ,
15996 config : ReaderConfig ,
16097 ) -> ForestResult < ( OriginalSkeletonForest < ' a > , HashMap < NodeIndex , ContractState > ) > {
161- let ( contracts_trie, original_contracts_trie_leaves) = self
162- . create_contracts_trie (
98+ let ( contracts_trie, original_contracts_trie_leaves) =
99+ create_contracts_trie :: < ContractState , FactsNodeLayout > (
100+ & mut self . storage ,
163101 context. 0 . contracts_trie_root_hash ,
164102 forest_sorted_indices. contracts_trie_sorted_indices ,
165103 )
166104 . await ?;
167- let storage_tries = self
168- . create_storage_tries (
169- storage_updates,
170- & original_contracts_trie_leaves,
171- & config,
172- & forest_sorted_indices. storage_tries_sorted_indices ,
173- )
174- . await ?;
175- let classes_trie = self
176- . create_classes_trie (
177- classes_updates,
178- context. 0 . classes_trie_root_hash ,
179- & config,
180- forest_sorted_indices. classes_trie_sorted_indices ,
181- )
182- . await ?;
105+ let storage_tries = create_storage_tries :: < StarknetStorageValue , FactsNodeLayout > (
106+ & mut self . storage ,
107+ storage_updates,
108+ & original_contracts_trie_leaves,
109+ & config,
110+ & forest_sorted_indices. storage_tries_sorted_indices ,
111+ )
112+ . await ?;
113+ let classes_trie = create_classes_trie :: < CompiledClassHash , FactsNodeLayout > (
114+ & mut self . storage ,
115+ classes_updates,
116+ context. 0 . classes_trie_root_hash ,
117+ & config,
118+ forest_sorted_indices. classes_trie_sorted_indices ,
119+ )
120+ . await ?;
183121
184122 Ok ( (
185123 OriginalSkeletonForest { classes_trie, contracts_trie, storage_tries } ,
0 commit comments