try-runtime - support all state versions (#12089)

* Add argument

* Apply

* More verbose parsing

* fmt

* fmt again

* Invalid state version

* reuse parsing

* type mismatch
This commit is contained in:
Piotr Mikołajczyk
2022-08-27 06:56:24 +02:00
committed by GitHub
parent 75a76d967b
commit 9e5b211828
6 changed files with 25 additions and 8 deletions
@@ -139,7 +139,8 @@ where
.state .state
.builder::<Block>()? .builder::<Block>()?
// make sure the state is being build with the parent hash, if it is online. // make sure the state is being build with the parent hash, if it is online.
.overwrite_online_at(parent_hash.to_owned()); .overwrite_online_at(parent_hash.to_owned())
.state_version(shared.state_version);
let builder = if command.overwrite_wasm_code { let builder = if command.overwrite_wasm_code {
log::info!( log::info!(
@@ -102,11 +102,13 @@ where
// create an ext at the state of this block, whatever is the first subscription event. // create an ext at the state of this block, whatever is the first subscription event.
if maybe_state_ext.is_none() { if maybe_state_ext.is_none() {
let builder = Builder::<Block>::new().mode(Mode::Online(OnlineConfig { let builder = Builder::<Block>::new()
transport: command.uri.clone().into(), .mode(Mode::Online(OnlineConfig {
at: Some(*header.parent_hash()), transport: command.uri.clone().into(),
..Default::default() at: Some(*header.parent_hash()),
})); ..Default::default()
}))
.state_version(shared.state_version);
let new_ext = builder let new_ext = builder
.inject_hashed_key_value(&[(code_key.clone(), code.clone())]) .inject_hashed_key_value(&[(code_key.clone(), code.clone())])
@@ -128,7 +128,7 @@ where
); );
let ext = { let ext = {
let builder = command.state.builder::<Block>()?; let builder = command.state.builder::<Block>()?.state_version(shared.state_version);
let builder = if command.overwrite_wasm_code { let builder = if command.overwrite_wasm_code {
log::info!( log::info!(
@@ -52,7 +52,7 @@ where
let execution = shared.execution; let execution = shared.execution;
let ext = { let ext = {
let builder = command.state.builder::<Block>()?; let builder = command.state.builder::<Block>()?.state_version(shared.state_version);
let (code_key, code) = extract_code(&config.chain_spec)?; let (code_key, code) = extract_code(&config.chain_spec)?;
builder.inject_hashed_key_value(&[(code_key, code)]).build().await? builder.inject_hashed_key_value(&[(code_key, code)]).build().await?
}; };
@@ -294,6 +294,7 @@ use sp_runtime::{
DeserializeOwned, DeserializeOwned,
}; };
use sp_state_machine::{OverlayedChanges, StateMachine, TrieBackendBuilder}; use sp_state_machine::{OverlayedChanges, StateMachine, TrieBackendBuilder};
use sp_version::StateVersion;
use std::{fmt::Debug, path::PathBuf, str::FromStr}; use std::{fmt::Debug, path::PathBuf, str::FromStr};
mod commands; mod commands;
@@ -421,6 +422,10 @@ pub struct SharedParams {
/// When enabled, the spec name check will not panic, and instead only show a warning. /// When enabled, the spec name check will not panic, and instead only show a warning.
#[clap(long)] #[clap(long)]
pub no_spec_name_check: bool, pub no_spec_name_check: bool,
/// State version that is used by the chain.
#[clap(long, default_value = "1", parse(try_from_str = parse::state_version))]
pub state_version: StateVersion,
} }
/// Our `try-runtime` command. /// Our `try-runtime` command.
@@ -17,6 +17,8 @@
//! Utils for parsing user input //! Utils for parsing user input
use sp_version::StateVersion;
pub(crate) fn hash(block_hash: &str) -> Result<String, String> { pub(crate) fn hash(block_hash: &str) -> Result<String, String> {
let (block_hash, offset) = if let Some(block_hash) = block_hash.strip_prefix("0x") { let (block_hash, offset) = if let Some(block_hash) = block_hash.strip_prefix("0x") {
(block_hash, 2) (block_hash, 2)
@@ -42,3 +44,10 @@ pub(crate) fn url(s: &str) -> Result<String, &'static str> {
Err("not a valid WS(S) url: must start with 'ws://' or 'wss://'") Err("not a valid WS(S) url: must start with 'ws://' or 'wss://'")
} }
} }
pub(crate) fn state_version(s: &str) -> Result<StateVersion, &'static str> {
s.parse::<u8>()
.map_err(|_| ())
.and_then(StateVersion::try_from)
.map_err(|_| "Invalid state version.")
}