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::{CliConfiguration, DatabaseParams, PruningParams, Resultas 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};
2526/// 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)]
31pub pruning_params: PruningParams,
3233#[allow(missing_docs)]
34 #[clap(flatten)]
35pub shared_params: SharedParams,
3637#[allow(missing_docs)]
38 #[clap(flatten)]
39pub database_params: DatabaseParams,
40}
4142/// Serializable `chain-info` subcommand output.
43#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, serde::Serialize)]
44struct ChainInfo<B: BlockT> {
45/// Best block hash.
46best_hash: B::Hash,
47/// Best block number.
48best_number: <<B as BlockT>::Header as HeaderT>::Number,
49/// Genesis block hash.
50genesis_hash: B::Hash,
51/// The head of the finalized chain.
52finalized_hash: B::Hash,
53/// Last finalized block number.
54finalized_number: <<B as BlockT>::Header as HeaderT>::Number,
55}
5657impl<B: BlockT> From<Info<B>> for ChainInfo<B> {
58fn 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}
6869impl ChainInfoCmd {
70/// Run the `chain-info` subcommand
71pub fn run<B>(&self, config: &sc_service::Configuration) -> CliResult<()>
72where
73B: BlockT,
74 {
75let 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 };
82let backend = sc_service::new_db_backend::<B>(db_config)?;
83let info: ChainInfo<B> = backend.blockchain().info().into();
84let mut out = io::stdout();
85 serde_json::to_writer_pretty(&mut out, &info)
86 .map_err(|e| format!("Error writing JSON: {}", e))?;
87Ok(())
88 }
89}
9091impl CliConfiguration for ChainInfoCmd {
92fn shared_params(&self) -> &SharedParams {
93&self.shared_params
94 }
9596fn pruning_params(&self) -> Option<&PruningParams> {
97Some(&self.pruning_params)
98 }
99100fn database_params(&self) -> Option<&DatabaseParams> {
101Some(&self.database_params)
102 }
103}