try_runtime_core/common/
shared_parameters.rs

1// This file is part of try-runtime-cli.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use std::{path::PathBuf, str::FromStr};
19
20use sc_cli::{
21    WasmExecutionMethod, WasmtimeInstantiationStrategy, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
22    DEFAULT_WASM_EXECUTION_METHOD,
23};
24use sp_runtime::StateVersion;
25
26use crate::common::parse;
27
28/// Shared parameters of the `try-runtime` commands
29#[derive(Debug, Clone, clap::Parser)]
30#[group(skip)]
31pub struct SharedParams {
32    /// The runtime to use.
33    ///
34    /// Must be a path to a wasm blob, compiled with `try-runtime` feature flag.
35    ///
36    /// Or, `existing`, indicating that you don't want to overwrite the runtime. This will use
37    /// whatever comes from the remote node, or the snapshot file. This will most likely not work
38    /// against a remote node, as no (sane) blockchain should compile its onchain wasm with
39    /// `try-runtime` feature.
40    #[arg(long, default_value = "existing")]
41    pub runtime: Runtime,
42
43    /// Whether to disable enforcing the new runtime `spec_name` matches the existing `spec_name`.
44    #[clap(long, default_value = "false", default_missing_value = "true")]
45    pub disable_spec_name_check: bool,
46
47    /// Type of wasm execution used.
48    #[arg(
49		long = "wasm-execution",
50		value_name = "METHOD",
51		value_enum,
52		ignore_case = true,
53		default_value_t = DEFAULT_WASM_EXECUTION_METHOD,
54	)]
55    pub wasm_method: WasmExecutionMethod,
56
57    /// The WASM instantiation method to use.
58    ///
59    /// Only has an effect when `wasm-execution` is set to `compiled`.
60    #[arg(
61		long = "wasm-instantiation-strategy",
62		value_name = "STRATEGY",
63		default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
64		value_enum,
65	)]
66    pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy,
67
68    /// The number of 64KB pages to allocate for Wasm execution. Defaults to
69    /// [`sc_service::Configuration.default_heap_pages`].
70    #[arg(long)]
71    pub heap_pages: Option<u64>,
72
73    /// Path to a file to export the storage proof into (as a JSON).
74    /// If several blocks are executed, the path is interpreted as a folder
75    /// where one file per block will be written (named `{block_number}-{block_hash}`).
76    #[clap(long)]
77    pub export_proof: Option<PathBuf>,
78
79    /// Overwrite the `state_version`.
80    ///
81    /// Otherwise `remote-externalities` will automatically set the correct state version.
82    #[arg(long, value_parser = parse::state_version)]
83    pub overwrite_state_version: Option<StateVersion>,
84}
85
86#[derive(Debug, Clone)]
87pub enum Runtime {
88    /// Use the given path to the wasm binary file.
89    ///
90    /// It must have been compiled with `try-runtime`.
91    Path(PathBuf),
92
93    /// Use the code of the remote node, or the snapshot.
94    ///
95    /// In almost all cases, this is not what you want, because the code in the remote node does
96    /// not have any of the try-runtime custom runtime APIs.
97    Existing,
98}
99
100impl FromStr for Runtime {
101    type Err = String;
102
103    fn from_str(s: &str) -> Result<Self, Self::Err> {
104        Ok(match s.to_lowercase().as_ref() {
105            "existing" => Runtime::Existing,
106            x => Runtime::Path(x.into()),
107        })
108    }
109}