@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
33use starknet_api:: hash:: HashOutput ;
44use starknet_patricia:: patricia_merkle_tree:: filled_tree:: node:: FilledNode ;
5+ use starknet_patricia:: patricia_merkle_tree:: filled_tree:: node_serde:: NodeContext ;
56use starknet_patricia:: patricia_merkle_tree:: node_data:: inner_node:: {
67 NodeData ,
78 Preimage ,
@@ -10,6 +11,7 @@ use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
1011use starknet_patricia:: patricia_merkle_tree:: node_data:: leaf:: Leaf ;
1112use starknet_patricia:: patricia_merkle_tree:: traversal:: { SubTreeTrait , TraversalResult } ;
1213use starknet_patricia:: patricia_merkle_tree:: types:: { NodeIndex , SortedLeafIndices } ;
14+ use starknet_patricia_storage:: db_object:: { DBObject , HasStaticPrefix } ;
1315use starknet_patricia_storage:: errors:: StorageError ;
1416use starknet_patricia_storage:: storage_trait:: { create_db_key, DbKey , Storage } ;
1517
@@ -23,19 +25,26 @@ pub mod traversal_test;
2325pub async fn calculate_subtrees_roots < ' a , L : Leaf > (
2426 subtrees : & [ FactsSubTree < ' a > ] ,
2527 storage : & mut impl Storage ,
28+ key_context : & <L as HasStaticPrefix >:: KeyContext ,
2629) -> TraversalResult < Vec < FilledNode < L > > > {
2730 let mut subtrees_roots = vec ! [ ] ;
2831 let db_keys: Vec < DbKey > = subtrees
2932 . iter ( )
3033 . map ( |subtree| {
31- create_db_key ( subtree. get_root_prefix :: < L > ( ) . into ( ) , & subtree. root_hash . 0 . to_bytes_be ( ) )
34+ create_db_key (
35+ subtree. get_root_prefix :: < L > ( key_context) ,
36+ & subtree. root_hash . 0 . to_bytes_be ( ) ,
37+ )
3238 } )
3339 . collect ( ) ;
3440
3541 let db_vals = storage. mget ( & db_keys. iter ( ) . collect :: < Vec < & DbKey > > ( ) ) . await ?;
3642 for ( ( subtree, optional_val) , db_key) in subtrees. iter ( ) . zip ( db_vals. iter ( ) ) . zip ( db_keys) {
3743 let Some ( val) = optional_val else { Err ( StorageError :: MissingKey ( db_key) ) ? } ;
38- subtrees_roots. push ( FilledNode :: deserialize ( subtree. root_hash , val, subtree. is_leaf ( ) ) ?)
44+ subtrees_roots. push ( FilledNode :: deserialize (
45+ val,
46+ & NodeContext { is_leaf : subtree. is_leaf ( ) , node_hash : subtree. root_hash } ,
47+ ) ?)
3948 }
4049 Ok ( subtrees_roots)
4150}
@@ -49,6 +58,7 @@ pub async fn fetch_patricia_paths<L: Leaf>(
4958 root_hash : HashOutput ,
5059 sorted_leaf_indices : SortedLeafIndices < ' _ > ,
5160 leaves : Option < & mut HashMap < NodeIndex , L > > ,
61+ key_context : & <L as HasStaticPrefix >:: KeyContext ,
5262) -> TraversalResult < PreimageMap > {
5363 let mut witnesses = PreimageMap :: new ( ) ;
5464
@@ -58,7 +68,14 @@ pub async fn fetch_patricia_paths<L: Leaf>(
5868
5969 let main_subtree = FactsSubTree { sorted_leaf_indices, root_index : NodeIndex :: ROOT , root_hash } ;
6070
61- fetch_patricia_paths_inner :: < L > ( storage, vec ! [ main_subtree] , & mut witnesses, leaves) . await ?;
71+ fetch_patricia_paths_inner :: < L > (
72+ storage,
73+ vec ! [ main_subtree] ,
74+ & mut witnesses,
75+ leaves,
76+ key_context,
77+ )
78+ . await ?;
6279 Ok ( witnesses)
6380}
6481
@@ -74,11 +91,13 @@ pub(crate) async fn fetch_patricia_paths_inner<'a, L: Leaf>(
7491 subtrees : Vec < FactsSubTree < ' a > > ,
7592 witnesses : & mut PreimageMap ,
7693 mut leaves : Option < & mut HashMap < NodeIndex , L > > ,
94+ key_context : & <L as HasStaticPrefix >:: KeyContext ,
7795) -> TraversalResult < ( ) > {
7896 let mut current_subtrees = subtrees;
7997 let mut next_subtrees = Vec :: new ( ) ;
8098 while !current_subtrees. is_empty ( ) {
81- let filled_roots = calculate_subtrees_roots :: < L > ( & current_subtrees, storage) . await ?;
99+ let filled_roots =
100+ calculate_subtrees_roots :: < L > ( & current_subtrees, storage, key_context) . await ?;
82101 for ( filled_root, subtree) in filled_roots. into_iter ( ) . zip ( current_subtrees. iter ( ) ) {
83102 // Always insert root.
84103 // No need to insert an unmodified node (which is not the root), because its parent is
0 commit comments