1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// 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.
1011// 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.
1516// 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/>.
1819use 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;
3233/// 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)]
38pub raw: bool,
3940/// 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)]
44pub disable_default_bootnode: bool,
4546#[allow(missing_docs)]
47 #[clap(flatten)]
48pub shared_params: SharedParams,
4950#[allow(missing_docs)]
51 #[clap(flatten)]
52pub node_key_params: NodeKeyParams,
53}
5455impl BuildSpecCmd {
56/// Run the build-spec command
57pub fn run(
58&self,
59mut spec: Box<dyn ChainSpec>,
60 network_config: NetworkConfiguration,
61 ) -> error::Result<()> {
62info!("Building chain spec");
63let raw_output = self.raw;
6465if spec.boot_nodes().is_empty() && !self.disable_default_bootnode {
66let keys = network_config.node_key.into_keypair()?;
67let peer_id = keys.public().to_peer_id();
68let 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 }
7475let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?;
76if std::io::stdout().write_all(json.as_bytes()).is_err() {
77let _ = std::io::stderr().write_all(b"Error writing to stdout\n");
78 }
79Ok(())
80 }
81}
8283impl CliConfiguration for BuildSpecCmd {
84fn shared_params(&self) -> &SharedParams {
85&self.shared_params
86 }
8788fn node_key_params(&self) -> Option<&NodeKeyParams> {
89Some(&self.node_key_params)
90 }
91}