From aae41d777a3bbfa2d3236e5135da7b746030c200 Mon Sep 17 00:00:00 2001 From: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Date: Wed, 13 Oct 2021 12:12:26 +0100 Subject: [PATCH] allow remote-ext to exclude some keys (#10020) * allow remote-ext to exclude some keys * don't use deprecated, just remove. * rename --- .../frame/remote-externalities/src/lib.rs | 90 +++++++++++++++---- .../cli/src/commands/execute_block.rs | 2 +- .../cli/src/commands/follow_chain.rs | 6 +- .../cli/src/commands/offchain_worker.rs | 2 +- .../cli/src/commands/on_runtime_upgrade.rs | 2 +- 5 files changed, 80 insertions(+), 22 deletions(-) diff --git a/substrate/utils/frame/remote-externalities/src/lib.rs b/substrate/utils/frame/remote-externalities/src/lib.rs index 2052780286c..733ec7c3200 100644 --- a/substrate/utils/frame/remote-externalities/src/lib.rs +++ b/substrate/utils/frame/remote-externalities/src/lib.rs @@ -160,13 +160,16 @@ impl Default for SnapshotConfig { /// Builder for remote-externalities. pub struct Builder<B: BlockT> { - /// Custom key-pairs to be injected into the externalities. - inject: Vec<KeyPair>, + /// Custom key-pairs to be injected into the externalities. The *hashed* keys and values must + /// be given. + hashed_key_values: Vec<KeyPair>, /// Storage entry key prefixes to be injected into the externalities. The *hashed* prefix must /// be given. hashed_prefixes: Vec<Vec<u8>>, /// Storage entry keys to be injected into the externalities. The *hashed* key must be given. hashed_keys: Vec<Vec<u8>>, + /// The keys that will be excluded from the final externality. The *hashed* key must be given. + hashed_blacklist: Vec<Vec<u8>>, /// connectivity mode, online or offline. mode: Mode<B>, } @@ -176,10 +179,11 @@ pub struct Builder<B: BlockT> { impl<B: BlockT> Default for Builder<B> { fn default() -> Self { Self { - inject: Default::default(), mode: Default::default(), + hashed_key_values: Default::default(), hashed_prefixes: Default::default(), hashed_keys: Default::default(), + hashed_blacklist: Default::default(), } } } @@ -435,12 +439,26 @@ impl<B: BlockT> Builder<B> { }, }; - debug!( - target: LOG_TARGET, - "extending externalities with {} manually injected key-values", - self.inject.len() - ); - base_kv.extend(self.inject.clone()); + // inject manual key values. + if !self.hashed_key_values.is_empty() { + debug!( + target: LOG_TARGET, + "extending externalities with {} manually injected key-values", + self.hashed_key_values.len() + ); + base_kv.extend(self.hashed_key_values.clone()); + } + + // exclude manual key values. + if !self.hashed_blacklist.is_empty() { + debug!( + target: LOG_TARGET, + "excluding externalities from {} keys", + self.hashed_blacklist.len() + ); + base_kv.retain(|(k, _)| !self.hashed_blacklist.contains(&k.0)) + } + Ok(base_kv) } } @@ -453,13 +471,12 @@ impl<B: BlockT> Builder<B> { } /// Inject a manual list of key and values to the storage. - pub fn inject_key_value(mut self, injections: &[KeyPair]) -> Self { + pub fn inject_hashed_key_value(mut self, injections: &[KeyPair]) -> Self { for i in injections { - self.inject.push(i.clone()); + self.hashed_key_values.push(i.clone()); } self } - /// Inject a hashed prefix. This is treated as-is, and should be pre-hashed. /// /// This should be used to inject a "PREFIX", like a storage (double) map. @@ -476,6 +493,13 @@ impl<B: BlockT> Builder<B> { self } + /// Blacklist this hashed key from the final externalities. This is treated as-is, and should be + /// pre-hashed. + pub fn blacklist_hashed_key(mut self, hashed: &[u8]) -> Self { + self.hashed_blacklist.push(hashed.to_vec()); + self + } + /// Configure a state snapshot to be used. pub fn mode(mut self, mode: Mode<B>) -> Self { self.mode = mode; @@ -541,12 +565,44 @@ mod tests { .expect("Can't read state snapshot file") .execute_with(|| {}); } + + #[tokio::test] + async fn can_exclude_from_cache() { + init_logger(); + + // get the first key from the cache file. + let some_key = Builder::<Block>::new() + .mode(Mode::Offline(OfflineConfig { + state_snapshot: SnapshotConfig::new("test_data/proxy_test"), + })) + .build() + .await + .expect("Can't read state snapshot file") + .execute_with(|| { + let key = + sp_io::storage::next_key(&[]).expect("some key must exist in the snapshot"); + assert!(sp_io::storage::get(&key).is_some()); + key + }); + + Builder::<Block>::new() + .mode(Mode::Offline(OfflineConfig { + state_snapshot: SnapshotConfig::new("test_data/proxy_test"), + })) + .blacklist_hashed_key(&some_key) + .build() + .await + .expect("Can't read state snapshot file") + .execute_with(|| assert!(sp_io::storage::get(&some_key).is_none())); + } } #[cfg(all(test, feature = "remote-test"))] mod remote_tests { use super::test_prelude::*; + const REMOTE_INACCESSIBLE: &'static str = "Can't reach the remote node. Is it running?"; + #[tokio::test] async fn can_build_one_pallet() { init_logger(); @@ -557,7 +613,7 @@ mod remote_tests { })) .build() .await - .expect("Can't reach the remote node. Is it running?") + .expect(REMOTE_INACCESSIBLE) .execute_with(|| {}); } @@ -575,7 +631,7 @@ mod remote_tests { })) .build() .await - .expect("Can't reach the remote node. Is it running?") + .expect(REMOTE_INACCESSIBLE) .execute_with(|| {}); } @@ -599,7 +655,7 @@ mod remote_tests { })) .build() .await - .expect("Can't reach the remote node. Is it running?") + .expect(REMOTE_INACCESSIBLE) .execute_with(|| { // Gav's polkadot account. 99% this will be in the council. let gav_polkadot = @@ -625,7 +681,7 @@ mod remote_tests { })) .build() .await - .expect("Can't reach the remote node. Is it running?") + .expect(REMOTE_INACCESSIBLE) .execute_with(|| {}); let to_delete = std::fs::read_dir(SnapshotConfig::default().path) @@ -648,7 +704,7 @@ mod remote_tests { Builder::<Block>::new() .build() .await - .expect("Can't reach the remote node. Is it running?") + .expect(REMOTE_INACCESSIBLE) .execute_with(|| {}); } } diff --git a/substrate/utils/frame/try-runtime/cli/src/commands/execute_block.rs b/substrate/utils/frame/try-runtime/cli/src/commands/execute_block.rs index 19422db9011..216c63d0052 100644 --- a/substrate/utils/frame/try-runtime/cli/src/commands/execute_block.rs +++ b/substrate/utils/frame/try-runtime/cli/src/commands/execute_block.rs @@ -143,7 +143,7 @@ where let builder = if command.overwrite_wasm_code { let (code_key, code) = extract_code(&config.chain_spec)?; - builder.inject_key_value(&[(code_key, code)]) + builder.inject_hashed_key_value(&[(code_key, code)]) } else { builder.inject_hashed_key(well_known_keys::CODE) }; diff --git a/substrate/utils/frame/try-runtime/cli/src/commands/follow_chain.rs b/substrate/utils/frame/try-runtime/cli/src/commands/follow_chain.rs index 0526f5d327f..9125db13c78 100644 --- a/substrate/utils/frame/try-runtime/cli/src/commands/follow_chain.rs +++ b/substrate/utils/frame/try-runtime/cli/src/commands/follow_chain.rs @@ -112,8 +112,10 @@ where ..Default::default() })); - let new_ext = - builder.inject_key_value(&[(code_key.clone(), code.clone())]).build().await?; + let new_ext = builder + .inject_hashed_key_value(&[(code_key.clone(), code.clone())]) + .build() + .await?; log::info!( target: LOG_TARGET, "initialized state externalities at {:?}, storage root {:?}", diff --git a/substrate/utils/frame/try-runtime/cli/src/commands/offchain_worker.rs b/substrate/utils/frame/try-runtime/cli/src/commands/offchain_worker.rs index 6f37e4b3849..22120ef4b5f 100644 --- a/substrate/utils/frame/try-runtime/cli/src/commands/offchain_worker.rs +++ b/substrate/utils/frame/try-runtime/cli/src/commands/offchain_worker.rs @@ -132,7 +132,7 @@ where let builder = if command.overwrite_wasm_code { let (code_key, code) = extract_code(&config.chain_spec)?; - builder.inject_key_value(&[(code_key, code)]) + builder.inject_hashed_key_value(&[(code_key, code)]) } else { builder.inject_hashed_key(well_known_keys::CODE) }; diff --git a/substrate/utils/frame/try-runtime/cli/src/commands/on_runtime_upgrade.rs b/substrate/utils/frame/try-runtime/cli/src/commands/on_runtime_upgrade.rs index 86f5548b8aa..8de3cb3a320 100644 --- a/substrate/utils/frame/try-runtime/cli/src/commands/on_runtime_upgrade.rs +++ b/substrate/utils/frame/try-runtime/cli/src/commands/on_runtime_upgrade.rs @@ -54,7 +54,7 @@ where let ext = { let builder = command.state.builder::<Block>()?; let (code_key, code) = extract_code(&config.chain_spec)?; - builder.inject_key_value(&[(code_key, code)]).build().await? + builder.inject_hashed_key_value(&[(code_key, code)]).build().await? }; if let Some(uri) = command.state.live_uri() { -- GitLab