referrerpolicy=no-referrer-when-downgrade

sc_cli/commands/
chain_info_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::{CliConfiguration, DatabaseParams, PruningParams, Result as CliResult, SharedParams};
20use codec::{Decode, Encode};
21use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend};
22use sp_blockchain::Info;
23use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
24use std::{fmt::Debug, io};
25
26/// The `chain-info` subcommand used to output db meta columns information.
27#[derive(Debug, Clone, clap::Parser)]
28pub struct ChainInfoCmd {
29	#[allow(missing_docs)]
30	#[clap(flatten)]
31	pub pruning_params: PruningParams,
32
33	#[allow(missing_docs)]
34	#[clap(flatten)]
35	pub shared_params: SharedParams,
36
37	#[allow(missing_docs)]
38	#[clap(flatten)]
39	pub database_params: DatabaseParams,
40}
41
42/// Serializable `chain-info` subcommand output.
43#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, serde::Serialize)]
44struct ChainInfo<B: BlockT> {
45	/// Best block hash.
46	best_hash: B::Hash,
47	/// Best block number.
48	best_number: <<B as BlockT>::Header as HeaderT>::Number,
49	/// Genesis block hash.
50	genesis_hash: B::Hash,
51	/// The head of the finalized chain.
52	finalized_hash: B::Hash,
53	/// Last finalized block number.
54	finalized_number: <<B as BlockT>::Header as HeaderT>::Number,
55}
56
57impl<B: BlockT> From<Info<B>> for ChainInfo<B> {
58	fn from(info: Info<B>) -> Self {
59		ChainInfo::<B> {
60			best_hash: info.best_hash,
61			best_number: info.best_number,
62			genesis_hash: info.genesis_hash,
63			finalized_hash: info.finalized_hash,
64			finalized_number: info.finalized_number,
65		}
66	}
67}
68
69impl ChainInfoCmd {
70	/// Run the `chain-info` subcommand
71	pub fn run<B>(&self, config: &sc_service::Configuration) -> CliResult<()>
72	where
73		B: BlockT,
74	{
75		let db_config = sc_client_db::DatabaseSettings {
76			trie_cache_maximum_size: config.trie_cache_maximum_size,
77			state_pruning: config.state_pruning.clone(),
78			source: config.database.clone(),
79			blocks_pruning: config.blocks_pruning,
80			metrics_registry: None,
81		};
82		let backend = sc_service::new_db_backend::<B>(db_config)?;
83		let info: ChainInfo<B> = backend.blockchain().info().into();
84		let mut out = io::stdout();
85		serde_json::to_writer_pretty(&mut out, &info)
86			.map_err(|e| format!("Error writing JSON: {}", e))?;
87		Ok(())
88	}
89}
90
91impl CliConfiguration for ChainInfoCmd {
92	fn shared_params(&self) -> &SharedParams {
93		&self.shared_params
94	}
95
96	fn pruning_params(&self) -> Option<&PruningParams> {
97		Some(&self.pruning_params)
98	}
99
100	fn database_params(&self) -> Option<&DatabaseParams> {
101		Some(&self.database_params)
102	}
103}