|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | | -use async_trait::async_trait; |
4 | 3 | use starknet_api::core::{ClassHash, ContractAddress, Nonce}; |
5 | 4 | use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices}; |
6 | 5 | use starknet_types_core::felt::Felt; |
@@ -30,101 +29,92 @@ use crate::patricia_merkle_tree::types::class_hash_into_node_index; |
30 | 29 |
|
31 | 30 | pub type BlockCommitmentResult<T> = Result<T, BlockCommitmentError>; |
32 | 31 |
|
33 | | -// TODO(Yoav): Remove this trait when the index layout is ready. |
34 | | -#[async_trait] |
35 | | -pub trait CommitBlockTrait: Send { |
36 | | - async fn commit_block<Reader: ForestReader + Send, M: MeasurementsTrait + Send>( |
37 | | - input: Input<Reader::InitialReadContext>, |
38 | | - trie_reader: &mut Reader, |
39 | | - measurements: &mut M, |
40 | | - ) -> BlockCommitmentResult<(FilledForest, DeletedNodes)> { |
41 | | - let (mut storage_tries_indices, mut contracts_trie_indices, mut classes_trie_indices) = |
42 | | - get_all_modified_indices(&input.state_diff); |
43 | | - let n_contracts_trie_modifications = contracts_trie_indices.len(); |
44 | | - let forest_sorted_indices = ForestSortedIndices { |
45 | | - storage_tries_sorted_indices: storage_tries_indices |
46 | | - .iter_mut() |
47 | | - .map(|(address, indices)| (*address, SortedLeafIndices::new(indices))) |
48 | | - .collect(), |
49 | | - contracts_trie_sorted_indices: SortedLeafIndices::new(&mut contracts_trie_indices), |
50 | | - classes_trie_sorted_indices: SortedLeafIndices::new(&mut classes_trie_indices), |
51 | | - }; |
52 | | - let actual_storage_updates = input.state_diff.actual_storage_updates(); |
53 | | - let actual_classes_updates = input.state_diff.actual_classes_updates(); |
54 | | - // Record the number of modifications. |
55 | | - measure_number_of_modifications( |
56 | | - measurements, |
57 | | - &actual_storage_updates, |
58 | | - n_contracts_trie_modifications, |
59 | | - actual_classes_updates.len(), |
60 | | - ); |
61 | | - // Reads - fetch_nodes. |
62 | | - measurements.start_measurement(Action::Read); |
63 | | - let roots = |
64 | | - trie_reader.read_roots(input.initial_read_context).await.map_err(ForestError::from)?; |
65 | | - let (original_forest, original_contracts_trie_leaves) = trie_reader |
66 | | - .read( |
67 | | - roots, |
68 | | - &actual_storage_updates, |
69 | | - &actual_classes_updates, |
70 | | - &forest_sorted_indices, |
71 | | - input.config.clone(), |
72 | | - ) |
73 | | - .await?; |
74 | | - let n_read_entries = |
75 | | - original_forest.storage_tries.values().map(|trie| trie.nodes.len()).sum(); |
76 | | - measurements.attempt_to_stop_measurement(Action::Read, n_read_entries).ok(); |
77 | | - debug!("Original skeleton forest created successfully."); |
78 | | - |
79 | | - if input.config.warn_on_trivial_modifications() { |
80 | | - check_trivial_nonce_and_class_hash_updates( |
81 | | - &original_contracts_trie_leaves, |
82 | | - &input.state_diff.address_to_class_hash, |
83 | | - &input.state_diff.address_to_nonce, |
84 | | - ); |
85 | | - } |
86 | | - |
87 | | - // Compute the new topology. |
88 | | - measurements.start_measurement(Action::Compute); |
89 | | - let updated_forest = UpdatedSkeletonForest::create( |
90 | | - &original_forest, |
91 | | - &input.state_diff.skeleton_classes_updates(), |
92 | | - &input.state_diff.skeleton_storage_updates(), |
93 | | - &original_contracts_trie_leaves, |
94 | | - &input.state_diff.address_to_class_hash, |
95 | | - &input.state_diff.address_to_nonce, |
96 | | - )?; |
97 | | - debug!("Updated skeleton forest created successfully."); |
98 | | - |
99 | | - // Find deleted nodes. |
100 | | - let deleted_nodes = find_deleted_nodes( |
101 | | - &original_forest, |
102 | | - &updated_forest, |
| 32 | +pub async fn commit_block<Reader: ForestReader + Send, M: MeasurementsTrait + Send>( |
| 33 | + input: Input<Reader::InitialReadContext>, |
| 34 | + trie_reader: &mut Reader, |
| 35 | + measurements: &mut M, |
| 36 | +) -> BlockCommitmentResult<(FilledForest, DeletedNodes)> { |
| 37 | + let (mut storage_tries_indices, mut contracts_trie_indices, mut classes_trie_indices) = |
| 38 | + get_all_modified_indices(&input.state_diff); |
| 39 | + let n_contracts_trie_modifications = contracts_trie_indices.len(); |
| 40 | + let forest_sorted_indices = ForestSortedIndices { |
| 41 | + storage_tries_sorted_indices: storage_tries_indices |
| 42 | + .iter_mut() |
| 43 | + .map(|(address, indices)| (*address, SortedLeafIndices::new(indices))) |
| 44 | + .collect(), |
| 45 | + contracts_trie_sorted_indices: SortedLeafIndices::new(&mut contracts_trie_indices), |
| 46 | + classes_trie_sorted_indices: SortedLeafIndices::new(&mut classes_trie_indices), |
| 47 | + }; |
| 48 | + let actual_storage_updates = input.state_diff.actual_storage_updates(); |
| 49 | + let actual_classes_updates = input.state_diff.actual_classes_updates(); |
| 50 | + // Record the number of modifications. |
| 51 | + measure_number_of_modifications( |
| 52 | + measurements, |
| 53 | + &actual_storage_updates, |
| 54 | + n_contracts_trie_modifications, |
| 55 | + actual_classes_updates.len(), |
| 56 | + ); |
| 57 | + // Reads - fetch_nodes. |
| 58 | + measurements.start_measurement(Action::Read); |
| 59 | + let roots = |
| 60 | + trie_reader.read_roots(input.initial_read_context).await.map_err(ForestError::from)?; |
| 61 | + let (original_forest, original_contracts_trie_leaves) = trie_reader |
| 62 | + .read( |
| 63 | + roots, |
103 | 64 | &actual_storage_updates, |
104 | 65 | &actual_classes_updates, |
105 | | - &original_contracts_trie_leaves, |
106 | | - ); |
| 66 | + &forest_sorted_indices, |
| 67 | + input.config.clone(), |
| 68 | + ) |
| 69 | + .await?; |
| 70 | + let n_read_entries = original_forest.storage_tries.values().map(|trie| trie.nodes.len()).sum(); |
| 71 | + measurements.attempt_to_stop_measurement(Action::Read, n_read_entries).ok(); |
| 72 | + debug!("Original skeleton forest created successfully."); |
107 | 73 |
|
108 | | - // Compute the new hashes. |
109 | | - let filled_forest = FilledForest::create::<TreeHashFunctionImpl>( |
110 | | - updated_forest, |
111 | | - actual_storage_updates, |
112 | | - actual_classes_updates, |
| 74 | + if input.config.warn_on_trivial_modifications() { |
| 75 | + check_trivial_nonce_and_class_hash_updates( |
113 | 76 | &original_contracts_trie_leaves, |
114 | 77 | &input.state_diff.address_to_class_hash, |
115 | 78 | &input.state_diff.address_to_nonce, |
116 | | - ) |
117 | | - .await?; |
118 | | - measurements.attempt_to_stop_measurement(Action::Compute, 0).ok(); |
119 | | - debug!("Filled forest created successfully."); |
120 | | - |
121 | | - Ok((filled_forest, deleted_nodes)) |
| 79 | + ); |
122 | 80 | } |
123 | | -} |
124 | 81 |
|
125 | | -pub struct CommitBlockImpl; |
126 | | - |
127 | | -impl CommitBlockTrait for CommitBlockImpl {} |
| 82 | + // Compute the new topology. |
| 83 | + measurements.start_measurement(Action::Compute); |
| 84 | + let updated_forest = UpdatedSkeletonForest::create( |
| 85 | + &original_forest, |
| 86 | + &input.state_diff.skeleton_classes_updates(), |
| 87 | + &input.state_diff.skeleton_storage_updates(), |
| 88 | + &original_contracts_trie_leaves, |
| 89 | + &input.state_diff.address_to_class_hash, |
| 90 | + &input.state_diff.address_to_nonce, |
| 91 | + )?; |
| 92 | + debug!("Updated skeleton forest created successfully."); |
| 93 | + |
| 94 | + // Find deleted nodes. |
| 95 | + let deleted_nodes = find_deleted_nodes( |
| 96 | + &original_forest, |
| 97 | + &updated_forest, |
| 98 | + &actual_storage_updates, |
| 99 | + &actual_classes_updates, |
| 100 | + &original_contracts_trie_leaves, |
| 101 | + ); |
| 102 | + |
| 103 | + // Compute the new hashes. |
| 104 | + let filled_forest = FilledForest::create::<TreeHashFunctionImpl>( |
| 105 | + updated_forest, |
| 106 | + actual_storage_updates, |
| 107 | + actual_classes_updates, |
| 108 | + &original_contracts_trie_leaves, |
| 109 | + &input.state_diff.address_to_class_hash, |
| 110 | + &input.state_diff.address_to_nonce, |
| 111 | + ) |
| 112 | + .await?; |
| 113 | + measurements.attempt_to_stop_measurement(Action::Compute, 0).ok(); |
| 114 | + debug!("Filled forest created successfully."); |
| 115 | + |
| 116 | + Ok((filled_forest, deleted_nodes)) |
| 117 | +} |
128 | 118 |
|
129 | 119 | /// Compares the previous state's nonce and class hash with the given in the state diff. |
130 | 120 | /// In case of trivial update, logs out a warning for trivial state diff update. |
|
0 commit comments