Skip to main content

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