referrerpolicy=no-referrer-when-downgrade

sc_cli/commands/
build_spec_cmd.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	error,
21	params::{NodeKeyParams, SharedParams},
22	CliConfiguration,
23};
24use clap::Parser;
25use log::info;
26use sc_network::config::build_multiaddr;
27use sc_service::{
28	config::{MultiaddrWithPeerId, NetworkConfiguration},
29	ChainSpec,
30};
31use std::io::Write;
32
33/// The `build-spec` command used to build a specification.
34#[derive(Debug, Clone, Parser)]
35pub struct BuildSpecCmd {
36	/// Force raw genesis storage output.
37	#[arg(long)]
38	pub raw: bool,
39
40	/// Disable adding the default bootnode to the specification.
41	/// By default the `/ip4/127.0.0.1/tcp/30333/p2p/NODE_PEER_ID` bootnode is added to the
42	/// specification when no bootnode exists.
43	#[arg(long)]
44	pub disable_default_bootnode: bool,
45
46	#[allow(missing_docs)]
47	#[clap(flatten)]
48	pub shared_params: SharedParams,
49
50	#[allow(missing_docs)]
51	#[clap(flatten)]
52	pub node_key_params: NodeKeyParams,
53}
54
55impl BuildSpecCmd {
56	/// Run the build-spec command
57	pub fn run(
58		&self,
59		mut spec: Box<dyn ChainSpec>,
60		network_config: NetworkConfiguration,
61	) -> error::Result<()> {
62		info!("Building chain spec");
63		let raw_output = self.raw;
64
65		if spec.boot_nodes().is_empty() && !self.disable_default_bootnode {
66			let keys = network_config.node_key.into_keypair()?;
67			let peer_id = keys.public().to_peer_id();
68			let addr = MultiaddrWithPeerId {
69				multiaddr: build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(30333u16)],
70				peer_id: peer_id.into(),
71			};
72			spec.add_boot_node(addr)
73		}
74
75		let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?;
76		if std::io::stdout().write_all(json.as_bytes()).is_err() {
77			let _ = std::io::stderr().write_all(b"Error writing to stdout\n");
78		}
79		Ok(())
80	}
81}
82
83impl CliConfiguration for BuildSpecCmd {
84	fn shared_params(&self) -> &SharedParams {
85		&self.shared_params
86	}
87
88	fn node_key_params(&self) -> Option<&NodeKeyParams> {
89		Some(&self.node_key_params)
90	}
91}