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