@@ -39,6 +39,7 @@ use sp_core::{
3939} ;
4040pub use sp_io:: TestExternalities ;
4141use sp_runtime:: { traits:: Block as BlockT , StateVersion } ;
42+ use sp_state_machine:: OverlayedChanges ;
4243use spinners:: { Spinner , Spinners } ;
4344use std:: {
4445 cmp:: max,
@@ -520,7 +521,7 @@ where
520521 & self ,
521522 prefix : StorageKey ,
522523 at : B :: Hash ,
523- pending_ext : & mut TestExternalities ,
524+ ext_overlay : & mut OverlayedChanges ,
524525 ) -> Result < Vec < KeyValue > , & ' static str > {
525526 let start = Instant :: now ( ) ;
526527 let mut sp = Spinner :: with_timer ( Spinners :: Dots , "Scraping keys..." . into ( ) ) ;
@@ -546,7 +547,7 @@ where
546547 . collect :: < Vec < _ > > ( ) ;
547548
548549 let bar = ProgressBar :: new ( payloads. len ( ) as u64 ) ;
549- let bar_message = format ! ( "Downloading key values" ) ;
550+ let bar_message = "Downloading key values" . to_string ( ) ;
550551 bar. set_message ( bar_message) ;
551552 bar. set_style (
552553 ProgressStyle :: with_template (
@@ -596,7 +597,9 @@ where
596597
597598 let mut sp = Spinner :: with_timer ( Spinners :: Dots , "Inserting keys into DB..." . into ( ) ) ;
598599 let start = Instant :: now ( ) ;
599- pending_ext. batch_insert ( key_values. clone ( ) . into_iter ( ) . map ( |( k, v) | ( k. 0 , v. 0 ) ) ) ;
600+ key_values. clone ( ) . into_iter ( ) . for_each ( |( k, v) | {
601+ ext_overlay. set_storage ( k. 0 , Some ( v. 0 ) ) ;
602+ } ) ;
600603 sp. stop_with_message ( format ! ( "✅ Inserted keys into DB ({}s)" , start. elapsed( ) . as_secs( ) ) ) ;
601604 Ok ( key_values)
602605 }
@@ -703,7 +706,7 @@ where
703706 async fn load_child_remote (
704707 & self ,
705708 top_kv : & [ KeyValue ] ,
706- pending_ext : & mut TestExternalities ,
709+ ext_overlay : & mut OverlayedChanges ,
707710 ) -> Result < ChildKeyValues , & ' static str > {
708711 let child_roots = top_kv
709712 . into_iter ( )
@@ -747,7 +750,7 @@ where
747750 child_kv_inner. iter ( ) . cloned ( ) . map ( |( k, v) | ( k. 0 , v. 0 ) ) . collect :: < Vec < _ > > ( ) ;
748751 child_kv. push ( ( info. clone ( ) , child_kv_inner) ) ;
749752 for ( k, v) in key_values {
750- pending_ext . insert_child ( info. clone ( ) , k, v ) ;
753+ ext_overlay . set_child_storage ( & info, k, Some ( v ) ) ;
751754 }
752755 }
753756
@@ -756,11 +759,11 @@ where
756759
757760 /// Build `Self` from a network node denoted by `uri`.
758761 ///
759- /// This function concurrently populates `pending_ext `. the return value is only for writing to
760- /// cache, we can also optimize further .
762+ /// This function populates `ext_overlay `. The return value is for optionally writing to a
763+ /// snapshot file .
761764 async fn load_top_remote (
762765 & self ,
763- pending_ext : & mut TestExternalities ,
766+ ext_overlay : & mut OverlayedChanges ,
764767 ) -> Result < TopKeyValues , & ' static str > {
765768 let config = self . as_online ( ) ;
766769 let at = self
@@ -773,7 +776,7 @@ where
773776 for prefix in & config. hashed_prefixes {
774777 let now = std:: time:: Instant :: now ( ) ;
775778 let additional_key_values =
776- self . rpc_get_pairs_paged ( StorageKey ( prefix. to_vec ( ) ) , at, pending_ext ) . await ?;
779+ self . rpc_get_pairs_paged ( StorageKey ( prefix. to_vec ( ) ) , at, ext_overlay ) . await ?;
777780 let elapsed = now. elapsed ( ) ;
778781 log:: info!(
779782 target: LOG_TARGET ,
@@ -793,7 +796,7 @@ where
793796 ) ;
794797 match self . rpc_get_storage ( key. clone ( ) , Some ( at) ) . await ? {
795798 Some ( value) => {
796- pending_ext . insert ( key. clone ( ) . 0 , value. clone ( ) . 0 ) ;
799+ ext_overlay . set_storage ( key. clone ( ) . 0 , Some ( value. clone ( ) . 0 ) ) ;
797800 keys_and_values. push ( ( key, value) ) ;
798801 } ,
799802 None => {
@@ -874,8 +877,10 @@ where
874877 Default :: default ( ) ,
875878 self . overwrite_state_version . unwrap_or ( state_version) ,
876879 ) ;
877- let top_kv = self . load_top_remote ( & mut pending_ext) . await ?;
878- let child_kv = self . load_child_remote ( & top_kv, & mut pending_ext) . await ?;
880+ let top_kv = self . load_top_remote ( & mut pending_ext. overlayed_changes_mut ( ) ) . await ?;
881+ let child_kv = self
882+ . load_child_remote ( & top_kv, & mut pending_ext. overlayed_changes_mut ( ) )
883+ . await ?;
879884
880885 if let Some ( path) = self . as_online ( ) . state_snapshot . clone ( ) . map ( |c| c. path ) {
881886 let snapshot = Snapshot :: < B > {
@@ -925,24 +930,23 @@ where
925930 Default :: default ( ) ,
926931 self . overwrite_state_version . unwrap_or ( state_version) ,
927932 ) ;
933+ let ext_overlay = inner_ext. overlayed_changes_mut ( ) ;
928934
929- info ! ( target: LOG_TARGET , "injecting a total of {} top keys" , top. len( ) ) ;
930- let top = top
931- . into_iter ( )
935+ info ! ( target: LOG_TARGET , "injecting {} top keys" , top. len( ) ) ;
936+ top. into_iter ( )
932937 . filter ( |( k, _) | !is_default_child_storage_key ( k. as_ref ( ) ) )
933- . map ( |( k, v) | ( k . 0 , v . 0 ) )
934- . collect :: < Vec < _ > > ( ) ;
935- inner_ext . batch_insert ( top ) ;
938+ . for_each ( |( k, v) | {
939+ ext_overlay . set_storage ( k . 0 , Some ( v . 0 ) ) ;
940+ } ) ;
936941
937942 info ! (
938943 target: LOG_TARGET ,
939- "injecting a total of {} child keys" ,
944+ "injecting {} child keys" ,
940945 child. iter( ) . flat_map( |( _, kv) | kv) . count( )
941946 ) ;
942-
943947 for ( info, key_values) in child {
944948 for ( k, v) in key_values {
945- inner_ext . insert_child ( info. clone ( ) , k. 0 , v. 0 ) ;
949+ ext_overlay . set_child_storage ( & info. clone ( ) , k. 0 , Some ( v. 0 ) ) ;
946950 }
947951 }
948952
@@ -960,6 +964,7 @@ where
960964 }
961965 } ,
962966 } ;
967+ let ext_overlay = ext. inner_ext . overlayed_changes_mut ( ) ;
963968
964969 // inject manual key values.
965970 if !self . hashed_key_values . is_empty ( ) {
@@ -968,7 +973,10 @@ where
968973 "extending externalities with {} manually injected key-values" ,
969974 self . hashed_key_values. len( )
970975 ) ;
971- ext. batch_insert ( self . hashed_key_values . into_iter ( ) . map ( |( k, v) | ( k. 0 , v. 0 ) ) ) ;
976+
977+ self . hashed_key_values . into_iter ( ) . for_each ( |( k, v) | {
978+ ext_overlay. set_storage ( k. 0 , Some ( v. 0 ) ) ;
979+ } ) ;
972980 }
973981
974982 // exclude manual key values.
0 commit comments