Skip to content

Commit a01772e

Browse files
starknet_committer,starknet_patricia: don't use impl Storage, use generics
1 parent aa50217 commit a01772e

File tree

10 files changed

+53
-47
lines changed

10 files changed

+53
-47
lines changed

crates/starknet_committer/src/block_committer/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::patricia_merkle_tree::types::class_hash_into_node_index;
2323

2424
type BlockCommitmentResult<T> = Result<T, BlockCommitmentError>;
2525

26-
pub async fn commit_block<S: Storage>(
26+
pub async fn commit_block<S: Storage + ?Sized>(
2727
input: Input<ConfigImpl>,
2828
storage: &mut S,
2929
mut time_measurement: Option<&mut TimeMeasurement>,

crates/starknet_committer/src/forest/filled_forest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct FilledForest {
3434
impl FilledForest {
3535
/// Writes the node serialization of the filled trees to storage. Returns the number of new
3636
/// objects written to storage.
37-
pub fn write_to_storage(&self, storage: &mut impl Storage) -> usize {
37+
pub fn write_to_storage<S: Storage + ?Sized>(&self, storage: &mut S) -> usize {
3838
// Serialize all trees to one hash map.
3939
let new_db_objects: DbHashMap = self
4040
.storage_tries

crates/starknet_committer/src/forest/original_skeleton_forest.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<'a> OriginalSkeletonForest<'a> {
3535
/// Creates an original skeleton forest that includes the storage tries of the modified
3636
/// contracts, the classes trie and the contracts trie. Additionally, returns the original
3737
/// contract states that are needed to compute the contract state tree.
38-
pub(crate) fn create(
39-
storage: &mut impl Storage,
38+
pub(crate) fn create<S: Storage + ?Sized>(
39+
storage: &mut S,
4040
contracts_trie_root_hash: HashOutput,
4141
classes_trie_root_hash: HashOutput,
4242
storage_updates: &HashMap<ContractAddress, LeafModifications<StarknetStorageValue>>,
@@ -72,9 +72,9 @@ impl<'a> OriginalSkeletonForest<'a> {
7272

7373
/// Creates the contracts trie original skeleton.
7474
/// Also returns the previous contracts state of the modified contracts.
75-
fn create_contracts_trie(
75+
fn create_contracts_trie<S: Storage + ?Sized>(
7676
contracts_trie_root_hash: HashOutput,
77-
storage: &mut impl Storage,
77+
storage: &mut S,
7878
contracts_trie_sorted_indices: SortedLeafIndices<'a>,
7979
) -> ForestResult<(OriginalSkeletonTreeImpl<'a>, HashMap<NodeIndex, ContractState>)> {
8080
Ok(OriginalSkeletonTreeImpl::create_and_get_previous_leaves(
@@ -86,10 +86,10 @@ impl<'a> OriginalSkeletonForest<'a> {
8686
)?)
8787
}
8888

89-
fn create_storage_tries(
89+
fn create_storage_tries<S: Storage + ?Sized>(
9090
actual_storage_updates: &HashMap<ContractAddress, LeafModifications<StarknetStorageValue>>,
9191
original_contracts_trie_leaves: &HashMap<NodeIndex, ContractState>,
92-
storage: &mut impl Storage,
92+
storage: &mut S,
9393
config: &impl Config,
9494
storage_tries_sorted_indices: &HashMap<ContractAddress, SortedLeafIndices<'a>>,
9595
) -> ForestResult<HashMap<ContractAddress, OriginalSkeletonTreeImpl<'a>>> {
@@ -116,10 +116,10 @@ impl<'a> OriginalSkeletonForest<'a> {
116116
Ok(storage_tries)
117117
}
118118

119-
fn create_classes_trie(
119+
fn create_classes_trie<S: Storage + ?Sized>(
120120
actual_classes_updates: &LeafModifications<CompiledClassHash>,
121121
classes_trie_root_hash: HashOutput,
122-
storage: &mut impl Storage,
122+
storage: &mut S,
123123
config: &impl Config,
124124
contracts_trie_sorted_indices: SortedLeafIndices<'a>,
125125
) -> ForestResult<OriginalSkeletonTreeImpl<'a>> {

crates/starknet_committer/src/patricia_merkle_tree/tree.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl OriginalSkeletonContractsTrieConfig {
4444
/// Fetch the leaves in the contracts trie only, to be able to get the storage root hashes.
4545
/// Assumption: `contract_sorted_leaf_indices` contains all `contract_storage_sorted_leaf_indices`
4646
/// keys.
47-
fn fetch_all_patricia_paths(
48-
storage: &mut impl Storage,
47+
fn fetch_all_patricia_paths<S: Storage + ?Sized>(
48+
storage: &mut S,
4949
classes_trie_root_hash: HashOutput,
5050
contracts_trie_root_hash: HashOutput,
5151
class_sorted_leaf_indices: SortedLeafIndices<'_>,
@@ -70,7 +70,7 @@ fn fetch_all_patricia_paths(
7070

7171
// Classes trie - no need to fetch the leaves.
7272
let leaves = None;
73-
let classes_trie_proof = fetch_patricia_paths::<CompiledClassHash>(
73+
let classes_trie_proof = fetch_patricia_paths::<S, CompiledClassHash>(
7474
storage,
7575
classes_trie_root_hash,
7676
class_sorted_leaf_indices,
@@ -79,7 +79,7 @@ fn fetch_all_patricia_paths(
7979

8080
// Contracts trie - the leaves are required.
8181
let mut leaves = HashMap::new();
82-
let contracts_proof_nodes = fetch_patricia_paths::<ContractState>(
82+
let contracts_proof_nodes = fetch_patricia_paths::<S, ContractState>(
8383
storage,
8484
contracts_trie_root_hash,
8585
contract_sorted_leaf_indices,
@@ -97,7 +97,7 @@ fn fetch_all_patricia_paths(
9797
.storage_root_hash;
9898
// No need to fetch the leaves.
9999
let leaves = None;
100-
let proof = fetch_patricia_paths::<StarknetStorageValue>(
100+
let proof = fetch_patricia_paths::<S, StarknetStorageValue>(
101101
storage,
102102
storage_root_hash,
103103
*sorted_leaf_indices,

crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/create_tree.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
3333
/// Given a list of subtrees, traverses towards their leaves and fetches all non-empty,
3434
/// unmodified nodes. If `compare_modified_leaves` is set, function logs out a warning when
3535
/// encountering a trivial modification. Fills the previous leaf values if it is not none.
36-
fn fetch_nodes<L: Leaf>(
36+
fn fetch_nodes<S: Storage + ?Sized, L: Leaf>(
3737
&mut self,
3838
subtrees: Vec<SubTree<'a>>,
39-
storage: &mut impl Storage,
39+
storage: &mut S,
4040
leaf_modifications: &LeafModifications<L>,
4141
config: &impl OriginalSkeletonTreeConfig<L>,
4242
mut previous_leaves: Option<&mut HashMap<NodeIndex, L>>,
@@ -47,7 +47,7 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
4747
let should_fetch_modified_leaves =
4848
config.compare_modified_leaves() || previous_leaves.is_some();
4949
let mut next_subtrees = Vec::new();
50-
let filled_roots = calculate_subtrees_roots::<L>(&subtrees, storage)?;
50+
let filled_roots = calculate_subtrees_roots::<S, L>(&subtrees, storage)?;
5151
for (filled_root, subtree) in filled_roots.into_iter().zip(subtrees.iter()) {
5252
match filled_root.data {
5353
// Binary node.
@@ -127,11 +127,17 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
127127
}
128128
}
129129
}
130-
self.fetch_nodes::<L>(next_subtrees, storage, leaf_modifications, config, previous_leaves)
130+
self.fetch_nodes::<S, L>(
131+
next_subtrees,
132+
storage,
133+
leaf_modifications,
134+
config,
135+
previous_leaves,
136+
)
131137
}
132138

133-
pub(crate) fn create_impl<L: Leaf>(
134-
storage: &mut impl Storage,
139+
pub(crate) fn create_impl<S: Storage + ?Sized, L: Leaf>(
140+
storage: &mut S,
135141
root_hash: HashOutput,
136142
sorted_leaf_indices: SortedLeafIndices<'a>,
137143
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -150,7 +156,7 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
150156
}
151157
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
152158
let mut skeleton_tree = Self { nodes: HashMap::new(), sorted_leaf_indices };
153-
skeleton_tree.fetch_nodes::<L>(
159+
skeleton_tree.fetch_nodes::<S, L>(
154160
vec![main_subtree],
155161
storage,
156162
leaf_modifications,
@@ -160,8 +166,8 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
160166
Ok(skeleton_tree)
161167
}
162168

163-
pub(crate) fn create_and_get_previous_leaves_impl<L: Leaf>(
164-
storage: &mut impl Storage,
169+
pub(crate) fn create_and_get_previous_leaves_impl<S: Storage + ?Sized, L: Leaf>(
170+
storage: &mut S,
165171
root_hash: HashOutput,
166172
sorted_leaf_indices: SortedLeafIndices<'a>,
167173
leaf_modifications: &LeafModifications<L>,
@@ -180,7 +186,7 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
180186
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
181187
let mut skeleton_tree = Self { nodes: HashMap::new(), sorted_leaf_indices };
182188
let mut leaves = HashMap::new();
183-
skeleton_tree.fetch_nodes::<L>(
189+
skeleton_tree.fetch_nodes::<S, L>(
184190
vec![main_subtree],
185191
storage,
186192
leaf_modifications,

crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/create_tree_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn test_create_tree(
210210
let config = OriginalSkeletonMockTrieConfig::new(compare_modified_leaves);
211211
let mut sorted_leaf_indices: Vec<NodeIndex> = leaf_modifications.keys().copied().collect();
212212
let sorted_leaf_indices = SortedLeafIndices::new(&mut sorted_leaf_indices);
213-
let skeleton_tree = OriginalSkeletonTreeImpl::create::<MockLeaf>(
213+
let skeleton_tree = OriginalSkeletonTreeImpl::create::<MapStorage, MockLeaf>(
214214
&mut storage,
215215
root_hash,
216216
sorted_leaf_indices,

crates/starknet_patricia/src/patricia_merkle_tree/original_skeleton_tree/tree.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub type OriginalSkeletonTreeResult<T> = Result<T, OriginalSkeletonTreeError>;
2020
/// update. It also contains the hashes (for edge siblings - also the edge data) of the unmodified
2121
/// nodes on the Merkle paths from the updated leaves to the root.
2222
pub trait OriginalSkeletonTree<'a>: Sized {
23-
fn create<L: Leaf>(
24-
storage: &mut impl Storage,
23+
fn create<S: Storage + ?Sized, L: Leaf>(
24+
storage: &mut S,
2525
root_hash: HashOutput,
2626
sorted_leaf_indices: SortedLeafIndices<'a>,
2727
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -32,8 +32,8 @@ pub trait OriginalSkeletonTree<'a>: Sized {
3232

3333
fn get_nodes_mut(&mut self) -> &mut OriginalSkeletonNodeMap;
3434

35-
fn create_and_get_previous_leaves<L: Leaf>(
36-
storage: &mut impl Storage,
35+
fn create_and_get_previous_leaves<S: Storage + ?Sized, L: Leaf>(
36+
storage: &mut S,
3737
root_hash: HashOutput,
3838
sorted_leaf_indices: SortedLeafIndices<'a>,
3939
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -52,8 +52,8 @@ pub struct OriginalSkeletonTreeImpl<'a> {
5252
}
5353

5454
impl<'a> OriginalSkeletonTree<'a> for OriginalSkeletonTreeImpl<'a> {
55-
fn create<L: Leaf>(
56-
storage: &mut impl Storage,
55+
fn create<S: Storage + ?Sized, L: Leaf>(
56+
storage: &mut S,
5757
root_hash: HashOutput,
5858
sorted_leaf_indices: SortedLeafIndices<'a>,
5959
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -70,8 +70,8 @@ impl<'a> OriginalSkeletonTree<'a> for OriginalSkeletonTreeImpl<'a> {
7070
&mut self.nodes
7171
}
7272

73-
fn create_and_get_previous_leaves<L: Leaf>(
74-
storage: &mut impl Storage,
73+
fn create_and_get_previous_leaves<S: Storage + ?Sized, L: Leaf>(
74+
storage: &mut S,
7575
root_hash: HashOutput,
7676
sorted_leaf_indices: SortedLeafIndices<'a>,
7777
config: &impl OriginalSkeletonTreeConfig<L>,
@@ -92,8 +92,8 @@ impl<'a> OriginalSkeletonTree<'a> for OriginalSkeletonTreeImpl<'a> {
9292
}
9393

9494
impl<'a> OriginalSkeletonTreeImpl<'a> {
95-
pub fn get_leaves<L: Leaf>(
96-
storage: &mut impl Storage,
95+
pub fn get_leaves<S: Storage + ?Sized, L: Leaf>(
96+
storage: &mut S,
9797
root_hash: HashOutput,
9898
sorted_leaf_indices: SortedLeafIndices<'a>,
9999
) -> OriginalSkeletonTreeResult<HashMap<NodeIndex, L>> {

crates/starknet_patricia/src/patricia_merkle_tree/traversal.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ impl<'a> SubTree<'a> {
125125
}
126126

127127
// TODO(Aviv, 17/07/2024): Split between storage prefix implementation and function logic.
128-
pub(crate) fn calculate_subtrees_roots<'a, L: Leaf>(
128+
pub(crate) fn calculate_subtrees_roots<'a, S: Storage + ?Sized, L: Leaf>(
129129
subtrees: &[SubTree<'a>],
130-
storage: &mut impl Storage,
130+
storage: &mut S,
131131
) -> TraversalResult<Vec<FilledNode<L>>> {
132132
let mut subtrees_roots = vec![];
133133
let db_keys: Vec<DbKey> = subtrees
@@ -149,8 +149,8 @@ pub(crate) fn calculate_subtrees_roots<'a, L: Leaf>(
149149
/// given tree according to the `root_hash`.
150150
/// If `leaves` is not `None`, it also fetches the modified leaves and inserts them into the
151151
/// provided map.
152-
pub fn fetch_patricia_paths<L: Leaf>(
153-
storage: &mut impl Storage,
152+
pub fn fetch_patricia_paths<S: Storage + ?Sized, L: Leaf>(
153+
storage: &mut S,
154154
root_hash: HashOutput,
155155
sorted_leaf_indices: SortedLeafIndices<'_>,
156156
leaves: Option<&mut HashMap<NodeIndex, L>>,
@@ -163,7 +163,7 @@ pub fn fetch_patricia_paths<L: Leaf>(
163163

164164
let main_subtree = SubTree { sorted_leaf_indices, root_index: NodeIndex::ROOT, root_hash };
165165

166-
fetch_patricia_paths_inner::<L>(storage, vec![main_subtree], &mut witnesses, leaves)?;
166+
fetch_patricia_paths_inner::<S, L>(storage, vec![main_subtree], &mut witnesses, leaves)?;
167167
Ok(witnesses)
168168
}
169169

@@ -174,8 +174,8 @@ pub fn fetch_patricia_paths<L: Leaf>(
174174
/// inner nodes in their paths.
175175
/// If `leaves` is not `None`, it also fetches the modified leaves and inserts them into the
176176
/// provided map.
177-
fn fetch_patricia_paths_inner<'a, L: Leaf>(
178-
storage: &mut impl Storage,
177+
fn fetch_patricia_paths_inner<'a, S: Storage + ?Sized, L: Leaf>(
178+
storage: &mut S,
179179
subtrees: Vec<SubTree<'a>>,
180180
witnesses: &mut PreimageMap,
181181
mut leaves: Option<&mut HashMap<NodeIndex, L>>,
@@ -184,7 +184,7 @@ fn fetch_patricia_paths_inner<'a, L: Leaf>(
184184
return Ok(());
185185
}
186186
let mut next_subtrees = Vec::new();
187-
let filled_roots = calculate_subtrees_roots::<L>(&subtrees, storage)?;
187+
let filled_roots = calculate_subtrees_roots::<S, L>(&subtrees, storage)?;
188188
for (filled_root, subtree) in filled_roots.into_iter().zip(subtrees.iter()) {
189189
// Always insert root.
190190
// No need to insert an unmodified node (which is not the root), because its parent is
@@ -225,5 +225,5 @@ fn fetch_patricia_paths_inner<'a, L: Leaf>(
225225
}
226226
}
227227
}
228-
fetch_patricia_paths_inner::<L>(storage, next_subtrees, witnesses, leaves)
228+
fetch_patricia_paths_inner::<S, L>(storage, next_subtrees, witnesses, leaves)
229229
}

crates/starknet_patricia/src/patricia_merkle_tree/traversal_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ fn test_fetch_patricia_paths_inner(
648648
let mut nodes = HashMap::new();
649649
let mut fetched_leaves = HashMap::new();
650650

651-
fetch_patricia_paths_inner::<MockLeaf>(
651+
fetch_patricia_paths_inner::<MapStorage, MockLeaf>(
652652
&mut storage,
653653
vec![main_subtree],
654654
&mut nodes,

crates/starknet_patricia/src/patricia_merkle_tree/updated_skeleton_tree/create_tree_helper_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ async fn test_update_non_modified_storage_tree(#[case] root_hash: HashOutput) {
498498
let empty_map = HashMap::new();
499499
let mut empty_storage = MapStorage::default();
500500
let config = OriginalSkeletonMockTrieConfig::new(false);
501-
let mut original_skeleton_tree = OriginalSkeletonTreeImpl::create_impl::<MockLeaf>(
501+
let mut original_skeleton_tree = OriginalSkeletonTreeImpl::create_impl::<MapStorage, MockLeaf>(
502502
&mut empty_storage,
503503
root_hash,
504504
SortedLeafIndices::new(&mut []),

0 commit comments

Comments
 (0)