Skip to content

Commit 526b5f0

Browse files
committed
starknet_committer: abstract fetch_all_patricia_paths
1 parent d746081 commit 526b5f0

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

  • crates/starknet_committer/src/patricia_merkle_tree

crates/starknet_committer/src/patricia_merkle_tree/tree.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::block_committer::input::{
1414
StarknetStorageKey,
1515
StarknetStorageValue,
1616
};
17+
use crate::db::db_layout::DbLayout;
1718
use crate::db::facts_db::FactsNodeLayout;
1819
use crate::db::trie_traversal::fetch_patricia_paths;
1920
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
@@ -55,14 +56,18 @@ impl OriginalSkeletonTreeConfig for OriginalSkeletonTrieConfig {
5556
/// Fetch the leaves in the contracts trie only, to be able to get the storage root hashes.
5657
/// Assumption: `contract_sorted_leaf_indices` contains all `contract_storage_sorted_leaf_indices`
5758
/// keys.
58-
async fn fetch_all_patricia_paths(
59+
pub async fn fetch_all_patricia_paths<Layout>(
5960
storage: &mut impl ReadOnlyStorage,
6061
classes_trie_root_hash: HashOutput,
6162
contracts_trie_root_hash: HashOutput,
6263
class_sorted_leaf_indices: SortedLeafIndices<'_>,
6364
contract_sorted_leaf_indices: SortedLeafIndices<'_>,
6465
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
65-
) -> TraversalResult<StarknetForestProofs> {
66+
) -> TraversalResult<StarknetForestProofs>
67+
where
68+
Layout: DbLayout,
69+
Layout::ContractStateDbLeaf: Clone + Into<ContractState>,
70+
{
6671
// Verify that all `contract_storage_sorted_leaf_indices` keys are included in
6772
// `contract_sorted_leaf_indices`.
6873
let mut address_counter = 0;
@@ -81,7 +86,8 @@ async fn fetch_all_patricia_paths(
8186

8287
// Classes trie - no need to fetch the leaves.
8388
let leaves = None;
84-
let classes_trie_proof = fetch_patricia_paths::<CompiledClassHash, FactsNodeLayout>(
89+
let classes_trie_proof =
90+
fetch_patricia_paths::<Layout::CompiledClassHashDbLeaf, Layout::NodeLayout>(
8591
storage,
8692
classes_trie_root_hash,
8793
class_sorted_leaf_indices,
@@ -92,7 +98,8 @@ async fn fetch_all_patricia_paths(
9298

9399
// Contracts trie - the leaves are required.
94100
let mut leaves = HashMap::new();
95-
let contracts_proof_nodes = fetch_patricia_paths::<ContractState, FactsNodeLayout>(
101+
let contracts_proof_nodes =
102+
fetch_patricia_paths::<Layout::ContractStateDbLeaf, Layout::NodeLayout>(
96103
storage,
97104
contracts_trie_root_hash,
98105
contract_sorted_leaf_indices,
@@ -118,12 +125,14 @@ async fn fetch_all_patricia_paths(
118125
// 2. We are looking at the new tree and the contract is deleted (revert).
119126
// In either case, the storage trie of this contract is empty, so there is nothing to
120127
// prove regarding the contract storage.
121-
let Some(storage_root_hash) = leaves.get(idx).map(|leaf| leaf.storage_root_hash) else {
128+
let Some(storage_root_hash) =
129+
leaves.get(idx).map(|leaf| leaf.clone().into().storage_root_hash)
130+
else {
122131
continue;
123132
};
124133
// No need to fetch the leaves.
125134
let leaves = None;
126-
let proof = fetch_patricia_paths::<StarknetStorageValue, FactsNodeLayout>(
135+
let proof = fetch_patricia_paths::<Layout::StarknetStorageValueDbLeaf, Layout::NodeLayout>(
127136
storage,
128137
storage_root_hash,
129138
*sorted_leaf_indices,
@@ -137,15 +146,15 @@ async fn fetch_all_patricia_paths(
137146
// Convert contract_leaves_data keys from NodeIndex to ContractAddress.
138147
let contract_leaves_data: HashMap<ContractAddress, ContractState> = leaves
139148
.into_iter()
140-
.map(|(idx, v)| {
149+
.map(|(idx, contract_state_leaf)| {
141150
(
142151
try_node_index_into_contract_address(&idx).unwrap_or_else(|_| {
143152
panic!(
144153
"Converting leaf NodeIndex to ContractAddress should succeed; failed to \
145154
convert {idx:?}."
146155
)
147156
}),
148-
v,
157+
contract_state_leaf.into(),
149158
)
150159
})
151160
.collect();
@@ -163,6 +172,8 @@ async fn fetch_all_patricia_paths(
163172
/// Fetch the Patricia paths (inner nodes) in the classes trie, contracts trie,
164173
/// and contracts storage tries for both the previous and new root hashes.
165174
/// Fetch the leaves in the contracts trie only, to be able to get the storage root hashes.
175+
///
176+
/// Only works with facts-layout storage.
166177
pub async fn fetch_previous_and_new_patricia_paths(
167178
storage: &mut impl ReadOnlyStorage,
168179
classes_trie_root_hashes: RootHashes,
@@ -192,8 +203,7 @@ pub async fn fetch_previous_and_new_patricia_paths(
192203
.iter_mut()
193204
.map(|(address, leaf_indices)| (*address, SortedLeafIndices::new(leaf_indices)))
194205
.collect();
195-
196-
let prev_proofs = fetch_all_patricia_paths(
206+
let prev_proofs = fetch_all_patricia_paths::<FactsNodeLayout>(
197207
storage,
198208
classes_trie_root_hashes.previous_root_hash,
199209
contracts_trie_root_hashes.previous_root_hash,
@@ -202,7 +212,7 @@ pub async fn fetch_previous_and_new_patricia_paths(
202212
contract_storage_sorted_leaf_indices,
203213
)
204214
.await?;
205-
let new_proofs = fetch_all_patricia_paths(
215+
let new_proofs = fetch_all_patricia_paths::<FactsNodeLayout>(
206216
storage,
207217
classes_trie_root_hashes.new_root_hash,
208218
contracts_trie_root_hashes.new_root_hash,

0 commit comments

Comments
 (0)