Skip to content

Commit 780c725

Browse files
committed
starknet_committer: abstract fetch_all_patricia_paths
1 parent 1551e53 commit 780c725

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

  • crates/starknet_committer/src/patricia_merkle_tree

crates/starknet_committer/src/patricia_merkle_tree/tree.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ use crate::block_committer::input::{
1212
contract_address_into_node_index,
1313
try_node_index_into_contract_address,
1414
StarknetStorageKey,
15-
StarknetStorageValue,
1615
};
16+
use crate::db::db_layout::DbLayout;
1717
use crate::db::facts_db::FactsNodeLayout;
1818
use crate::db::trie_traversal::fetch_patricia_paths;
1919
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
2020
use crate::patricia_merkle_tree::types::{
2121
class_hash_into_node_index,
22-
CompiledClassHash,
2322
ContractsTrieProof,
2423
RootHashes,
2524
StarknetForestProofs,
@@ -55,14 +54,18 @@ impl OriginalSkeletonTreeConfig for OriginalSkeletonTrieConfig {
5554
/// Fetch the leaves in the contracts trie only, to be able to get the storage root hashes.
5655
/// Assumption: `contract_sorted_leaf_indices` contains all `contract_storage_sorted_leaf_indices`
5756
/// keys.
58-
async fn fetch_all_patricia_paths(
57+
pub async fn fetch_all_patricia_paths<Layout>(
5958
storage: &mut impl ReadOnlyStorage,
6059
classes_trie_root_hash: HashOutput,
6160
contracts_trie_root_hash: HashOutput,
6261
class_sorted_leaf_indices: SortedLeafIndices<'_>,
6362
contract_sorted_leaf_indices: SortedLeafIndices<'_>,
6463
contract_storage_sorted_leaf_indices: &HashMap<NodeIndex, SortedLeafIndices<'_>>,
65-
) -> TraversalResult<StarknetForestProofs> {
64+
) -> TraversalResult<StarknetForestProofs>
65+
where
66+
Layout: DbLayout,
67+
Layout::ContractStateDbLeaf: AsRef<ContractState> + Into<ContractState>,
68+
{
6669
// Verify that all `contract_storage_sorted_leaf_indices` keys are included in
6770
// `contract_sorted_leaf_indices`.
6871
let mut address_counter = 0;
@@ -81,25 +84,27 @@ async fn fetch_all_patricia_paths(
8184

8285
// Classes trie - no need to fetch the leaves.
8386
let leaves = None;
84-
let classes_trie_proof = fetch_patricia_paths::<CompiledClassHash, FactsNodeLayout>(
85-
storage,
86-
classes_trie_root_hash,
87-
class_sorted_leaf_indices,
88-
leaves,
89-
&EmptyKeyContext,
90-
)
91-
.await?;
87+
let classes_trie_proof =
88+
fetch_patricia_paths::<Layout::CompiledClassHashDbLeaf, Layout::NodeLayout>(
89+
storage,
90+
classes_trie_root_hash,
91+
class_sorted_leaf_indices,
92+
leaves,
93+
&EmptyKeyContext,
94+
)
95+
.await?;
9296

9397
// Contracts trie - the leaves are required.
9498
let mut leaves = HashMap::new();
95-
let contracts_proof_nodes = fetch_patricia_paths::<ContractState, FactsNodeLayout>(
96-
storage,
97-
contracts_trie_root_hash,
98-
contract_sorted_leaf_indices,
99-
Some(&mut leaves),
100-
&EmptyKeyContext,
101-
)
102-
.await?;
99+
let contracts_proof_nodes =
100+
fetch_patricia_paths::<Layout::ContractStateDbLeaf, Layout::NodeLayout>(
101+
storage,
102+
contracts_trie_root_hash,
103+
contract_sorted_leaf_indices,
104+
Some(&mut leaves),
105+
&EmptyKeyContext,
106+
)
107+
.await?;
103108

104109
// Contracts storage tries.
105110
let mut contracts_trie_storage_proofs =
@@ -118,12 +123,13 @@ async fn fetch_all_patricia_paths(
118123
// 2. We are looking at the new tree and the contract is deleted (revert).
119124
// In either case, the storage trie of this contract is empty, so there is nothing to
120125
// prove regarding the contract storage.
121-
let Some(storage_root_hash) = leaves.get(idx).map(|leaf| leaf.storage_root_hash) else {
126+
let Some(storage_root_hash) = leaves.get(idx).map(|leaf| leaf.as_ref().storage_root_hash)
127+
else {
122128
continue;
123129
};
124130
// No need to fetch the leaves.
125131
let leaves = None;
126-
let proof = fetch_patricia_paths::<StarknetStorageValue, FactsNodeLayout>(
132+
let proof = fetch_patricia_paths::<Layout::StarknetStorageValueDbLeaf, Layout::NodeLayout>(
127133
storage,
128134
storage_root_hash,
129135
*sorted_leaf_indices,
@@ -137,15 +143,15 @@ async fn fetch_all_patricia_paths(
137143
// Convert contract_leaves_data keys from NodeIndex to ContractAddress.
138144
let contract_leaves_data: HashMap<ContractAddress, ContractState> = leaves
139145
.into_iter()
140-
.map(|(idx, v)| {
146+
.map(|(idx, contract_state_leaf)| {
141147
(
142148
try_node_index_into_contract_address(&idx).unwrap_or_else(|_| {
143149
panic!(
144150
"Converting leaf NodeIndex to ContractAddress should succeed; failed to \
145151
convert {idx:?}."
146152
)
147153
}),
148-
v,
154+
contract_state_leaf.into(),
149155
)
150156
})
151157
.collect();
@@ -163,6 +169,8 @@ async fn fetch_all_patricia_paths(
163169
/// Fetch the Patricia paths (inner nodes) in the classes trie, contracts trie,
164170
/// and contracts storage tries for both the previous and new root hashes.
165171
/// Fetch the leaves in the contracts trie only, to be able to get the storage root hashes.
172+
///
173+
/// Only works with facts-layout storage.
166174
pub async fn fetch_previous_and_new_patricia_paths(
167175
storage: &mut impl ReadOnlyStorage,
168176
classes_trie_root_hashes: RootHashes,
@@ -192,8 +200,7 @@ pub async fn fetch_previous_and_new_patricia_paths(
192200
.iter_mut()
193201
.map(|(address, leaf_indices)| (*address, SortedLeafIndices::new(leaf_indices)))
194202
.collect();
195-
196-
let prev_proofs = fetch_all_patricia_paths(
203+
let prev_proofs = fetch_all_patricia_paths::<FactsNodeLayout>(
197204
storage,
198205
classes_trie_root_hashes.previous_root_hash,
199206
contracts_trie_root_hashes.previous_root_hash,
@@ -202,7 +209,7 @@ pub async fn fetch_previous_and_new_patricia_paths(
202209
contract_storage_sorted_leaf_indices,
203210
)
204211
.await?;
205-
let new_proofs = fetch_all_patricia_paths(
212+
let new_proofs = fetch_all_patricia_paths::<FactsNodeLayout>(
206213
storage,
207214
classes_trie_root_hashes.new_root_hash,
208215
contracts_trie_root_hashes.new_root_hash,

0 commit comments

Comments
 (0)