sc_cli/commands/
chain_info_cmd.rs1use 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#[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#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, serde::Serialize)]
44struct ChainInfo<B: BlockT> {
45 best_hash: B::Hash,
47 best_number: <<B as BlockT>::Header as HeaderT>::Number,
49 genesis_hash: B::Hash,
51 finalized_hash: B::Hash,
53 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 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 pruning_filters: Default::default(),
81 metrics_registry: None,
82 };
83 let backend = sc_service::new_db_backend::<B>(db_config)?;
84 let info: ChainInfo<B> = backend.blockchain().info().into();
85 let mut out = io::stdout();
86 serde_json::to_writer_pretty(&mut out, &info)
87 .map_err(|e| format!("Error writing JSON: {}", e))?;
88 Ok(())
89 }
90}
91
92impl CliConfiguration for ChainInfoCmd {
93 fn shared_params(&self) -> &SharedParams {
94 &self.shared_params
95 }
96
97 fn pruning_params(&self) -> Option<&PruningParams> {
98 Some(&self.pruning_params)
99 }
100
101 fn database_params(&self) -> Option<&DatabaseParams> {
102 Some(&self.database_params)
103 }
104}