Skip to main content

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	#[arg(skip)]
266	pub(crate) _phantom: PhantomData<Config>,
267}
268
269/// Development sealing mode.
270#[derive(Debug, Clone, Copy)]
271pub(crate) enum DevSealMode {
272	/// Produces blocks immediately upon receiving transactions.
273	InstantSeal,
274	/// Produces blocks at fixed time intervals.
275	/// The u64 parameter represents the block time in milliseconds.
276	ManualSeal(u64),
277}
278
279/// Collator implementation to use.
280#[derive(PartialEq, Debug, ValueEnum, Clone, Copy)]
281pub enum AuthoringPolicy {
282	/// Use the lookahead collator. Builds a block once per imported relay chain block and
283	/// on relay chain forks. Default for asynchronous backing chains.
284	Lookahead,
285	/// Use the slot-based collator. Builds a block based on time. Can utilize multiple cores,
286	/// always builds on the best relay chain block available. Should be used with elastic-scaling
287	/// chains.
288	SlotBased,
289}
290
291impl Display for AuthoringPolicy {
292	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
293		match self {
294			AuthoringPolicy::Lookahead => write!(f, "lookahead"),
295			AuthoringPolicy::SlotBased => write!(f, "slot-based"),
296		}
297	}
298}
299
300impl<Config: CliConfig> Cli<Config> {
301	pub(crate) fn node_extra_args(&self) -> NodeExtraArgs {
302		NodeExtraArgs {
303			authoring_policy: self
304				.experimental_use_slot_based
305				.then(|| AuthoringPolicy::SlotBased)
306				.unwrap_or(self.authoring),
307			export_pov: self.export_pov_to_path.clone(),
308			max_pov_percentage: self.run.experimental_max_pov_percentage,
309			statement_store_config: self.enable_statement_store.then_some(
310				sc_statement_store::Config {
311					max_total_statements: self.statement_store_max_total_statements,
312					max_total_size: self.statement_store_max_total_size,
313					purge_after_sec: self.statement_store_purge_after_sec,
314					network_workers: self.statement_network_workers,
315					rate_limit: self.statement_rate_limit,
316				},
317			),
318			storage_monitor: self.storage_monitor.clone(),
319		}
320	}
321
322	/// Returns the dev seal mode if the node is in dev mode.
323	pub(crate) fn dev_mode(&self) -> Option<DevSealMode> {
324		if self.instant_seal {
325			Some(DevSealMode::InstantSeal)
326		} else if let Some(dev_block_time) = self.dev_block_time {
327			Some(DevSealMode::ManualSeal(dev_block_time))
328		} else if self.run.base.is_dev().unwrap_or(false) {
329			Some(DevSealMode::ManualSeal(DEFAULT_DEV_BLOCK_TIME_MS))
330		} else {
331			None
332		}
333	}
334}
335
336impl<Config: CliConfig> SubstrateCli for Cli<Config> {
337	fn impl_name() -> String {
338		Self::executable_name()
339	}
340
341	fn impl_version() -> String {
342		Config::impl_version()
343	}
344
345	fn description() -> String {
346		Config::description(Self::executable_name())
347	}
348
349	fn author() -> String {
350		Config::author()
351	}
352
353	fn support_url() -> String {
354		Config::support_url()
355	}
356
357	fn copyright_start_year() -> i32 {
358		Config::copyright_start_year() as i32
359	}
360
361	fn load_spec(&self, id: &str) -> Result<Box<dyn ChainSpec>, String> {
362		match &self.chain_spec_loader {
363			Some(chain_spec_loader) => chain_spec_loader.load_spec(id),
364			None => DiskChainSpecLoader.load_spec(id),
365		}
366	}
367}
368
369/// The relay chain CLI flags. These are passed in after a `--` at the end.
370#[derive(Debug)]
371pub struct RelayChainCli<Config: CliConfig> {
372	/// The actual relay chain cli object.
373	pub base: polkadot_cli::RunCmd,
374
375	/// Optional chain id that should be passed to the relay chain.
376	pub chain_id: Option<String>,
377
378	/// The base path that should be used by the relay chain.
379	pub base_path: Option<PathBuf>,
380
381	_phantom: PhantomData<Config>,
382}
383
384impl<Config: CliConfig> RelayChainCli<Config> {
385	fn polkadot_cmd() -> Command {
386		let help_template = color_print::cformat!(
387			"The arguments that are passed to the relay chain node. \n\
388			\n\
389			<bold><underline>RELAY_CHAIN_ARGS:</></> \n\
390			{{options}}",
391		);
392
393		polkadot_cli::RunCmd::command()
394			.no_binary_name(true)
395			.help_template(help_template)
396	}
397
398	/// Parse the relay chain CLI parameters using the parachain `Configuration`.
399	pub fn new<'a>(
400		para_config: &sc_service::Configuration,
401		relay_chain_args: impl Iterator<Item = &'a String>,
402	) -> Self {
403		let polkadot_cmd = Self::polkadot_cmd();
404		let matches = polkadot_cmd.get_matches_from(relay_chain_args);
405		let base = FromArgMatches::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
406
407		let extension = Extensions::try_get(&*para_config.chain_spec);
408		let chain_id = extension.map(|e| e.relay_chain());
409
410		let base_path = para_config.base_path.path().join("polkadot");
411		Self { base, chain_id, base_path: Some(base_path), _phantom: Default::default() }
412	}
413}
414
415impl<Config: CliConfig> SubstrateCli for RelayChainCli<Config> {
416	fn impl_name() -> String {
417		Cli::<Config>::impl_name()
418	}
419
420	fn impl_version() -> String {
421		Cli::<Config>::impl_version()
422	}
423
424	fn description() -> String {
425		Cli::<Config>::description()
426	}
427
428	fn author() -> String {
429		Cli::<Config>::author()
430	}
431
432	fn support_url() -> String {
433		Cli::<Config>::support_url()
434	}
435
436	fn copyright_start_year() -> i32 {
437		Cli::<Config>::copyright_start_year()
438	}
439
440	fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
441		polkadot_cli::Cli::from_iter([Self::executable_name()].iter()).load_spec(id)
442	}
443}
444
445impl<Config: CliConfig> DefaultConfigurationValues for RelayChainCli<Config> {
446	fn p2p_listen_port() -> u16 {
447		30334
448	}
449
450	fn rpc_listen_port() -> u16 {
451		9945
452	}
453
454	fn prometheus_listen_port() -> u16 {
455		9616
456	}
457}
458
459impl<Config: CliConfig> CliConfiguration<Self> for RelayChainCli<Config> {
460	fn shared_params(&self) -> &SharedParams {
461		self.base.base.shared_params()
462	}
463
464	fn import_params(&self) -> Option<&ImportParams> {
465		self.base.base.import_params()
466	}
467
468	fn network_params(&self) -> Option<&NetworkParams> {
469		self.base.base.network_params()
470	}
471
472	fn keystore_params(&self) -> Option<&KeystoreParams> {
473		self.base.base.keystore_params()
474	}
475
476	fn base_path(&self) -> sc_cli::Result<Option<BasePath>> {
477		Ok(self
478			.shared_params()
479			.base_path()?
480			.or_else(|| self.base_path.clone().map(Into::into)))
481	}
482
483	fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<Vec<RpcEndpoint>>> {
484		self.base.base.rpc_addr(default_listen_port)
485	}
486
487	fn prometheus_config(
488		&self,
489		default_listen_port: u16,
490		chain_spec: &Box<dyn ChainSpec>,
491	) -> sc_cli::Result<Option<PrometheusConfig>> {
492		self.base.base.prometheus_config(default_listen_port, chain_spec)
493	}
494
495	fn init<F>(
496		&self,
497		_support_url: &String,
498		_impl_version: &String,
499		_logger_hook: F,
500	) -> sc_cli::Result<()>
501	where
502		F: FnOnce(&mut sc_cli::LoggerBuilder),
503	{
504		unreachable!("PolkadotCli is never initialized; qed");
505	}
506
507	fn chain_id(&self, is_dev: bool) -> sc_cli::Result<String> {
508		let chain_id = self.base.base.chain_id(is_dev)?;
509
510		Ok(if chain_id.is_empty() { self.chain_id.clone().unwrap_or_default() } else { chain_id })
511	}
512
513	fn role(&self, is_dev: bool) -> sc_cli::Result<sc_service::Role> {
514		self.base.base.role(is_dev)
515	}
516
517	fn transaction_pool(
518		&self,
519		is_dev: bool,
520	) -> sc_cli::Result<sc_service::config::TransactionPoolOptions> {
521		self.base.base.transaction_pool(is_dev)
522	}
523
524	fn trie_cache_maximum_size(&self) -> sc_cli::Result<Option<usize>> {
525		self.base.base.trie_cache_maximum_size()
526	}
527
528	fn rpc_methods(&self) -> sc_cli::Result<sc_service::config::RpcMethods> {
529		self.base.base.rpc_methods()
530	}
531
532	fn rpc_max_connections(&self) -> sc_cli::Result<u32> {
533		self.base.base.rpc_max_connections()
534	}
535
536	fn rpc_cors(&self, is_dev: bool) -> sc_cli::Result<Option<Vec<String>>> {
537		self.base.base.rpc_cors(is_dev)
538	}
539
540	fn default_heap_pages(&self) -> sc_cli::Result<Option<u64>> {
541		self.base.base.default_heap_pages()
542	}
543
544	fn force_authoring(&self) -> sc_cli::Result<bool> {
545		self.base.base.force_authoring()
546	}
547
548	fn disable_grandpa(&self) -> sc_cli::Result<bool> {
549		self.base.base.disable_grandpa()
550	}
551
552	fn max_runtime_instances(&self) -> sc_cli::Result<Option<usize>> {
553		self.base.base.max_runtime_instances()
554	}
555
556	fn announce_block(&self) -> sc_cli::Result<bool> {
557		self.base.base.announce_block()
558	}
559
560	fn telemetry_endpoints(
561		&self,
562		chain_spec: &Box<dyn ChainSpec>,
563	) -> sc_cli::Result<Option<sc_telemetry::TelemetryEndpoints>> {
564		self.base.base.telemetry_endpoints(chain_spec)
565	}
566
567	fn node_name(&self) -> sc_cli::Result<String> {
568		self.base.base.node_name()
569	}
570}