@@ -434,29 +434,17 @@ where
434434{
435435 let mut storage_tries = HashMap :: new ( ) ;
436436 for ( address, updates) in actual_storage_updates {
437- let sorted_leaf_indices = storage_tries_sorted_indices
438- . get ( address)
439- . ok_or ( ForestError :: MissingSortedLeafIndices ( * address) ) ?;
440- let contract_state = original_contracts_trie_leaves
441- . get ( & contract_address_into_node_index ( address) )
442- . ok_or ( ForestError :: MissingContractCurrentState ( * address) ) ?;
443- let config = OriginalSkeletonTrieConfig :: new_for_classes_or_storage_trie (
444- config. warn_on_trivial_modifications ( ) ,
445- ) ;
446-
447- let original_skeleton = create_original_skeleton_tree :: < Layout :: DbLeaf , Layout > (
437+ let ( address, original_skeleton) = create_storage_trie_for_contract :: < Layout > (
448438 storage,
449- contract_state. storage_root_hash ,
450- * sorted_leaf_indices,
451- & config,
452- // TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
453- // iterator over borrowed data so that the conversion below is costless.
454- & updates. iter ( ) . map ( |( idx, value) | ( * idx, Layout :: DbLeaf :: from ( * value) ) ) . collect ( ) ,
455- None ,
456- address,
439+ * address,
440+ updates,
441+ original_contracts_trie_leaves,
442+ storage_tries_sorted_indices,
443+ config. warn_on_trivial_modifications ( ) ,
457444 )
458445 . await ?;
459- storage_tries. insert ( * address, original_skeleton) ;
446+
447+ storage_tries. insert ( address, original_skeleton) ;
460448 }
461449 Ok ( storage_tries)
462450}
@@ -478,45 +466,70 @@ where
478466 let mut storage_tries = HashMap :: new ( ) ;
479467
480468 for ( address, updates) in actual_storage_updates {
481- // Extract data needed for this contract.
482- let sorted_leaf_indices = * storage_tries_sorted_indices
483- . get ( address)
484- . ok_or ( ForestError :: MissingSortedLeafIndices ( * address) ) ?;
485- let contract_state = original_contracts_trie_leaves
486- . get ( & contract_address_into_node_index ( address) )
487- . ok_or ( ForestError :: MissingContractCurrentState ( * address) ) ?;
488- let trie_config = OriginalSkeletonTrieConfig :: new_for_classes_or_storage_trie (
489- config. warn_on_trivial_modifications ( ) ,
490- ) ;
491-
492- // TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
493- // iterator over borrowed data so that the conversion below is costless.
494- let leaf_modifications: HashMap <
495- NodeIndex ,
496- <Layout as NodeLayoutFor < StarknetStorageValue > >:: DbLeaf ,
497- > = updates. iter ( ) . map ( |( idx, value) | ( * idx, Layout :: DbLeaf :: from ( * value) ) ) . collect ( ) ;
498-
499469 // Create the future - tokio will poll all futures concurrently.
500470 futures. push ( async move {
501- let original_skeleton = create_original_skeleton_tree :: < Layout :: DbLeaf , Layout > (
471+ create_storage_trie_for_contract :: < Layout > (
502472 storage,
503- contract_state. storage_root_hash ,
504- sorted_leaf_indices,
505- & trie_config,
506- & leaf_modifications,
507- None ,
508- address,
473+ * address,
474+ updates,
475+ original_contracts_trie_leaves,
476+ storage_tries_sorted_indices,
477+ config. warn_on_trivial_modifications ( ) ,
509478 )
510- . await ?;
511- Ok :: < _ , ForestError > ( ( address, original_skeleton) )
479+ . await
512480 } ) ;
513481 }
514482
515483 // Collect all results as they complete.
516484 while let Some ( result) = futures. next ( ) . await {
517485 let ( address, original_skeleton) = result?;
518- storage_tries. insert ( * address, original_skeleton) ;
486+ storage_tries. insert ( address, original_skeleton) ;
519487 }
520488
521489 Ok ( storage_tries)
522490}
491+
492+ /// Helper function to create a storage trie for a single contract.
493+ async fn create_storage_trie_for_contract < ' a , Layout : NodeLayoutFor < StarknetStorageValue > > (
494+ storage : & impl Storage ,
495+ address : ContractAddress ,
496+ updates : & LeafModifications < StarknetStorageValue > ,
497+ original_contracts_trie_leaves : & HashMap < NodeIndex , ContractState > ,
498+ storage_tries_sorted_indices : & HashMap < ContractAddress , SortedLeafIndices < ' a > > ,
499+ warn_on_trivial_modifications : bool ,
500+ ) -> ForestResult < ( ContractAddress , OriginalSkeletonTreeImpl < ' a > ) >
501+ where
502+ <Layout as NodeLayoutFor < StarknetStorageValue > >:: DbLeaf :
503+ HasStaticPrefix < KeyContext = ContractAddress > ,
504+ {
505+ // Extract data needed for this contract.
506+ let sorted_leaf_indices = * storage_tries_sorted_indices
507+ . get ( & address)
508+ . ok_or ( ForestError :: MissingSortedLeafIndices ( address) ) ?;
509+ let contract_state = original_contracts_trie_leaves
510+ . get ( & contract_address_into_node_index ( & address) )
511+ . ok_or ( ForestError :: MissingContractCurrentState ( address) ) ?;
512+
513+ let trie_config =
514+ OriginalSkeletonTrieConfig :: new_for_classes_or_storage_trie ( warn_on_trivial_modifications) ;
515+
516+ // TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
517+ // iterator over borrowed data so that the conversion below is costless.
518+ let leaf_modifications: HashMap <
519+ NodeIndex ,
520+ <Layout as NodeLayoutFor < StarknetStorageValue > >:: DbLeaf ,
521+ > = updates. iter ( ) . map ( |( idx, value) | ( * idx, Layout :: DbLeaf :: from ( * value) ) ) . collect ( ) ;
522+
523+ let original_skeleton = create_original_skeleton_tree :: < Layout :: DbLeaf , Layout > (
524+ storage,
525+ contract_state. storage_root_hash ,
526+ sorted_leaf_indices,
527+ & trie_config,
528+ & leaf_modifications,
529+ None ,
530+ & address,
531+ )
532+ . await ?;
533+
534+ Ok ( ( address, original_skeleton) )
535+ }
0 commit comments