allow remote-ext to exclude some keys (#10020)

* allow remote-ext to exclude some keys

* don't use deprecated, just remove.

* rename
This commit is contained in:
Kian Paimani
2021-10-13 12:12:26 +01:00
committed by GitHub
parent be3d0ba4ab
commit aae41d777a
5 changed files with 80 additions and 22 deletions
@@ -160,13 +160,16 @@ impl Default for SnapshotConfig {
/// Builder for remote-externalities. /// Builder for remote-externalities.
pub struct Builder<B: BlockT> { pub struct Builder<B: BlockT> {
/// Custom key-pairs to be injected into the externalities. /// Custom key-pairs to be injected into the externalities. The *hashed* keys and values must
inject: Vec<KeyPair>, /// be given.
hashed_key_values: Vec<KeyPair>,
/// Storage entry key prefixes to be injected into the externalities. The *hashed* prefix must /// Storage entry key prefixes to be injected into the externalities. The *hashed* prefix must
/// be given. /// be given.
hashed_prefixes: Vec<Vec<u8>>, hashed_prefixes: Vec<Vec<u8>>,
/// Storage entry keys to be injected into the externalities. The *hashed* key must be given. /// Storage entry keys to be injected into the externalities. The *hashed* key must be given.
hashed_keys: Vec<Vec<u8>>, 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. /// connectivity mode, online or offline.
mode: Mode<B>, mode: Mode<B>,
} }
@@ -176,10 +179,11 @@ pub struct Builder<B: BlockT> {
impl<B: BlockT> Default for Builder<B> { impl<B: BlockT> Default for Builder<B> {
fn default() -> Self { fn default() -> Self {
Self { Self {
inject: Default::default(),
mode: Default::default(), mode: Default::default(),
hashed_key_values: Default::default(),
hashed_prefixes: Default::default(), hashed_prefixes: Default::default(),
hashed_keys: Default::default(), hashed_keys: Default::default(),
hashed_blacklist: Default::default(),
} }
} }
} }
@@ -435,12 +439,26 @@ impl<B: BlockT> Builder<B> {
}, },
}; };
debug!( // inject manual key values.
target: LOG_TARGET, if !self.hashed_key_values.is_empty() {
"extending externalities with {} manually injected key-values", debug!(
self.inject.len() target: LOG_TARGET,
); "extending externalities with {} manually injected key-values",
base_kv.extend(self.inject.clone()); 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) Ok(base_kv)
} }
} }
@@ -453,13 +471,12 @@ impl<B: BlockT> Builder<B> {
} }
/// Inject a manual list of key and values to the storage. /// 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 { for i in injections {
self.inject.push(i.clone()); self.hashed_key_values.push(i.clone());
} }
self self
} }
/// Inject a hashed prefix. This is treated as-is, and should be pre-hashed. /// 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. /// This should be used to inject a "PREFIX", like a storage (double) map.
@@ -476,6 +493,13 @@ impl<B: BlockT> Builder<B> {
self 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. /// Configure a state snapshot to be used.
pub fn mode(mut self, mode: Mode<B>) -> Self { pub fn mode(mut self, mode: Mode<B>) -> Self {
self.mode = mode; self.mode = mode;
@@ -541,12 +565,44 @@ mod tests {
.expect("Can't read state snapshot file") .expect("Can't read state snapshot file")
.execute_with(|| {}); .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"))] #[cfg(all(test, feature = "remote-test"))]
mod remote_tests { mod remote_tests {
use super::test_prelude::*; use super::test_prelude::*;
const REMOTE_INACCESSIBLE: &'static str = "Can't reach the remote node. Is it running?";
#[tokio::test] #[tokio::test]
async fn can_build_one_pallet() { async fn can_build_one_pallet() {
init_logger(); init_logger();
@@ -557,7 +613,7 @@ mod remote_tests {
})) }))
.build() .build()
.await .await
.expect("Can't reach the remote node. Is it running?") .expect(REMOTE_INACCESSIBLE)
.execute_with(|| {}); .execute_with(|| {});
} }
@@ -575,7 +631,7 @@ mod remote_tests {
})) }))
.build() .build()
.await .await
.expect("Can't reach the remote node. Is it running?") .expect(REMOTE_INACCESSIBLE)
.execute_with(|| {}); .execute_with(|| {});
} }
@@ -599,7 +655,7 @@ mod remote_tests {
})) }))
.build() .build()
.await .await
.expect("Can't reach the remote node. Is it running?") .expect(REMOTE_INACCESSIBLE)
.execute_with(|| { .execute_with(|| {
// Gav's polkadot account. 99% this will be in the council. // Gav's polkadot account. 99% this will be in the council.
let gav_polkadot = let gav_polkadot =
@@ -625,7 +681,7 @@ mod remote_tests {
})) }))
.build() .build()
.await .await
.expect("Can't reach the remote node. Is it running?") .expect(REMOTE_INACCESSIBLE)
.execute_with(|| {}); .execute_with(|| {});
let to_delete = std::fs::read_dir(SnapshotConfig::default().path) let to_delete = std::fs::read_dir(SnapshotConfig::default().path)
@@ -648,7 +704,7 @@ mod remote_tests {
Builder::<Block>::new() Builder::<Block>::new()
.build() .build()
.await .await
.expect("Can't reach the remote node. Is it running?") .expect(REMOTE_INACCESSIBLE)
.execute_with(|| {}); .execute_with(|| {});
} }
} }
@@ -143,7 +143,7 @@ where
let builder = if command.overwrite_wasm_code { let builder = if command.overwrite_wasm_code {
let (code_key, code) = extract_code(&config.chain_spec)?; 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 { } else {
builder.inject_hashed_key(well_known_keys::CODE) builder.inject_hashed_key(well_known_keys::CODE)
}; };
@@ -112,8 +112,10 @@ where
..Default::default() ..Default::default()
})); }));
let new_ext = let new_ext = builder
builder.inject_key_value(&[(code_key.clone(), code.clone())]).build().await?; .inject_hashed_key_value(&[(code_key.clone(), code.clone())])
.build()
.await?;
log::info!( log::info!(
target: LOG_TARGET, target: LOG_TARGET,
"initialized state externalities at {:?}, storage root {:?}", "initialized state externalities at {:?}, storage root {:?}",
@@ -132,7 +132,7 @@ where
let builder = if command.overwrite_wasm_code { let builder = if command.overwrite_wasm_code {
let (code_key, code) = extract_code(&config.chain_spec)?; 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 { } else {
builder.inject_hashed_key(well_known_keys::CODE) builder.inject_hashed_key(well_known_keys::CODE)
}; };
@@ -54,7 +54,7 @@ where
let ext = { let ext = {
let builder = command.state.builder::<Block>()?; let builder = command.state.builder::<Block>()?;
let (code_key, code) = extract_code(&config.chain_spec)?; 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() { if let Some(uri) = command.state.live_uri() {