referrerpolicy=no-referrer-when-downgrade

sc_cli/params/
import_params.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::{
20	arg_enums::{
21		ExecutionStrategy, WasmExecutionMethod, WasmtimeInstantiationStrategy,
22		DEFAULT_WASMTIME_INSTANTIATION_STRATEGY, DEFAULT_WASM_EXECUTION_METHOD,
23	},
24	params::{DatabaseParams, PruningParams},
25};
26use clap::{Args, ValueEnum};
27use std::path::PathBuf;
28
29/// Parameters for block import.
30#[derive(Debug, Clone, Args)]
31pub struct ImportParams {
32	#[allow(missing_docs)]
33	#[clap(flatten)]
34	pub pruning_params: PruningParams,
35
36	#[allow(missing_docs)]
37	#[clap(flatten)]
38	pub database_params: DatabaseParams,
39
40	/// Method for executing Wasm runtime code.
41	#[arg(
42		long = "wasm-execution",
43		value_name = "METHOD",
44		value_enum,
45		ignore_case = true,
46		default_value_t = DEFAULT_WASM_EXECUTION_METHOD,
47	)]
48	pub wasm_method: WasmExecutionMethod,
49
50	/// The WASM instantiation method to use.
51	///
52	/// Only has an effect when `wasm-execution` is set to `compiled`.
53	/// The copy-on-write strategies are only supported on Linux.
54	/// If the copy-on-write variant of a strategy is unsupported
55	/// the executor will fall back to the non-CoW equivalent.
56	/// The fastest (and the default) strategy available is `pooling-copy-on-write`.
57	/// The `legacy-instance-reuse` strategy is deprecated and will
58	/// be removed in the future. It should only be used in case of
59	/// issues with the default instantiation strategy.
60	#[arg(
61		long,
62		value_name = "STRATEGY",
63		default_value_t = DEFAULT_WASMTIME_INSTANTIATION_STRATEGY,
64		value_enum,
65	)]
66	pub wasmtime_instantiation_strategy: WasmtimeInstantiationStrategy,
67
68	/// Specify the path where local WASM runtimes are stored.
69	///
70	/// These runtimes will override on-chain runtimes when the version matches.
71	#[arg(long, value_name = "PATH")]
72	pub wasm_runtime_overrides: Option<PathBuf>,
73
74	#[allow(missing_docs)]
75	#[clap(flatten)]
76	pub execution_strategies: ExecutionStrategiesParams,
77
78	/// Specify the state cache size.
79	///
80	/// Providing `0` will disable the cache.
81	#[arg(long, value_name = "Bytes", default_value_t = 1024 * 1024 * 1024)]
82	pub trie_cache_size: usize,
83
84	/// Warm up the trie cache.
85	///
86	/// No warmup if flag is not present. Using flag without value chooses non-blocking warmup.
87	#[arg(long, value_name = "STRATEGY", value_enum, num_args = 0..=1, default_missing_value = "non-blocking")]
88	pub warm_up_trie_cache: Option<TrieCacheWarmUpStrategy>,
89}
90
91/// Warmup strategy for the trie cache.
92#[derive(Debug, Clone, Copy, ValueEnum)]
93pub enum TrieCacheWarmUpStrategy {
94	/// Warm up the cache in a non-blocking way.
95	#[clap(name = "non-blocking")]
96	NonBlocking,
97	/// Warm up the cache in a blocking way (not recommended for production use).
98	///
99	/// When enabled, the trie cache warm-up will block the node startup until complete.
100	/// This is not recommended for production use as it can significantly delay node startup.
101	/// Only enable this option for testing or debugging purposes.
102	#[clap(name = "blocking")]
103	Blocking,
104}
105
106impl From<TrieCacheWarmUpStrategy> for sc_service::config::TrieCacheWarmUpStrategy {
107	fn from(strategy: TrieCacheWarmUpStrategy) -> Self {
108		match strategy {
109			TrieCacheWarmUpStrategy::NonBlocking =>
110				sc_service::config::TrieCacheWarmUpStrategy::NonBlocking,
111			TrieCacheWarmUpStrategy::Blocking =>
112				sc_service::config::TrieCacheWarmUpStrategy::Blocking,
113		}
114	}
115}
116
117impl ImportParams {
118	/// Specify the trie cache maximum size.
119	pub fn trie_cache_maximum_size(&self) -> Option<usize> {
120		if self.trie_cache_size == 0 {
121			None
122		} else {
123			Some(self.trie_cache_size)
124		}
125	}
126
127	/// Specify if we should warm up the trie cache.
128	pub fn warm_up_trie_cache(&self) -> Option<TrieCacheWarmUpStrategy> {
129		self.warm_up_trie_cache
130	}
131
132	/// Get the WASM execution method from the parameters
133	pub fn wasm_method(&self) -> sc_service::config::WasmExecutionMethod {
134		self.execution_strategies.check_usage_and_print_deprecation_warning();
135
136		crate::execution_method_from_cli(self.wasm_method, self.wasmtime_instantiation_strategy)
137	}
138
139	/// Enable overriding on-chain WASM with locally-stored WASM
140	/// by specifying the path where local WASM is stored.
141	pub fn wasm_runtime_overrides(&self) -> Option<PathBuf> {
142		self.wasm_runtime_overrides.clone()
143	}
144}
145
146/// Execution strategies parameters.
147#[derive(Debug, Clone, Args)]
148pub struct ExecutionStrategiesParams {
149	/// Runtime execution strategy for importing blocks during initial sync.
150	#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
151	pub execution_syncing: Option<ExecutionStrategy>,
152
153	/// Runtime execution strategy for general block import (including locally authored blocks).
154	#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
155	pub execution_import_block: Option<ExecutionStrategy>,
156
157	/// Runtime execution strategy for constructing blocks.
158	#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
159	pub execution_block_construction: Option<ExecutionStrategy>,
160
161	/// Runtime execution strategy for offchain workers.
162	#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
163	pub execution_offchain_worker: Option<ExecutionStrategy>,
164
165	/// Runtime execution strategy when not syncing, importing or constructing blocks.
166	#[arg(long, value_name = "STRATEGY", value_enum, ignore_case = true)]
167	pub execution_other: Option<ExecutionStrategy>,
168
169	/// The execution strategy that should be used by all execution contexts.
170	#[arg(
171		long,
172		value_name = "STRATEGY",
173		value_enum,
174		ignore_case = true,
175		conflicts_with_all = &[
176			"execution_other",
177			"execution_offchain_worker",
178			"execution_block_construction",
179			"execution_import_block",
180			"execution_syncing",
181		]
182	)]
183	pub execution: Option<ExecutionStrategy>,
184}
185
186impl ExecutionStrategiesParams {
187	/// Check if one of the parameters is still passed and print a warning if so.
188	fn check_usage_and_print_deprecation_warning(&self) {
189		for (param, name) in [
190			(&self.execution_syncing, "execution-syncing"),
191			(&self.execution_import_block, "execution-import-block"),
192			(&self.execution_block_construction, "execution-block-construction"),
193			(&self.execution_offchain_worker, "execution-offchain-worker"),
194			(&self.execution_other, "execution-other"),
195			(&self.execution, "execution"),
196		] {
197			if param.is_some() {
198				eprintln!(
199					"CLI parameter `--{name}` has no effect anymore and will be removed in the future!"
200				);
201			}
202		}
203	}
204}