Skip to main content

soil_cli/params/
import_params.rs

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