Skip to content

Commit 693fb7e

Browse files
committed
starknet_committer: fetch patricia paths concurrently
1 parent a82c2c2 commit 693fb7e

1 file changed

Lines changed: 55 additions & 37 deletions

File tree

  • crates/starknet_committer/src/patricia_merkle_tree

crates/starknet_committer/src/patricia_merkle_tree/tree.rs

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::HashMap;
33
use blockifier::state::accessed_keys::AccessedKeys;
44
use starknet_api::core::ContractAddress;
55
use starknet_api::hash::HashOutput;
6+
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::PreimageMap;
67
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
78
use starknet_patricia::patricia_merkle_tree::traversal::TraversalResult;
89
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
@@ -17,7 +18,7 @@ use crate::block_committer::input::{
1718
};
1819
use crate::db::db_layout::DbLayout;
1920
use crate::db::facts_db::FactsNodeLayout;
20-
use crate::db::trie_traversal::fetch_patricia_paths;
21+
use crate::db::trie_traversal::{fetch_contract_storage_paths, fetch_patricia_paths};
2122
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
2223
use crate::patricia_merkle_tree::types::{
2324
class_hash_into_node_index,
@@ -132,6 +133,44 @@ pub async fn fetch_all_patricia_paths<Layout>(
132133
contract_sorted_leaf_indices: SortedLeafIndices<'_>,
133134
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
134135
) -> TraversalResult<StarknetForestProofs>
136+
where
137+
Layout: DbLayout,
138+
Layout::ContractStateDbLeaf: AsRef<ContractState> + Into<ContractState>,
139+
Layout::NodeLayout: Send + 'static,
140+
{
141+
let (classes_trie_proof, contracts_proof_nodes, contract_leaves) =
142+
fetch_classes_and_contracts_patricia_paths::<Layout>(
143+
storage,
144+
classes_trie_root_hash,
145+
contracts_trie_root_hash,
146+
class_sorted_leaf_indices,
147+
contract_sorted_leaf_indices,
148+
contract_storage_sorted_leaf_indices,
149+
)
150+
.await?;
151+
let contracts_trie_storage_proofs =
152+
fetch_contract_storage_paths::<Layout::NodeLayout, Layout::ContractStateDbLeaf>(
153+
storage,
154+
contract_storage_sorted_leaf_indices,
155+
&contract_leaves,
156+
)
157+
.await?;
158+
Ok(build_starknet_forest_proofs::<Layout>(
159+
classes_trie_proof,
160+
contracts_proof_nodes,
161+
contract_leaves,
162+
contracts_trie_storage_proofs,
163+
))
164+
}
165+
166+
async fn fetch_classes_and_contracts_patricia_paths<Layout>(
167+
storage: &mut impl ReadOnlyStorage,
168+
classes_trie_root_hash: HashOutput,
169+
contracts_trie_root_hash: HashOutput,
170+
class_sorted_leaf_indices: SortedLeafIndices<'_>,
171+
contract_sorted_leaf_indices: SortedLeafIndices<'_>,
172+
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
173+
) -> TraversalResult<(PreimageMap, PreimageMap, HashMap<NodeIndex, Layout::ContractStateDbLeaf>)>
135174
where
136175
Layout: DbLayout,
137176
Layout::ContractStateDbLeaf: AsRef<ContractState> + Into<ContractState>,
@@ -176,42 +215,21 @@ where
176215
)
177216
.await?;
178217

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 contract_address = try_node_index_into_contract_address(idx).unwrap_or_else(|_| {
185-
panic!(
186-
"Converting leaf NodeIndex to ContractAddress should succeed; failed to convert \
187-
{idx:?}."
188-
)
189-
});
190-
191-
// The contract address might not exist in the contracts trie in the following cases:
192-
// 1. We are looking at the previous tree and the contract is new.
193-
// 2. We are looking at the new tree and the contract is deleted (revert).
194-
// In either case, the storage trie of this contract is empty, so there is nothing to
195-
// prove regarding the contract storage.
196-
let Some(storage_root_hash) = leaves.get(idx).map(|leaf| leaf.as_ref().storage_root_hash)
197-
else {
198-
continue;
199-
};
200-
// No need to fetch the leaves.
201-
let leaves = None;
202-
let proof = fetch_patricia_paths::<Layout::StarknetStorageValueDbLeaf, Layout::NodeLayout>(
203-
storage,
204-
storage_root_hash,
205-
*sorted_leaf_indices,
206-
leaves,
207-
&contract_address,
208-
)
209-
.await?;
210-
contracts_trie_storage_proofs.insert(contract_address, proof);
211-
}
218+
Ok((classes_trie_proof, contracts_proof_nodes, leaves))
219+
}
212220

221+
fn build_starknet_forest_proofs<Layout>(
222+
classes_trie_proof: PreimageMap,
223+
contracts_proof_nodes: PreimageMap,
224+
contract_leaves: HashMap<NodeIndex, Layout::ContractStateDbLeaf>,
225+
contracts_trie_storage_proofs: HashMap<ContractAddress, PreimageMap>,
226+
) -> StarknetForestProofs
227+
where
228+
Layout: DbLayout,
229+
Layout::ContractStateDbLeaf: Into<ContractState>,
230+
{
213231
// Convert contract_leaves_data keys from NodeIndex to ContractAddress.
214-
let contract_leaves_data: HashMap<ContractAddress, ContractState> = leaves
232+
let contract_leaves_data: HashMap<ContractAddress, ContractState> = contract_leaves
215233
.into_iter()
216234
.map(|(idx, contract_state_leaf)| {
217235
(
@@ -226,14 +244,14 @@ where
226244
})
227245
.collect();
228246

229-
Ok(StarknetForestProofs {
247+
StarknetForestProofs {
230248
classes_trie_proof,
231249
contracts_trie_proof: ContractsTrieProof {
232250
nodes: contracts_proof_nodes,
233251
leaves: contract_leaves_data,
234252
},
235253
contracts_trie_storage_proofs,
236-
})
254+
}
237255
}
238256

239257
/// Fetch the Patricia paths (inner nodes) in the classes trie, contracts trie,

0 commit comments

Comments
 (0)