Skip to content

Commit aa41958

Browse files
committed
starknet_committer: fetch patricia paths concurrently
1 parent 376f97c commit aa41958

2 files changed

Lines changed: 55 additions & 30 deletions

File tree

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,6 @@ where
852852
}
853853

854854
/// Storage task for fetching Patricia paths in a single storage trie.
855-
#[allow(dead_code)]
856855
struct StoragePathsReadTask<'indices, Layout: NodeLayoutFor<StarknetStorageValue>> {
857856
address: ContractAddress,
858857
storage_root_hash: HashOutput,
@@ -928,7 +927,6 @@ where
928927

929928
/// Fetches Patricia proofs for the storage tries. If the storage has a [GatherableStorage] version,
930929
/// then the paths are fetched concurrently. Otherwise, they are fetched sequentially.
931-
#[allow(dead_code)]
932930
pub(crate) async fn fetch_contract_storage_paths<StorageLayout, ContractLeaf>(
933931
storage: &mut impl ReadOnlyStorage,
934932
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
@@ -979,7 +977,6 @@ pub(crate) fn get_address_and_storage_root<ContractLeaf: AsRef<ContractState>>(
979977
}
980978

981979
/// Sequentially fetches Patricia proofs for the storage tries.
982-
#[allow(dead_code)]
983980
async fn fetch_contract_storage_paths_sequentially<StorageLayout, ContractLeaf>(
984981
storage: &mut impl ReadOnlyStorage,
985982
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
@@ -1017,7 +1014,6 @@ where
10171014
}
10181015

10191016
/// Concurrently fetches Patricia proofs for the storage tries.
1020-
#[allow(dead_code)]
10211017
async fn fetch_contract_storage_paths_concurrently<S, StorageLayout, ContractLeaf>(
10221018
storage: &mut S,
10231019
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,

crates/starknet_committer/src/patricia_merkle_tree/tree.rs

Lines changed: 55 additions & 26 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, get_address_and_storage_root};
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,31 +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 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-
}
218+
Ok((classes_trie_proof, contracts_proof_nodes, leaves))
219+
}
201220

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+
{
202231
// Convert contract_leaves_data keys from NodeIndex to ContractAddress.
203-
let contract_leaves_data: HashMap<ContractAddress, ContractState> = leaves
232+
let contract_leaves_data: HashMap<ContractAddress, ContractState> = contract_leaves
204233
.into_iter()
205234
.map(|(idx, contract_state_leaf)| {
206235
(
@@ -215,14 +244,14 @@ where
215244
})
216245
.collect();
217246

218-
Ok(StarknetForestProofs {
247+
StarknetForestProofs {
219248
classes_trie_proof,
220249
contracts_trie_proof: ContractsTrieProof {
221250
nodes: contracts_proof_nodes,
222251
leaves: contract_leaves_data,
223252
},
224253
contracts_trie_storage_proofs,
225-
})
254+
}
226255
}
227256

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

0 commit comments

Comments
 (0)