referrerpolicy=no-referrer-when-downgrade

sc_cli/
arg_enums.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
19//! Definitions of [`ValueEnum`] types.
20
21use clap::ValueEnum;
22use std::str::FromStr;
23
24/// The instantiation strategy to use in compiled mode.
25#[derive(Debug, Clone, Copy, ValueEnum)]
26#[value(rename_all = "kebab-case")]
27pub enum WasmtimeInstantiationStrategy {
28	/// Pool the instances to avoid initializing everything from scratch
29	/// on each instantiation. Use copy-on-write memory when possible.
30	PoolingCopyOnWrite,
31
32	/// Recreate the instance from scratch on every instantiation.
33	/// Use copy-on-write memory when possible.
34	RecreateInstanceCopyOnWrite,
35
36	/// Pool the instances to avoid initializing everything from scratch
37	/// on each instantiation.
38	Pooling,
39
40	/// Recreate the instance from scratch on every instantiation. Very slow.
41	RecreateInstance,
42}
43
44/// The default [`WasmtimeInstantiationStrategy`].
45pub const DEFAULT_WASMTIME_INSTANTIATION_STRATEGY: WasmtimeInstantiationStrategy =
46	WasmtimeInstantiationStrategy::PoolingCopyOnWrite;
47
48/// How to execute Wasm runtime code.
49#[derive(Debug, Clone, Copy, ValueEnum)]
50#[value(rename_all = "kebab-case")]
51pub enum WasmExecutionMethod {
52	/// Uses an interpreter which now is deprecated.
53	#[clap(name = "interpreted-i-know-what-i-do")]
54	Interpreted,
55	/// Uses a compiled runtime.
56	Compiled,
57}
58
59impl std::fmt::Display for WasmExecutionMethod {
60	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61		match self {
62			Self::Interpreted => write!(f, "Interpreted"),
63			Self::Compiled => write!(f, "Compiled"),
64		}
65	}
66}
67
68/// Converts the execution method and instantiation strategy command line arguments
69/// into an execution method which can be used internally.
70pub fn execution_method_from_cli(
71	execution_method: WasmExecutionMethod,
72	instantiation_strategy: WasmtimeInstantiationStrategy,
73) -> sc_service::config::WasmExecutionMethod {
74	if let WasmExecutionMethod::Interpreted = execution_method {
75		log::warn!(
76			"`interpreted-i-know-what-i-do` is deprecated and will be removed in the future. Defaults to `compiled` execution mode."
77		);
78	}
79
80	sc_service::config::WasmExecutionMethod::Compiled {
81		instantiation_strategy: match instantiation_strategy {
82			WasmtimeInstantiationStrategy::PoolingCopyOnWrite =>
83				sc_service::config::WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
84			WasmtimeInstantiationStrategy::RecreateInstanceCopyOnWrite =>
85				sc_service::config::WasmtimeInstantiationStrategy::RecreateInstanceCopyOnWrite,
86			WasmtimeInstantiationStrategy::Pooling =>
87				sc_service::config::WasmtimeInstantiationStrategy::Pooling,
88			WasmtimeInstantiationStrategy::RecreateInstance =>
89				sc_service::config::WasmtimeInstantiationStrategy::RecreateInstance,
90		},
91	}
92}
93
94/// The default [`WasmExecutionMethod`].
95pub const DEFAULT_WASM_EXECUTION_METHOD: WasmExecutionMethod = WasmExecutionMethod::Compiled;
96
97#[allow(missing_docs)]
98#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
99#[value(rename_all = "kebab-case")]
100pub enum TracingReceiver {
101	/// Output the tracing records using the log.
102	Log,
103}
104
105impl Into<sc_tracing::TracingReceiver> for TracingReceiver {
106	fn into(self) -> sc_tracing::TracingReceiver {
107		match self {
108			TracingReceiver::Log => sc_tracing::TracingReceiver::Log,
109		}
110	}
111}
112
113/// The type of the node key.
114#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
115#[value(rename_all = "kebab-case")]
116pub enum NodeKeyType {
117	/// Use ed25519.
118	Ed25519,
119}
120
121/// The crypto scheme to use.
122#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
123#[value(rename_all = "kebab-case")]
124pub enum CryptoScheme {
125	/// Use ed25519.
126	Ed25519,
127	/// Use sr25519.
128	Sr25519,
129	/// Use ecdsa.
130	Ecdsa,
131}
132
133/// The type of the output format.
134#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
135#[value(rename_all = "kebab-case")]
136pub enum OutputType {
137	/// Output as json.
138	Json,
139	/// Output as text.
140	Text,
141}
142
143/// How to execute blocks
144#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
145#[value(rename_all = "kebab-case")]
146pub enum ExecutionStrategy {
147	/// Execute with native build (if available, WebAssembly otherwise).
148	Native,
149	/// Only execute with the WebAssembly build.
150	Wasm,
151	/// Execute with both native (where available) and WebAssembly builds.
152	Both,
153	/// Execute with the native build if possible; if it fails, then execute with WebAssembly.
154	NativeElseWasm,
155}
156
157/// Available RPC methods.
158#[allow(missing_docs)]
159#[derive(Debug, Copy, Clone, PartialEq, ValueEnum)]
160#[value(rename_all = "kebab-case")]
161pub enum RpcMethods {
162	/// Expose every RPC method only when RPC is listening on `localhost`,
163	/// otherwise serve only safe RPC methods.
164	Auto,
165	/// Allow only a safe subset of RPC methods.
166	Safe,
167	/// Expose every RPC method (even potentially unsafe ones).
168	Unsafe,
169}
170
171impl FromStr for RpcMethods {
172	type Err = String;
173
174	fn from_str(s: &str) -> Result<Self, Self::Err> {
175		match s {
176			"safe" => Ok(RpcMethods::Safe),
177			"unsafe" => Ok(RpcMethods::Unsafe),
178			"auto" => Ok(RpcMethods::Auto),
179			invalid => Err(format!("Invalid rpc methods {invalid}")),
180		}
181	}
182}
183
184impl Into<sc_service::config::RpcMethods> for RpcMethods {
185	fn into(self) -> sc_service::config::RpcMethods {
186		match self {
187			RpcMethods::Auto => sc_service::config::RpcMethods::Auto,
188			RpcMethods::Safe => sc_service::config::RpcMethods::Safe,
189			RpcMethods::Unsafe => sc_service::config::RpcMethods::Unsafe,
190		}
191	}
192}
193
194/// CORS setting
195///
196/// The type is introduced to overcome `Option<Option<T>>` handling of `clap`.
197#[derive(Clone, Debug)]
198pub enum Cors {
199	/// All hosts allowed.
200	All,
201	/// Only hosts on the list are allowed.
202	List(Vec<String>),
203}
204
205impl From<Cors> for Option<Vec<String>> {
206	fn from(cors: Cors) -> Self {
207		match cors {
208			Cors::All => None,
209			Cors::List(list) => Some(list),
210		}
211	}
212}
213
214impl FromStr for Cors {
215	type Err = crate::Error;
216
217	fn from_str(s: &str) -> Result<Self, Self::Err> {
218		let mut is_all = false;
219		let mut origins = Vec::new();
220		for part in s.split(',') {
221			match part {
222				"all" | "*" => {
223					is_all = true;
224					break
225				},
226				other => origins.push(other.to_owned()),
227			}
228		}
229
230		if is_all {
231			Ok(Cors::All)
232		} else {
233			Ok(Cors::List(origins))
234		}
235	}
236}
237
238/// Database backend
239#[derive(Debug, Clone, PartialEq, Copy, clap::ValueEnum)]
240#[value(rename_all = "lower")]
241pub enum Database {
242	/// Facebooks RocksDB
243	#[cfg(feature = "rocksdb")]
244	RocksDb,
245	/// ParityDb. <https://github.com/paritytech/parity-db/>
246	ParityDb,
247	/// Detect whether there is an existing database. Use it, if there is, if not, create new
248	/// instance of ParityDb
249	Auto,
250	/// ParityDb. <https://github.com/paritytech/parity-db/>
251	#[value(name = "paritydb-experimental")]
252	ParityDbDeprecated,
253}
254
255impl Database {
256	/// Returns all the variants of this enum to be shown in the cli.
257	pub const fn variants() -> &'static [&'static str] {
258		&[
259			#[cfg(feature = "rocksdb")]
260			"rocksdb",
261			"paritydb",
262			"paritydb-experimental",
263			"auto",
264		]
265	}
266}
267
268/// Whether off-chain workers are enabled.
269#[allow(missing_docs)]
270#[derive(Debug, Clone, ValueEnum)]
271#[value(rename_all = "kebab-case")]
272pub enum OffchainWorkerEnabled {
273	/// Always have offchain worker enabled.
274	Always,
275	/// Never enable the offchain worker.
276	Never,
277	/// Only enable the offchain worker when running as a validator (or collator, if this is a
278	/// parachain node).
279	WhenAuthority,
280}
281
282/// Syncing mode.
283#[derive(Debug, Clone, Copy, ValueEnum, PartialEq)]
284#[value(rename_all = "kebab-case")]
285pub enum SyncMode {
286	/// Full sync. Download and verify all blocks.
287	Full,
288	/// Download blocks without executing them. Download latest state with proofs.
289	Fast,
290	/// Download blocks without executing them. Download latest state without proofs.
291	FastUnsafe,
292	/// Prove finality and download the latest state.
293	Warp,
294}
295
296impl Into<sc_network::config::SyncMode> for SyncMode {
297	fn into(self) -> sc_network::config::SyncMode {
298		match self {
299			SyncMode::Full => sc_network::config::SyncMode::Full,
300			SyncMode::Fast => sc_network::config::SyncMode::LightState {
301				skip_proofs: false,
302				storage_chain_mode: false,
303			},
304			SyncMode::FastUnsafe => sc_network::config::SyncMode::LightState {
305				skip_proofs: true,
306				storage_chain_mode: false,
307			},
308			SyncMode::Warp => sc_network::config::SyncMode::Warp,
309		}
310	}
311}
312
313/// Network backend type.
314#[derive(Debug, Clone, Copy, ValueEnum, PartialEq)]
315#[value(rename_all = "lower")]
316pub enum NetworkBackendType {
317	/// Use libp2p for P2P networking.
318	Libp2p,
319
320	/// Use litep2p for P2P networking.
321	Litep2p,
322}
323
324impl Into<sc_network::config::NetworkBackendType> for NetworkBackendType {
325	fn into(self) -> sc_network::config::NetworkBackendType {
326		match self {
327			Self::Libp2p => sc_network::config::NetworkBackendType::Libp2p,
328			Self::Litep2p => sc_network::config::NetworkBackendType::Litep2p,
329		}
330	}
331}