@@ -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,40 +466,17 @@ 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- // Prepare data for the async operation.
493- let address = * address;
494- let cloned_storage = storage. clone ( ) ;
495- // TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
496- // iterator over borrowed data so that the conversion below is costless.
497- let leaf_modifications: HashMap <
498- NodeIndex ,
499- <Layout as NodeLayoutFor < StarknetStorageValue > >:: DbLeaf ,
500- > = updates. iter ( ) . map ( |( idx, value) | ( * idx, Layout :: DbLeaf :: from ( * value) ) ) . collect ( ) ;
501-
502469 // Create the future - tokio will poll all futures concurrently.
503470 futures. push ( async move {
504- let original_skeleton = create_original_skeleton_tree :: < Layout :: DbLeaf , Layout > (
505- & cloned_storage,
506- contract_state. storage_root_hash ,
507- sorted_leaf_indices,
508- & trie_config,
509- & leaf_modifications,
510- None ,
511- & address,
471+ create_storage_trie_for_contract :: < Layout > (
472+ storage,
473+ * address,
474+ updates,
475+ original_contracts_trie_leaves,
476+ storage_tries_sorted_indices,
477+ config. warn_on_trivial_modifications ( ) ,
512478 )
513- . await ?;
514- Ok :: < _ , ForestError > ( ( address, original_skeleton) )
479+ . await
515480 } ) ;
516481 }
517482
@@ -523,3 +488,48 @@ where
523488
524489 Ok ( storage_tries)
525490}
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