|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | 3 | use blockifier::state::accessed_keys::AccessedKeys; |
4 | | -use starknet_api::core::ContractAddress; |
5 | 4 | use starknet_api::hash::HashOutput; |
| 5 | +use starknet_patricia::patricia_merkle_tree::node_data::inner_node::PreimageMap; |
6 | 6 | use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig; |
7 | 7 | use starknet_patricia::patricia_merkle_tree::traversal::TraversalResult; |
8 | 8 | use starknet_patricia::patricia_merkle_tree::types::NodeIndex; |
9 | 9 | pub use starknet_patricia::patricia_merkle_tree::types::SortedLeafIndices; |
10 | 10 | use starknet_patricia_storage::db_object::EmptyKeyContext; |
11 | 11 | use starknet_patricia_storage::storage_trait::ReadOnlyStorage; |
12 | 12 |
|
13 | | -use crate::block_committer::input::{ |
14 | | - contract_address_into_node_index, |
15 | | - try_node_index_into_contract_address, |
16 | | - StarknetStorageKey, |
17 | | -}; |
| 13 | +use crate::block_committer::input::{contract_address_into_node_index, StarknetStorageKey}; |
18 | 14 | use crate::db::db_layout::DbLayout; |
19 | 15 | use crate::db::facts_db::FactsNodeLayout; |
20 | | -use crate::db::trie_traversal::{fetch_patricia_paths, get_address_and_storage_root}; |
| 16 | +use crate::db::trie_traversal::{fetch_contract_storage_paths, fetch_patricia_paths}; |
21 | 17 | use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState; |
22 | 18 | use crate::patricia_merkle_tree::types::{ |
23 | 19 | class_hash_into_node_index, |
24 | | - ContractsTrieProof, |
25 | 20 | RootHashes, |
26 | 21 | StarknetForestProofs, |
27 | 22 | }; |
@@ -132,6 +127,44 @@ pub async fn fetch_all_patricia_paths<Layout>( |
132 | 127 | contract_sorted_leaf_indices: SortedLeafIndices<'_>, |
133 | 128 | contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>, |
134 | 129 | ) -> TraversalResult<StarknetForestProofs> |
| 130 | +where |
| 131 | + Layout: DbLayout, |
| 132 | + Layout::ContractStateDbLeaf: AsRef<ContractState> + Into<ContractState>, |
| 133 | + Layout::NodeLayout: Send + 'static, |
| 134 | +{ |
| 135 | + let (classes_trie_proof, contracts_proof_nodes, contract_leaves) = |
| 136 | + fetch_classes_and_contracts_patricia_paths::<Layout>( |
| 137 | + storage, |
| 138 | + classes_trie_root_hash, |
| 139 | + contracts_trie_root_hash, |
| 140 | + class_sorted_leaf_indices, |
| 141 | + contract_sorted_leaf_indices, |
| 142 | + contract_storage_sorted_leaf_indices, |
| 143 | + ) |
| 144 | + .await?; |
| 145 | + let contracts_trie_storage_proofs = |
| 146 | + fetch_contract_storage_paths::<Layout::NodeLayout, Layout::ContractStateDbLeaf>( |
| 147 | + storage, |
| 148 | + contract_storage_sorted_leaf_indices, |
| 149 | + &contract_leaves, |
| 150 | + ) |
| 151 | + .await?; |
| 152 | + Ok(StarknetForestProofs::build::<Layout>( |
| 153 | + classes_trie_proof, |
| 154 | + contracts_proof_nodes, |
| 155 | + contract_leaves, |
| 156 | + contracts_trie_storage_proofs, |
| 157 | + )) |
| 158 | +} |
| 159 | + |
| 160 | +async fn fetch_classes_and_contracts_patricia_paths<Layout>( |
| 161 | + storage: &mut impl ReadOnlyStorage, |
| 162 | + classes_trie_root_hash: HashOutput, |
| 163 | + contracts_trie_root_hash: HashOutput, |
| 164 | + class_sorted_leaf_indices: SortedLeafIndices<'_>, |
| 165 | + contract_sorted_leaf_indices: SortedLeafIndices<'_>, |
| 166 | + contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>, |
| 167 | +) -> TraversalResult<(PreimageMap, PreimageMap, HashMap<NodeIndex, Layout::ContractStateDbLeaf>)> |
135 | 168 | where |
136 | 169 | Layout: DbLayout, |
137 | 170 | Layout::ContractStateDbLeaf: AsRef<ContractState> + Into<ContractState>, |
@@ -176,53 +209,7 @@ where |
176 | 209 | ) |
177 | 210 | .await?; |
178 | 211 |
|
179 | | - // Contracts storage tries. |
180 | | - let mut contracts_trie_storage_proofs = |
181 | | - HashMap::with_capacity(contract_storage_sorted_leaf_indices.len()); |
182 | | - |
183 | | - for (idx, sorted_leaf_indices) in contract_storage_sorted_leaf_indices { |
184 | | - let Some((contract_address, storage_root_hash)) = |
185 | | - get_address_and_storage_root(idx, &leaves) |
186 | | - else { |
187 | | - continue; |
188 | | - }; |
189 | | - // No need to fetch the leaves. |
190 | | - let leaves = None; |
191 | | - let proof = fetch_patricia_paths::<Layout::StarknetStorageValueDbLeaf, Layout::NodeLayout>( |
192 | | - storage, |
193 | | - storage_root_hash, |
194 | | - *sorted_leaf_indices, |
195 | | - leaves, |
196 | | - &contract_address, |
197 | | - ) |
198 | | - .await?; |
199 | | - contracts_trie_storage_proofs.insert(contract_address, proof); |
200 | | - } |
201 | | - |
202 | | - // Convert contract_leaves_data keys from NodeIndex to ContractAddress. |
203 | | - let contract_leaves_data: HashMap<ContractAddress, ContractState> = leaves |
204 | | - .into_iter() |
205 | | - .map(|(idx, contract_state_leaf)| { |
206 | | - ( |
207 | | - try_node_index_into_contract_address(&idx).unwrap_or_else(|_| { |
208 | | - panic!( |
209 | | - "Converting leaf NodeIndex to ContractAddress should succeed; failed to \ |
210 | | - convert {idx:?}." |
211 | | - ) |
212 | | - }), |
213 | | - contract_state_leaf.into(), |
214 | | - ) |
215 | | - }) |
216 | | - .collect(); |
217 | | - |
218 | | - Ok(StarknetForestProofs { |
219 | | - classes_trie_proof, |
220 | | - contracts_trie_proof: ContractsTrieProof { |
221 | | - nodes: contracts_proof_nodes, |
222 | | - leaves: contract_leaves_data, |
223 | | - }, |
224 | | - contracts_trie_storage_proofs, |
225 | | - }) |
| 212 | + Ok((classes_trie_proof, contracts_proof_nodes, leaves)) |
226 | 213 | } |
227 | 214 |
|
228 | 215 | /// Fetch the Patricia paths (inner nodes) in the classes trie, contracts trie, |
|
0 commit comments