referrerpolicy=no-referrer-when-downgrade

polkadot_omni_node_lib/
cli.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! CLI options of the omni-node. See [`Command`].
18
19/// Default block time for dev mode when using `--dev` flag.
20const DEFAULT_DEV_BLOCK_TIME_MS: u64 = 3000;
21
22use crate::{
23	chain_spec::DiskChainSpecLoader,
24	common::{
25		chain_spec::{Extensions, LoadSpec},
26		NodeExtraArgs,
27	},
28};
29use chain_spec_builder::ChainSpecBuilder;
30use clap::{Command, CommandFactory, FromArgMatches, ValueEnum};
31use sc_chain_spec::ChainSpec;
32use sc_cli::{
33	CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams,
34	RpcEndpoint, SharedParams, SubstrateCli,
35};
36use sc_service::{config::PrometheusConfig, BasePath};
37use sc_storage_monitor::StorageMonitorParams;
38use std::{
39	fmt::{Display, Formatter},
40	marker::PhantomData,
41	path::PathBuf,
42};
43/// Trait that can be used to customize some of the customer-facing info related to the node binary
44/// that is being built using this library.
45///
46/// The related info is shown to the customer as part of logs or help messages.
47/// It does not impact functionality.
48pub trait CliConfig {
49	/// The version of the resulting node binary.
50	fn impl_version() -> String;
51
52	/// The description of the resulting node binary.
53	fn description(executable_name: String) -> String {
54		format!(
55			"The command-line arguments provided first will be passed to the parachain node, \n\
56			and the arguments provided after -- will be passed to the relay chain node. \n\
57			\n\
58			Example: \n\
59			\n\
60			{} [parachain-args] -- [relay-chain-args]",
61			executable_name
62		)
63	}
64
65	/// The author of the resulting node binary.
66	fn author() -> String;
67
68	/// The support URL for the resulting node binary.
69	fn support_url() -> String;
70
71	/// The starting copyright year of the resulting node binary.
72	fn copyright_start_year() -> u16;
73}
74
75/// Sub-commands supported by the collator.
76#[derive(Debug, clap::Subcommand)]
77pub enum Subcommand {
78	/// Key management CLI utilities
79	#[command(subcommand)]
80	Key(sc_cli::KeySubcommand),
81
82	/// Build a chain specification.
83	///
84	/// The `build-spec` command relies on the chain specification built (hard-coded) into the node
85	/// binary, and may utilize the genesis presets of the runtimes  also embedded in the nodes
86	/// that support  this command. Since `polkadot-omni-node` does not contain any embedded
87	/// runtime, and requires a `chain-spec` path to be passed to its `--chain` flag, the command
88	/// isn't bringing significant value as it does for other node binaries (e.g. the
89	///  `polkadot` binary).
90	///
91	/// For a more versatile `chain-spec` manipulation experience please check out the
92	/// `polkadot-omni-node chain-spec-builder` subcommand.
93	#[deprecated(
94		note = "build-spec will be removed after 1/06/2025. Use chain-spec-builder instead"
95	)]
96	BuildSpec(sc_cli::BuildSpecCmd),
97
98	/// Validate blocks.
99	CheckBlock(sc_cli::CheckBlockCmd),
100
101	/// Export blocks.
102	ExportBlocks(sc_cli::ExportBlocksCmd),
103
104	/// Export the state of a given block into a chain spec.
105	ExportState(sc_cli::ExportStateCmd),
106
107	/// Import blocks.
108	ImportBlocks(sc_cli::ImportBlocksCmd),
109
110	/// Revert the chain to a previous state.
111	Revert(sc_cli::RevertCmd),
112
113	/// Subcommand for generating and managing chain specifications.
114	///
115	/// A `chain-spec-builder` subcommand corresponds to the existing `chain-spec-builder` tool
116	/// (<https://crates.io/crates/staging-chain-spec-builder>), which can be used already standalone.
117	/// It provides the same functionality as the tool but bundled with `polkadot-omni-node` to
118	/// enable easier access to chain-spec generation, patching, converting to raw or validation,
119	/// from a single binary, which can be used as a parachain node tool
120	/// For a detailed usage guide please check out the standalone tool's crates.io or docs.rs
121	/// pages:
122	/// - <https://crates.io/crates/staging-chain-spec-builder>
123	/// - <https://docs.rs/staging-chain-spec-builder/latest/staging_chain_spec_builder/>
124	ChainSpecBuilder(ChainSpecBuilder),
125
126	/// Remove the whole chain.
127	PurgeChain(cumulus_client_cli::PurgeChainCmd),
128	/// Export the genesis state of the parachain.
129	#[command(alias = "export-genesis-state")]
130	ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),
131
132	/// Export the genesis wasm of the parachain.
133	ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
134
135	/// Sub-commands concerned with benchmarking.
136	/// The pallet benchmarking moved to the `pallet` sub-command.
137	#[command(subcommand)]
138	Benchmark(frame_benchmarking_cli::BenchmarkCmd),
139}
140
141/// CLI Options shipped with `polkadot-omni-node`.
142#[derive(clap::Parser)]
143#[command(
144	propagate_version = true,
145	args_conflicts_with_subcommands = true,
146	subcommand_negates_reqs = true
147)]
148pub struct Cli<Config: CliConfig> {
149	#[arg(skip)]
150	pub(crate) chain_spec_loader: Option<Box<dyn LoadSpec>>,
151
152	/// Possible subcommands. See [`Subcommand`].
153	#[command(subcommand)]
154	pub subcommand: Option<Subcommand>,
155
156	/// The shared parameters with all cumulus-based parachain nodes.
157	#[command(flatten)]
158	pub run: cumulus_client_cli::RunCmd,
159
160	/// Parameters for storage monitoring.
161	#[command(flatten)]
162	pub storage_monitor: StorageMonitorParams,
163
164	/// Start a dev node that produces a block each `dev_block_time` ms.
165	///
166	/// This is a dev option. It enables a manual sealing, meaning blocks are produced manually
167	/// rather than being part of an actual network consensus process. Using the option won't
168	/// result in starting or connecting to a parachain network. The resulting node will work on
169	/// its own, running the wasm blob and artificially producing a block each `dev_block_time` ms,
170	/// as if it was part of a parachain.
171	///
172	/// The `--dev` flag sets the `dev_block_time` to a default value of 3000ms unless explicitly
173	/// provided.
174	#[arg(long, conflicts_with = "instant_seal")]
175	pub dev_block_time: Option<u64>,
176
177	/// Start a dev node with instant seal.
178	///
179	/// This is a dev option that enables instant sealing, meaning blocks are produced
180	/// immediately when transactions are received, rather than at fixed intervals.
181	/// Using this option won't result in starting or connecting to a parachain network.
182	/// The resulting node will work on its own, running the wasm blob and producing blocks
183	/// instantly upon receiving transactions.
184	#[arg(long, conflicts_with = "dev_block_time")]
185	pub instant_seal: bool,
186
187	/// DEPRECATED: This feature has been stabilized, pLease use `--authoring slot-based` instead.
188	///
189	/// Use slot-based collator which can handle elastic scaling.
190	/// Use with care, this flag is unstable and subject to change.
191	#[arg(long, conflicts_with = "authoring")]
192	pub experimental_use_slot_based: bool,
193
194	/// Authoring style to use.
195	#[arg(long, default_value_t = AuthoringPolicy::Lookahead)]
196	pub authoring: AuthoringPolicy,
197
198	/// Disable automatic hardware benchmarks.
199	///
200	/// By default these benchmarks are automatically ran at startup and measure
201	/// the CPU speed, the memory bandwidth and the disk speed.
202	///
203	/// The results are then printed out in the logs, and also sent as part of
204	/// telemetry, if telemetry is enabled.
205	#[arg(long)]
206	pub no_hardware_benchmarks: bool,
207
208	/// Export all `PoVs` build by this collator to the given folder.
209	///
210	/// This is useful for debugging issues that are occurring while validating these `PoVs` on the
211	/// relay chain.
212	#[arg(long)]
213	pub export_pov_to_path: Option<PathBuf>,
214
215	/// Relay chain arguments
216	#[arg(raw = true)]
217	pub relay_chain_args: Vec<String>,
218
219	/// Enable the statement store.
220	///
221	/// The statement store reads the storage of the chain to determine if users are allowed to
222	/// store statements or not.
223	#[arg(long)]
224	pub enable_statement_store: bool,
225
226	/// Number of concurrent workers for statement validation from the network.
227	///
228	/// Only relevant when `--enable-statement-store` is used.
229	#[arg(long, default_value_t = sc_statement_store::DEFAULT_NETWORK_WORKERS)]
230	pub statement_network_workers: usize,
231
232	/// Maximum statements per second per peer before rate limiting kicks in.
233	///
234	/// Uses a token bucket algorithm that allows short bursts up to this limit
235	/// while enforcing the average rate over time.
236	///
237	/// Only relevant when `--enable-statement-store` is used.
238	#[arg(long, default_value_t = sc_statement_store::DEFAULT_RATE_LIMIT)]
239	pub statement_rate_limit: u32,
240
241	/// Maximum number of statements the statement store can hold.
242	///
243	/// Once this limit is reached, lower-priority statements may be evicted.
244	///
245	/// Only relevant when `--enable-statement-store` is used.
246	#[arg(long, default_value_t = sc_statement_store::DEFAULT_MAX_TOTAL_STATEMENTS)]
247	pub statement_store_max_total_statements: usize,
248
249	/// Maximum total data size (in bytes) the statement store can hold.
250	///
251	/// Once this limit is reached, lower-priority statements may be evicted.
252	///
253	/// Only relevant when `--enable-statement-store` is used.
254	#[arg(long, default_value_t = sc_statement_store::DEFAULT_MAX_TOTAL_SIZE)]
255	pub statement_store_max_total_size: usize,
256
257	/// Number of seconds for which removed statements won't be allowed to be added back.
258	///
259	/// This prevents old statements from being re-propagated on the network.
260	///
261	/// Only relevant when `--enable-statement-store` is used.
262	#[arg(long, default_value_t = sc_statement_store::DEFAULT_PURGE_AFTER_SEC)]
263	pub statement_store_purge_after_sec: u64,
264
265	/// Upper bound on collator reserved-peer slots.
266	#[arg(long, value_name = "N", default_value_t = 32)]
267	pub collator_reserved_slots: usize,
268
269	/// HOP (Hand-Off Protocol) configuration parameters.
270	#[command(flatten)]
271	pub hop: sc_hop::HopParams,
272
273	#[arg(skip)]
274	pub(crate) _phantom: PhantomData<Config>,
275}
276
277/// Development sealing mode.
278#[derive(Debug, Clone, Copy)]
279pub(crate) enum DevSealMode {
280	/// Produces blocks immediately upon receiving transactions.
281	InstantSeal,
282	/// Produces blocks at fixed time intervals.
283	/// The u64 parameter represents the block time in milliseconds.
284	ManualSeal(u64),
285}
286
287/// Collator implementation to use.
288#[derive(PartialEq, Debug, ValueEnum, Clone, Copy)]
289pub enum AuthoringPolicy {
290	/// Use the lookahead collator. Builds a block once per imported relay chain block and
291	/// on relay chain forks. Default for asynchronous backing chains.
292	Lookahead,
293	/// Use the slot-based collator. Builds a block based on time. Can utilize multiple cores,
294	/// always builds on the best relay chain block available. Should be used with elastic-scaling
295	/// chains.
296	SlotBased,
297}
298
299impl Display for AuthoringPolicy {
300	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
301		match self {
302			AuthoringPolicy::Lookahead => write!(f, "lookahead"),
303			AuthoringPolicy::SlotBased => write!(f, "slot-based"),
304		}
305	}
306}
307
308impl<Config: CliConfig> Cli<Config> {
309	pub(crate) fn node_extra_args(&self) -> NodeExtraArgs {
310		NodeExtraArgs {
311			authoring_policy: self
312				.experimental_use_slot_based
313				.then(|| AuthoringPolicy::SlotBased)
314				.unwrap_or(self.authoring),
315			export_pov: self.export_pov_to_path.clone(),
316			max_pov_percentage: self.run.experimental_max_pov_percentage,
317			statement_store_config: self.enable_statement_store.then_some(
318				sc_statement_store::Config {
319					max_total_statements: self.statement_store_max_total_statements,
320					max_total_size: self.statement_store_max_total_size,
321					purge_after_sec: self.statement_store_purge_after_sec,
322					network_workers: self.statement_network_workers,
323					rate_limit: self.statement_rate_limit,
324				},
325			),
326			storage_monitor: self.storage_monitor.clone(),
327			collator_reserved_slots: self.collator_reserved_slots,
328			hop: self.hop.enabled.then(|| self.hop.clone()),
329		}
330	}
331
332	/// Returns the dev seal mode if the node is in dev mode.
333	pub(crate) fn dev_mode(&self) -> Option<DevSealMode> {
334		if self.instant_seal {
335			Some(DevSealMode::InstantSeal)
336		} else if let Some(dev_block_time) = self.dev_block_time {
337			Some(DevSealMode::ManualSeal(dev_block_time))
338		} else if self.run.base.is_dev().unwrap_or(false) {
339			Some(DevSealMode::ManualSeal(DEFAULT_DEV_BLOCK_TIME_MS))
340		} else {
341			None
342		}
343	}
344}
345
346impl<Config: CliConfig> SubstrateCli for Cli<Config> {
347	fn impl_name() -> String {
348		Self::executable_name()
349	}
350
351	fn impl_version() -> String {
352		Config::impl_version()
353	}
354
355	fn description() -> String {
356		Config::description(Self::executable_name())
357	}
358
359	fn author() -> String {
360		Config::author()
361	}
362
363	fn support_url() -> String {
364		Config::support_url()
365	}
366
367	fn copyright_start_year() -> i32 {
368		Config::copyright_start_year() as i32
369	}
370
371	fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, String> {
372		match &self.chain_spec_loader {
373			Some(chain_spec_loader) => chain_spec_loader.load_spec(id),
374			None => DiskChainSpecLoader.load_spec(id),
375		}
376	}
377}
378
379/// The relay chain CLI flags. These are passed in after a `--` at the end.
380#[derive(Debug)]
381pub struct RelayChainCli<Config: CliConfig> {
382	/// The actual relay chain cli object.
383	pub base: polkadot_cli::RunCmd,
384
385	/// Optional chain id that should be passed to the relay chain.
386	pub chain_id: Option<String>,
387
388	/// The base path that should be used by the relay chain.
389	pub base_path: Option<PathBuf>,
390
391	_phantom: PhantomData<Config>,
392}
393
394impl<Config: CliConfig> RelayChainCli<Config> {
395	fn polkadot_cmd() -> Command {
396		let help_template = color_print::cformat!(
397			"The arguments that are passed to the relay chain node. \n\
398			\n\
399			<bold><underline>RELAY_CHAIN_ARGS:</></> \n\
400			{{options}}",
401		);
402
403		polkadot_cli::RunCmd::command()
404			.no_binary_name(true)
405			.help_template(help_template)
406	}
407
408	/// Parse the relay chain CLI parameters using the parachain `Configuration`.
409	pub fn new<'a>(
410		para_config: &sc_service::Configuration,
411		relay_chain_args: impl Iterator<Item = &'a String>,
412	) -> Self {
413		let polkadot_cmd = Self::polkadot_cmd();
414		let matches = polkadot_cmd.get_matches_from(relay_chain_args);
415		let base = FromArgMatches::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
416
417		let extension = Extensions::try_get(&*para_config.chain_spec);
418		let chain_id = extension.map(|e| e.relay_chain());
419
420		let base_path = para_config.base_path.path().join("polkadot");
421		Self { base, chain_id, base_path: Some(base_path), _phantom: Default::default() }
422	}
423}
424
425impl<Config: CliConfig> SubstrateCli for RelayChainCli<Config> {
426	fn impl_name() -> String {
427		Cli::<Config>::impl_name()
428	}
429
430	fn impl_version() -> String {
431		Cli::<Config>::impl_version()
432	}
433
434	fn description() -> String {
435		Cli::<Config>::description()
436	}
437
438	fn author() -> String {
439		Cli::<Config>::author()
440	}
441
442	fn support_url() -> String {
443		Cli::<Config>::support_url()
444	}
445
446	fn copyright_start_year() -> i32 {
447		Cli::<Config>::copyright_start_year()
448	}
449
450	fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
451		polkadot_cli::Cli::from_iter([Self::executable_name()].iter()).load_spec(id)
452	}
453}
454
455impl<Config: CliConfig> DefaultConfigurationValues for RelayChainCli<Config> {
456	fn p2p_listen_port() -> u16 {
457		30334
458	}
459
460	fn rpc_listen_port() -> u16 {
461		9945
462	}
463
464	fn prometheus_listen_port() -> u16 {
465		9616
466	}
467}
468
469impl<Config: CliConfig> CliConfiguration<Self> for RelayChainCli<Config> {
470	fn shared_params(&self) -> &SharedParams {
471		self.base.base.shared_params()
472	}
473
474	fn import_params(&self) -> Option<&ImportParams> {
475		self.base.base.import_params()
476	}
477
478	fn network_params(&self) -> Option<&NetworkParams> {
479		self.base.base.network_params()
480	}
481
482	fn keystore_params(&self) -> Option<&KeystoreParams> {
483		self.base.base.keystore_params()
484	}
485
486	fn base_path(&self) -> sc_cli::Result<Option<BasePath>> {
487		Ok(self
488			.shared_params()
489			.base_path()?
490			.or_else(|| self.base_path.clone().map(Into::into)))
491	}
492
493	fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<Vec<RpcEndpoint>>> {
494		self.base.base.rpc_addr(default_listen_port)
495	}
496
497	fn prometheus_config(
498		&self,
499		default_listen_port: u16,
500		chain_spec: &Box<dyn ChainSpec>,
501	) -> sc_cli::Result<Option<PrometheusConfig>> {
502		self.base.base.prometheus_config(default_listen_port, chain_spec)
503	}
504
505	fn init<F>(
506		&self,
507		_support_url: &String,
508		_impl_version: &String,
509		_logger_hook: F,
510	) -> sc_cli::Result<()>
511	where
512		F: FnOnce(&mut sc_cli::LoggerBuilder),
513	{
514		unreachable!("PolkadotCli is never initialized; qed");
515	}
516
517	fn chain_id(&self, is_dev: bool) -> sc_cli::Result<String> {
518		let chain_id = self.base.base.chain_id(is_dev)?;
519
520		Ok(if chain_id.is_empty() { self.chain_id.clone().unwrap_or_default() } else { chain_id })
521	}
522
523	fn role(&self, is_dev: bool) -> sc_cli::Result<sc_service::Role> {
524		self.base.base.role(is_dev)
525	}
526
527	fn transaction_pool(
528		&self,
529		is_dev: bool,
530	) -> sc_cli::Result<sc_service::config::TransactionPoolOptions> {
531		self.base.base.transaction_pool(is_dev)
532	}
533
534	fn trie_cache_maximum_size(&self) -> sc_cli::Result<Option<usize>> {
535		self.base.base.trie_cache_maximum_size()
536	}
537
538	fn rpc_methods(&self) -> sc_cli::Result<sc_service::config::RpcMethods> {
539		self.base.base.rpc_methods()
540	}
541
542	fn rpc_max_connections(&self) -> sc_cli::Result<u32> {
543		self.base.base.rpc_max_connections()
544	}
545
546	fn rpc_cors(&self, is_dev: bool) -> sc_cli::Result<Option<Vec<String>>> {
547		self.base.base.rpc_cors(is_dev)
548	}
549
550	fn default_heap_pages(&self) -> sc_cli::Result<Option<u64>> {
551		self.base.base.default_heap_pages()
552	}
553
554	fn force_authoring(&self) -> sc_cli::Result<bool> {
555		self.base.base.force_authoring()
556	}
557
558	fn disable_grandpa(&self) -> sc_cli::Result<bool> {
559		self.base.base.disable_grandpa()
560	}
561
562	fn max_runtime_instances(&self) -> sc_cli::Result<Option<usize>> {
563		self.base.base.max_runtime_instances()
564	}
565
566	fn announce_block(&self) -> sc_cli::Result<bool> {
567		self.base.base.announce_block()
568	}
569
570	fn telemetry_endpoints(
571		&self,
572		chain_spec: &Box<dyn ChainSpec>,
573	) -> sc_cli::Result<Option<sc_telemetry::TelemetryEndpoints>> {
574		self.base.base.telemetry_endpoints(chain_spec)
575	}
576
577	fn node_name(&self) -> sc_cli::Result<String> {
578		self.base.base.node_name()
579	}
580}