use super::parse::state_version;
use sc_cli::{
DEFAULT_WASM_EXECUTION_METHOD, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, WasmExecutionMethod,
WasmtimeInstantiationStrategy,
};
use serde::Serialize;
use sp_runtime::StateVersion;
use std::{path::PathBuf, str::FromStr};
const SHARED_PARAMS: [&str; 7] = [
"--runtime",
"--disable-spec-name-check",
"--wasm-execution",
"--wasm-instantiation-strategy",
"--heap-pages",
"--export-proof",
"--overwrite-state-version",
];
#[derive(Debug, Clone, clap::Parser, Serialize)]
#[group(skip)]
pub struct SharedParams {
#[serde(skip_serializing)]
#[arg(long, default_value = "existing")]
pub runtime: Runtime,
#[clap(long, default_value = "false", default_missing_value = "true")]
pub disable_spec_name_check: bool,
#[serde(skip_serializing)]
#[arg(
long = "wasm-execution",
value_name = "METHOD",
value_enum,
ignore_case = true,
default_value_t = DEFAULT_WASM_EXECUTION_METHOD,
)]
pub wasm_method: WasmExecutionMethod,
#[serde(skip_serializing)]
#[arg(
long = "wasm-instantiation-strategy",
value_name = "STRATEGY",
default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
value_enum,
)]
pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy,
#[clap(long)]
pub export_proof: Option<PathBuf>,
#[serde(skip_serializing)]
#[arg(long, value_parser = state_version)]
pub overwrite_state_version: Option<StateVersion>,
}
impl Default for SharedParams {
fn default() -> Self {
SharedParams {
runtime: Runtime::Existing,
disable_spec_name_check: false,
wasm_method: DEFAULT_WASM_EXECUTION_METHOD,
wasmtime_instantiation_strategy: DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
export_proof: None,
overwrite_state_version: None,
}
}
}
impl SharedParams {
pub fn has_argument(arg: &str) -> bool {
SHARED_PARAMS.iter().any(|a| arg.starts_with(a))
}
}
#[derive(Debug, Clone, Serialize)]
pub enum Runtime {
Path(PathBuf),
Existing,
}
impl FromStr for Runtime {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.to_lowercase().as_ref() {
"existing" => Runtime::Existing,
x => Runtime::Path(x.into()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_shared_params_works() {
assert!(SHARED_PARAMS.into_iter().all(SharedParams::has_argument));
}
}