Skip to content

Commit

Permalink
allow remote-ext to exclude some keys (paritytech#10020)
Browse files Browse the repository at this point in the history
* allow remote-ext to exclude some keys

* don't use deprecated, just remove.

* rename
  • Loading branch information
kianenigma authored Oct 13, 2021
1 parent dc88737 commit c5c9087
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
90 changes: 73 additions & 17 deletions utils/frame/remote-externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -557,7 +613,7 @@ mod remote_tests {
}))
.build()
.await
.expect("Can't reach the remote node. Is it running?")
.expect(REMOTE_INACCESSIBLE)
.execute_with(|| {});
}

Expand All @@ -575,7 +631,7 @@ mod remote_tests {
}))
.build()
.await
.expect("Can't reach the remote node. Is it running?")
.expect(REMOTE_INACCESSIBLE)
.execute_with(|| {});
}

Expand All @@ -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 =
Expand All @@ -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)
Expand All @@ -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(|| {});
}
}
2 changes: 1 addition & 1 deletion utils/frame/try-runtime/cli/src/commands/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
6 changes: 4 additions & 2 deletions utils/frame/try-runtime/cli/src/commands/follow_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {:?}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c5c9087

Please sign in to comment.