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

Commit

Permalink
Add Clap wrapper for enum UpgradeCheckSelect
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez committed Jan 3, 2023
1 parent 1f9eb3a commit e29538c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
15 changes: 0 additions & 15 deletions frame/support/src/traits/try_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,6 @@ impl UpgradeCheckSelect {
}
}

#[cfg(feature = "std")]
impl core::str::FromStr for UpgradeCheckSelect {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"none" => Ok(Self::None),
"all" => Ok(Self::All),
"pre-and-post" => Ok(Self::PreAndPost),
"try-state" => Ok(Self::TryState),
_ => Err("Invalid CheckSelector"),
}
}
}

/// Execute some checks to ensure the internal state of a pallet is consistent.
///
/// Usually, these checks should check all of the invariants that are expected to be held on all of
Expand Down
39 changes: 30 additions & 9 deletions utils/frame/try-runtime/cli/src/commands/on_runtime_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// limitations under the License.

use crate::{build_executor, state_machine_call_with_proof, SharedParams, State, LOG_TARGET};
use frame_try_runtime::UpgradeCheckSelect;
use parity_scale_codec::{Decode, Encode};
use sc_executor::sp_wasm_interface::HostFunctions;
use sp_runtime::traits::{Block as BlockT, NumberFor};
Expand All @@ -30,18 +29,39 @@ pub struct OnRuntimeUpgradeCmd {
#[command(subcommand)]
pub state: State,

/// Select which optional checks to perform:
///
/// - `all`: Perform all checks (default).
/// - `pre-and-post`: Perform pre- and post-upgrade checks.
/// - `try-state`: Perform the try-state checks.
/// - `none`: Perform no checks.
/// Select which optional checks to perform.
///
/// Performing any checks will potentially invalidate the measured PoV/Weight.
#[clap(long, default_value = "All", verbatim_doc_comment)]
#[clap(long, value_enum, default_value_t = UpgradeCheckSelect::All)]
pub checks: UpgradeCheckSelect,
}

/// This is an adapter for [`frame_try_runtime::UpgradeCheckSelect`] since that does not implement
/// `clap::ValueEnum`.
#[derive(clap::ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum UpgradeCheckSelect {
/// Perform no checks.
None,
/// Perform all checks.
All,
/// Perform pre- and post-upgrade checks.
PreAndPost,
/// Perform the try-state checks.
TryState,
}

impl From<UpgradeCheckSelect> for frame_try_runtime::UpgradeCheckSelect {
fn from(x: UpgradeCheckSelect) -> Self {
match x {
UpgradeCheckSelect::None => Self::None,
UpgradeCheckSelect::All => Self::All,
UpgradeCheckSelect::PreAndPost => Self::PreAndPost,
UpgradeCheckSelect::TryState => Self::TryState,
}
}
}

pub(crate) async fn on_runtime_upgrade<Block, HostFns>(
shared: SharedParams,
command: OnRuntimeUpgradeCmd,
Expand All @@ -57,12 +77,13 @@ where
{
let executor = build_executor(&shared);
let ext = command.state.into_ext::<Block, HostFns>(&shared, &executor, None).await?;
let checks: frame_try_runtime::UpgradeCheckSelect = command.checks.into();

let (_, encoded_result) = state_machine_call_with_proof::<Block, HostFns>(
&ext,
&executor,
"TryRuntime_on_runtime_upgrade",
command.checks.encode().as_ref(),
checks.encode().as_ref(),
Default::default(), // we don't really need any extensions here.
shared.export_proof,
)?;
Expand Down

0 comments on commit e29538c

Please sign in to comment.