Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit c0ddb87

Browse files
committed
use overlayedchanges
1 parent 094bdc1 commit c0ddb87

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

primitives/state-machine/src/overlayed_changes/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl OverlayedChanges {
276276
/// `None` can be used to delete a value specified by the given key.
277277
///
278278
/// Can be rolled back or committed when called inside a transaction.
279-
pub(crate) fn set_child_storage(
279+
pub fn set_child_storage(
280280
&mut self,
281281
child_info: &ChildInfo,
282282
key: StorageKey,

primitives/state-machine/src/testing.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ where
117117
}
118118
}
119119

120-
/// Returns the overlayed changes.
120+
/// Returns a mutable reference to the overlayed changes.
121+
pub fn overlayed_changes_mut(&mut self) -> &mut OverlayedChanges {
122+
&mut self.overlay
123+
}
124+
125+
/// Returns an immutable reference to the overlayed changes.
121126
pub fn overlayed_changes(&self) -> &OverlayedChanges {
122127
&self.overlay
123128
}

utils/frame/remote-externalities/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2" }
1717
log = "0.4.17"
1818
serde = "1.0.136"
1919
frame-support = { version = "4.0.0-dev", optional = true, path = "../../../frame/support" }
20+
sp-state-machine = { version = "0.13.0", path = "../../../primitives/state-machine" }
2021
sp-core = { version = "7.0.0", path = "../../../primitives/core" }
2122
sp-io = { version = "7.0.0", path = "../../../primitives/io" }
2223
sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" }

utils/frame/remote-externalities/src/lib.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use sp_core::{
3939
};
4040
pub use sp_io::TestExternalities;
4141
use sp_runtime::{traits::Block as BlockT, StateVersion};
42+
use sp_state_machine::OverlayedChanges;
4243
use spinners::{Spinner, Spinners};
4344
use 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

Comments
 (0)