Skip to main content

pop_chains/try_runtime/
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 super::parse::state_version;
19use sc_cli::{
20	DEFAULT_WASM_EXECUTION_METHOD, DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, WasmExecutionMethod,
21	WasmtimeInstantiationStrategy,
22};
23use serde::Serialize;
24use sp_runtime::StateVersion;
25use std::{path::PathBuf, str::FromStr};
26
27/// Shared parameters derived from the fields of struct [SharedParams].
28const SHARED_PARAMS: [&str; 7] = [
29	"--runtime",
30	"--disable-spec-name-check",
31	"--wasm-execution",
32	"--wasm-instantiation-strategy",
33	"--heap-pages",
34	"--export-proof",
35	"--overwrite-state-version",
36];
37
38/// Shared parameters of the `try-runtime` commands
39#[derive(Debug, Clone, clap::Parser, Serialize)]
40#[group(skip)]
41pub struct SharedParams {
42	/// The runtime to use.
43	///
44	/// Must be a path to a wasm blob, compiled with `try-runtime` feature flag.
45	///
46	/// Or, `existing`, indicating that you don't want to overwrite the runtime. This will use
47	/// whatever comes from the remote node, or the snapshot file. This will most likely not work
48	/// against a remote node, as no (sane) blockchain should compile its onchain wasm with
49	/// `try-runtime` feature.
50	#[serde(skip_serializing)]
51	#[arg(long, default_value = "existing")]
52	pub runtime: Runtime,
53
54	/// Whether to disable enforcing the new runtime `spec_name` matches the existing `spec_name`.
55	#[clap(long, default_value = "false", default_missing_value = "true")]
56	pub disable_spec_name_check: bool,
57
58	/// Type of wasm execution used.
59	#[serde(skip_serializing)]
60	#[arg(
61		long = "wasm-execution",
62		value_name = "METHOD",
63		value_enum,
64		ignore_case = true,
65		default_value_t = DEFAULT_WASM_EXECUTION_METHOD,
66	)]
67	pub wasm_method: WasmExecutionMethod,
68
69	/// The WASM instantiation method to use.
70	///
71	/// Only has an effect when `wasm-execution` is set to `compiled`.
72	#[serde(skip_serializing)]
73	#[arg(
74		long = "wasm-instantiation-strategy",
75		value_name = "STRATEGY",
76		default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
77		value_enum,
78	)]
79	pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy,
80
81	/// Path to a file to export the storage proof into (as a JSON).
82	/// If several blocks are executed, the path is interpreted as a folder
83	/// where one file per block will be written (named `{block_number}-{block_hash}`).
84	#[clap(long)]
85	pub export_proof: Option<PathBuf>,
86
87	/// Overwrite the `state_version`.
88	///
89	/// Otherwise `remote-externalities` will automatically set the correct state version.
90	#[serde(skip_serializing)]
91	#[arg(long, value_parser = state_version)]
92	pub overwrite_state_version: Option<StateVersion>,
93}
94
95impl Default for SharedParams {
96	fn default() -> Self {
97		SharedParams {
98			runtime: Runtime::Existing,
99			disable_spec_name_check: false,
100			wasm_method: DEFAULT_WASM_EXECUTION_METHOD,
101			wasmtime_instantiation_strategy: DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
102			export_proof: None,
103			overwrite_state_version: None,
104		}
105	}
106}
107
108impl SharedParams {
109	/// Check if the given argument is a shared parameter.
110	pub fn has_argument(arg: &str) -> bool {
111		SHARED_PARAMS.iter().any(|a| arg.starts_with(a))
112	}
113}
114
115/// Source of the runtime.
116#[derive(Debug, Clone, Serialize)]
117pub enum Runtime {
118	/// Use the given path to the wasm binary file.
119	///
120	/// It must have been compiled with `try-runtime`.
121	Path(PathBuf),
122
123	/// Use the code of the remote node, or the snapshot.
124	///
125	/// In almost all cases, this is not what you want, because the code in the remote node does
126	/// not have any of the try-runtime custom runtime APIs.
127	Existing,
128}
129
130impl FromStr for Runtime {
131	type Err = String;
132
133	fn from_str(s: &str) -> Result<Self, Self::Err> {
134		Ok(match s.to_lowercase().as_ref() {
135			"existing" => Runtime::Existing,
136			x => Runtime::Path(x.into()),
137		})
138	}
139}
140
141#[cfg(test)]
142mod tests {
143	use super::*;
144
145	#[test]
146	fn is_shared_params_works() {
147		assert!(SHARED_PARAMS.into_iter().all(SharedParams::has_argument));
148	}
149}