From 4678fee1bb85ff33ca95e9fa61d9571cb2ee8ae4 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Wed, 20 Apr 2022 16:20:50 +0200 Subject: [PATCH 01/12] Add `blockchain-info` Subcommand --- Cargo.lock | 1 + bin/node-template/node/src/cli.rs | 3 + bin/node-template/node/src/command.rs | 7 ++ bin/node/cli/src/cli.rs | 3 + bin/node/cli/src/command.rs | 7 ++ client/cli/Cargo.toml | 1 + .../cli/src/commands/blockchain_info_cmd.rs | 86 +++++++++++++++++++ client/cli/src/commands/mod.rs | 7 +- 8 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 client/cli/src/commands/blockchain_info_cmd.rs diff --git a/Cargo.lock b/Cargo.lock index 796a3e3d271ce..de7ac5034984b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8242,6 +8242,7 @@ dependencies = [ "regex", "rpassword", "sc-client-api", + "sc-client-db", "sc-keystore", "sc-network", "sc-service", diff --git a/bin/node-template/node/src/cli.rs b/bin/node-template/node/src/cli.rs index 710c7f3f9e145..7668e4225f398 100644 --- a/bin/node-template/node/src/cli.rs +++ b/bin/node-template/node/src/cli.rs @@ -47,4 +47,7 @@ pub enum Subcommand { /// Try some command against runtime state. Note: `try-runtime` feature must be enabled. #[cfg(not(feature = "try-runtime"))] TryRuntime, + + /// Db meta columns information. + BlockchainInfo(sc_cli::BlockchainInfoCmd), } diff --git a/bin/node-template/node/src/command.rs b/bin/node-template/node/src/command.rs index afa4612f1ee4a..86b0438632547 100644 --- a/bin/node-template/node/src/command.rs +++ b/bin/node-template/node/src/command.rs @@ -162,6 +162,13 @@ pub fn run() -> sc_cli::Result<()> { Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \ You can enable it with `--features try-runtime`." .into()), + Some(Subcommand::BlockchainInfo(cmd)) => { + let runner = cli.create_runner(cmd)?; + runner.async_run(|config| { + let PartialComponents { backend, task_manager, .. } = service::new_partial(&config)?; + Ok((cmd.run(backend.clone()), task_manager)) + }) + }, None => { let runner = cli.create_runner(&cli.run)?; runner.run_node_until_exit(|config| async move { diff --git a/bin/node/cli/src/cli.rs b/bin/node/cli/src/cli.rs index 5b5db62199bbd..1ff06b9acc934 100644 --- a/bin/node/cli/src/cli.rs +++ b/bin/node/cli/src/cli.rs @@ -94,4 +94,7 @@ pub enum Subcommand { /// Revert the chain to a previous state. Revert(sc_cli::RevertCmd), + + /// Db meta columns information. + BlockchainInfo(sc_cli::BlockchainInfoCmd), } diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index e2c772e809200..86ecfe0f0b82a 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -206,5 +206,12 @@ pub fn run() -> Result<()> { Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \ You can enable it with `--features try-runtime`." .into()), + Some(Subcommand::BlockchainInfo(cmd)) => { + let runner = cli.create_runner(cmd)?; + runner.async_run(|config| { + let PartialComponents { backend, task_manager, .. } = new_partial(&config)?; + Ok((cmd.run(backend.clone()), task_manager)) + }) + }, } } diff --git a/client/cli/Cargo.toml b/client/cli/Cargo.toml index 1fabbca46968e..53a52bb8331d9 100644 --- a/client/cli/Cargo.toml +++ b/client/cli/Cargo.toml @@ -32,6 +32,7 @@ tokio = { version = "1.17.0", features = ["signal", "rt-multi-thread", "parking_ parity-scale-codec = "3.0.0" sc-client-api = { version = "4.0.0-dev", path = "../api" } +sc-client-db = { version = "0.10.0-dev", path = "../db" } sc-keystore = { version = "4.0.0-dev", path = "../keystore" } sc-network = { version = "0.10.0-dev", path = "../network" } sc-service = { version = "0.10.0-dev", default-features = false, path = "../service" } diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs new file mode 100644 index 0000000000000..2494fea6a6fcd --- /dev/null +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -0,0 +1,86 @@ +// This file is part of Substrate. + +// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use clap::Parser; +use parity_scale_codec::{Decode, Encode}; +use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; +use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; +use sc_client_db::Backend; +use serde::Serialize; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; +use std::{fmt::Debug, io, sync::Arc}; + +/// The `chain-info` subcommand used to output db meta columns information. +#[derive(Debug, Clone, Parser)] +pub struct BlockchainInfoCmd { + #[allow(missing_docs)] + #[clap(flatten)] + pub pruning_params: PruningParams, + + #[allow(missing_docs)] + #[clap(flatten)] + pub shared_params: SharedParams, +} + +/// Serializable `blockchain-info` subcommand output. +#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)] +struct BlockchainInfo { + /// Best block hash. + best_hash: B::Hash, + /// Best block number. + best_number: <::Header as HeaderT>::Number, + /// Genesis block hash. + genesis_hash: B::Hash, + /// The head of the finalized chain. + finalized_hash: B::Hash, + /// Last finalized block number. + finalized_number: <::Header as HeaderT>::Number, + /// Last finalized state. + finalized_state: Option<(B::Hash, <::Header as HeaderT>::Number)>, +} + +impl BlockchainInfoCmd { + /// Run the `blockchain-info` subcommand + pub async fn run(&self, backend: Arc>) -> CliResult<()> + where + B: BlockT, + { + let blockchain_info = backend.blockchain().info(); + let info = BlockchainInfo:: { + best_hash: blockchain_info.best_hash, + best_number: blockchain_info.best_number, + genesis_hash: blockchain_info.genesis_hash, + finalized_hash: blockchain_info.finalized_hash, + finalized_number: blockchain_info.finalized_number, + finalized_state: blockchain_info.finalized_state, + }; + let mut out = Box::new(io::stdout()); + serde_json::to_writer(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; + Ok(()) + } +} + +impl CliConfiguration for BlockchainInfoCmd { + fn shared_params(&self) -> &SharedParams { + &self.shared_params + } + + fn pruning_params(&self) -> Option<&PruningParams> { + Some(&self.pruning_params) + } +} diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index 2b46e1d99caa2..953421a127814 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -15,6 +15,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +mod blockchain_info_cmd; mod build_spec_cmd; mod check_block_cmd; mod export_blocks_cmd; @@ -35,10 +36,10 @@ mod vanity; mod verify; pub use self::{ - build_spec_cmd::BuildSpecCmd, check_block_cmd::CheckBlockCmd, + blockchain_info_cmd::BlockchainInfoCmd, build_spec_cmd::BuildSpecCmd, check_block_cmd::CheckBlockCmd, export_blocks_cmd::ExportBlocksCmd, export_state_cmd::ExportStateCmd, generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, import_blocks_cmd::ImportBlocksCmd, insert_key::InsertKeyCmd, inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd, - key::KeySubcommand, purge_chain_cmd::PurgeChainCmd, revert_cmd::RevertCmd, run_cmd::RunCmd, - sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd, + key::KeySubcommand, purge_chain_cmd::PurgeChainCmd, revert_cmd::RevertCmd, + run_cmd::RunCmd, sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd, }; From ea409545635d331e34d4f5a47ff53a3feab52839 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Wed, 20 Apr 2022 16:37:32 +0200 Subject: [PATCH 02/12] Update comment --- client/cli/src/commands/blockchain_info_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index 2494fea6a6fcd..1846286af7ceb 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -25,7 +25,7 @@ use serde::Serialize; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{fmt::Debug, io, sync::Arc}; -/// The `chain-info` subcommand used to output db meta columns information. +/// The `blockchain-info` subcommand used to output db meta columns information. #[derive(Debug, Clone, Parser)] pub struct BlockchainInfoCmd { #[allow(missing_docs)] From caf8876a32f9943613bf372d1fda3580033c5225 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Wed, 20 Apr 2022 16:43:23 +0200 Subject: [PATCH 03/12] Cleanup --- client/cli/src/commands/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index 953421a127814..10d98ea0989ef 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -40,6 +40,6 @@ pub use self::{ export_blocks_cmd::ExportBlocksCmd, export_state_cmd::ExportStateCmd, generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, import_blocks_cmd::ImportBlocksCmd, insert_key::InsertKeyCmd, inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd, - key::KeySubcommand, purge_chain_cmd::PurgeChainCmd, revert_cmd::RevertCmd, - run_cmd::RunCmd, sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd, + key::KeySubcommand, purge_chain_cmd::PurgeChainCmd, revert_cmd::RevertCmd, run_cmd::RunCmd, + sign::SignCmd, vanity::VanityCmd, verify::VerifyCmd, }; From 6b6876f283292d267a101e81a0d50bcbea79b859 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Thu, 21 Apr 2022 10:14:52 +0200 Subject: [PATCH 04/12] Cleanup --- client/cli/src/commands/blockchain_info_cmd.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index 1846286af7ceb..7b0ba05cd8a4a 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -16,17 +16,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use clap::Parser; use parity_scale_codec::{Decode, Encode}; use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; use sc_client_db::Backend; -use serde::Serialize; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{fmt::Debug, io, sync::Arc}; /// The `blockchain-info` subcommand used to output db meta columns information. -#[derive(Debug, Clone, Parser)] +#[derive(Debug, Clone, clap::Parser)] pub struct BlockchainInfoCmd { #[allow(missing_docs)] #[clap(flatten)] @@ -38,7 +36,7 @@ pub struct BlockchainInfoCmd { } /// Serializable `blockchain-info` subcommand output. -#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, Serialize)] +#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, serde::Serialize)] struct BlockchainInfo { /// Best block hash. best_hash: B::Hash, @@ -50,8 +48,6 @@ struct BlockchainInfo { finalized_hash: B::Hash, /// Last finalized block number. finalized_number: <::Header as HeaderT>::Number, - /// Last finalized state. - finalized_state: Option<(B::Hash, <::Header as HeaderT>::Number)>, } impl BlockchainInfoCmd { @@ -67,10 +63,9 @@ impl BlockchainInfoCmd { genesis_hash: blockchain_info.genesis_hash, finalized_hash: blockchain_info.finalized_hash, finalized_number: blockchain_info.finalized_number, - finalized_state: blockchain_info.finalized_state, }; - let mut out = Box::new(io::stdout()); - serde_json::to_writer(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; + let mut out = io::stdout(); + serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; Ok(()) } } From 15c6ddf78c3c70b0bd4f0876cb1db427684a77d7 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Thu, 21 Apr 2022 10:19:23 +0200 Subject: [PATCH 05/12] Use `sync_run` --- bin/node-template/node/src/command.rs | 6 +++--- bin/node/cli/src/command.rs | 6 +++--- client/cli/src/commands/blockchain_info_cmd.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/node-template/node/src/command.rs b/bin/node-template/node/src/command.rs index 86b0438632547..96400b4b887b9 100644 --- a/bin/node-template/node/src/command.rs +++ b/bin/node-template/node/src/command.rs @@ -164,9 +164,9 @@ pub fn run() -> sc_cli::Result<()> { .into()), Some(Subcommand::BlockchainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.async_run(|config| { - let PartialComponents { backend, task_manager, .. } = service::new_partial(&config)?; - Ok((cmd.run(backend.clone()), task_manager)) + runner.sync_run(|config| { + let PartialComponents { backend, .. } = service::new_partial(&config)?; + cmd.run(backend.clone()) }) }, None => { diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index 86ecfe0f0b82a..9b0b9a41f43c8 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -208,9 +208,9 @@ pub fn run() -> Result<()> { .into()), Some(Subcommand::BlockchainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.async_run(|config| { - let PartialComponents { backend, task_manager, .. } = new_partial(&config)?; - Ok((cmd.run(backend.clone()), task_manager)) + runner.sync_run(|config| { + let PartialComponents { backend, .. } = new_partial(&config)?; + cmd.run(backend.clone()) }) }, } diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index 7b0ba05cd8a4a..c2a2032a06d80 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -52,7 +52,7 @@ struct BlockchainInfo { impl BlockchainInfoCmd { /// Run the `blockchain-info` subcommand - pub async fn run(&self, backend: Arc>) -> CliResult<()> + pub fn run(&self, backend: Arc>) -> CliResult<()> where B: BlockT, { From 009d6a9eb7ca30b9f605c2e7b6e96cd82d3d6b0e Mon Sep 17 00:00:00 2001 From: tgmichel Date: Thu, 21 Apr 2022 11:23:06 +0200 Subject: [PATCH 06/12] Use `sc_client_db` utility fns instead service backend --- bin/node-template/node/src/command.rs | 5 +--- bin/node/cli/src/command.rs | 5 +--- .../cli/src/commands/blockchain_info_cmd.rs | 27 ++++++++++++------- client/db/src/lib.rs | 18 ++++++++++--- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/bin/node-template/node/src/command.rs b/bin/node-template/node/src/command.rs index 96400b4b887b9..72b4d46b8f657 100644 --- a/bin/node-template/node/src/command.rs +++ b/bin/node-template/node/src/command.rs @@ -164,10 +164,7 @@ pub fn run() -> sc_cli::Result<()> { .into()), Some(Subcommand::BlockchainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.sync_run(|config| { - let PartialComponents { backend, .. } = service::new_partial(&config)?; - cmd.run(backend.clone()) - }) + runner.sync_run(|config| cmd.run::(&config)) }, None => { let runner = cli.create_runner(&cli.run)?; diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index 9b0b9a41f43c8..93be2c0100ff5 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -208,10 +208,7 @@ pub fn run() -> Result<()> { .into()), Some(Subcommand::BlockchainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.sync_run(|config| { - let PartialComponents { backend, .. } = new_partial(&config)?; - cmd.run(backend.clone()) - }) + runner.sync_run(|config| cmd.run::(&config)) }, } } diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index c2a2032a06d80..d1440f6307c5e 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -18,10 +18,8 @@ use parity_scale_codec::{Decode, Encode}; use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; -use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; -use sc_client_db::Backend; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; -use std::{fmt::Debug, io, sync::Arc}; +use std::{fmt::Debug, io}; /// The `blockchain-info` subcommand used to output db meta columns information. #[derive(Debug, Clone, clap::Parser)] @@ -52,17 +50,26 @@ struct BlockchainInfo { impl BlockchainInfoCmd { /// Run the `blockchain-info` subcommand - pub fn run(&self, backend: Arc>) -> CliResult<()> + pub fn run(&self, config: &sc_service::Configuration) -> CliResult<()> where B: BlockT, { - let blockchain_info = backend.blockchain().info(); + let db_config = sc_client_db::DatabaseSettings { + state_cache_size: config.state_cache_size, + state_cache_child_ratio: config.state_cache_child_ratio.map(|v| (v, 100)), + state_pruning: config.state_pruning.clone(), + source: config.database.clone(), + keep_blocks: config.keep_blocks.clone(), + }; + let db = sc_client_db::open_database::(&db_config, sc_client_db::DatabaseType::Full) + .map_err(|e| format!("{}", e))?; + let meta = sc_client_db::read_meta::(&*db, sc_client_db::columns::HEADER)?; let info = BlockchainInfo:: { - best_hash: blockchain_info.best_hash, - best_number: blockchain_info.best_number, - genesis_hash: blockchain_info.genesis_hash, - finalized_hash: blockchain_info.finalized_hash, - finalized_number: blockchain_info.finalized_number, + best_hash: meta.best_hash, + best_number: meta.best_number, + genesis_hash: meta.genesis_hash, + finalized_hash: meta.finalized_hash, + finalized_number: meta.finalized_number, }; let mut out = io::stdout(); serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 735058897340e..77cde53df97c7 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -55,8 +55,11 @@ use std::{ use crate::{ stats::StateUsageStats, storage_cache::{new_shared_cache, CachingState, SharedCache, SyncingCachingState}, - utils::{meta_keys, read_db, read_meta, DatabaseType, Meta}, + utils::{meta_keys, read_db, Meta}, }; + +pub use crate::utils::{read_meta, open_database, DatabaseType}; + use codec::{Decode, Encode}; use hash_db::Prefix; use sc_client_api::{ @@ -387,20 +390,29 @@ impl std::fmt::Display for DatabaseSource { } } -pub(crate) mod columns { +/// Database column indexes +pub mod columns { + /// Meta pub const META: u32 = crate::utils::COLUMN_META; + /// State pub const STATE: u32 = 1; + /// State meta pub const STATE_META: u32 = 2; /// maps hashes to lookup keys and numbers to canon hashes. pub const KEY_LOOKUP: u32 = 3; + /// Header pub const HEADER: u32 = 4; + /// Body pub const BODY: u32 = 5; + /// Justifications pub const JUSTIFICATIONS: u32 = 6; + /// Aux pub const AUX: u32 = 8; /// Offchain workers local storage pub const OFFCHAIN: u32 = 9; /// Transactions pub const TRANSACTION: u32 = 11; + /// Body index pub const BODY_INDEX: u32 = 12; } @@ -1013,7 +1025,7 @@ impl Backend { /// /// The pruning window is how old a block must be before the state is pruned. pub fn new(config: DatabaseSettings, canonicalization_delay: u64) -> ClientResult { - let db = crate::utils::open_database::(&config, DatabaseType::Full)?; + let db = open_database::(&config, DatabaseType::Full)?; Self::from_database(db as Arc<_>, canonicalization_delay, &config) } From d2cf477378781c42dfded3114bf49e5cc31d94a4 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Thu, 21 Apr 2022 11:59:54 +0200 Subject: [PATCH 07/12] Use service `Backend` builder --- client/cli/src/commands/blockchain_info_cmd.rs | 16 ++++++++-------- client/db/src/lib.rs | 18 +++--------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index d1440f6307c5e..15b38bc6f8ea9 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -18,6 +18,7 @@ use parity_scale_codec::{Decode, Encode}; use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; +use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{fmt::Debug, io}; @@ -61,15 +62,14 @@ impl BlockchainInfoCmd { source: config.database.clone(), keep_blocks: config.keep_blocks.clone(), }; - let db = sc_client_db::open_database::(&db_config, sc_client_db::DatabaseType::Full) - .map_err(|e| format!("{}", e))?; - let meta = sc_client_db::read_meta::(&*db, sc_client_db::columns::HEADER)?; + let backend = sc_service::new_db_backend::(db_config)?; + let blockchain_info = backend.blockchain().info(); let info = BlockchainInfo:: { - best_hash: meta.best_hash, - best_number: meta.best_number, - genesis_hash: meta.genesis_hash, - finalized_hash: meta.finalized_hash, - finalized_number: meta.finalized_number, + best_hash: blockchain_info.best_hash, + best_number: blockchain_info.best_number, + genesis_hash: blockchain_info.genesis_hash, + finalized_hash: blockchain_info.finalized_hash, + finalized_number: blockchain_info.finalized_number, }; let mut out = io::stdout(); serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 77cde53df97c7..735058897340e 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -55,11 +55,8 @@ use std::{ use crate::{ stats::StateUsageStats, storage_cache::{new_shared_cache, CachingState, SharedCache, SyncingCachingState}, - utils::{meta_keys, read_db, Meta}, + utils::{meta_keys, read_db, read_meta, DatabaseType, Meta}, }; - -pub use crate::utils::{read_meta, open_database, DatabaseType}; - use codec::{Decode, Encode}; use hash_db::Prefix; use sc_client_api::{ @@ -390,29 +387,20 @@ impl std::fmt::Display for DatabaseSource { } } -/// Database column indexes -pub mod columns { - /// Meta +pub(crate) mod columns { pub const META: u32 = crate::utils::COLUMN_META; - /// State pub const STATE: u32 = 1; - /// State meta pub const STATE_META: u32 = 2; /// maps hashes to lookup keys and numbers to canon hashes. pub const KEY_LOOKUP: u32 = 3; - /// Header pub const HEADER: u32 = 4; - /// Body pub const BODY: u32 = 5; - /// Justifications pub const JUSTIFICATIONS: u32 = 6; - /// Aux pub const AUX: u32 = 8; /// Offchain workers local storage pub const OFFCHAIN: u32 = 9; /// Transactions pub const TRANSACTION: u32 = 11; - /// Body index pub const BODY_INDEX: u32 = 12; } @@ -1025,7 +1013,7 @@ impl Backend { /// /// The pruning window is how old a block must be before the state is pruned. pub fn new(config: DatabaseSettings, canonicalization_delay: u64) -> ClientResult { - let db = open_database::(&config, DatabaseType::Full)?; + let db = crate::utils::open_database::(&config, DatabaseType::Full)?; Self::from_database(db as Arc<_>, canonicalization_delay, &config) } From 50a7e05ecc09343ae7d5428a39d4ae9e75f1e384 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Fri, 22 Apr 2022 09:40:51 +0200 Subject: [PATCH 08/12] Impl `From` --- .../cli/src/commands/blockchain_info_cmd.rs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/blockchain_info_cmd.rs index 15b38bc6f8ea9..a5fedcc4f185b 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/blockchain_info_cmd.rs @@ -16,9 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use parity_scale_codec::{Decode, Encode}; use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; +use parity_scale_codec::{Decode, Encode}; use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; +use sp_blockchain::Info; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{fmt::Debug, io}; @@ -49,6 +50,18 @@ struct BlockchainInfo { finalized_number: <::Header as HeaderT>::Number, } +impl From> for BlockchainInfo { + fn from(info: Info) -> Self { + BlockchainInfo:: { + best_hash: info.best_hash, + best_number: info.best_number, + genesis_hash: info.genesis_hash, + finalized_hash: info.finalized_hash, + finalized_number: info.finalized_number, + } + } +} + impl BlockchainInfoCmd { /// Run the `blockchain-info` subcommand pub fn run(&self, config: &sc_service::Configuration) -> CliResult<()> @@ -63,14 +76,7 @@ impl BlockchainInfoCmd { keep_blocks: config.keep_blocks.clone(), }; let backend = sc_service::new_db_backend::(db_config)?; - let blockchain_info = backend.blockchain().info(); - let info = BlockchainInfo:: { - best_hash: blockchain_info.best_hash, - best_number: blockchain_info.best_number, - genesis_hash: blockchain_info.genesis_hash, - finalized_hash: blockchain_info.finalized_hash, - finalized_number: blockchain_info.finalized_number, - }; + let info: BlockchainInfo = backend.blockchain().info().into(); let mut out = io::stdout(); serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; Ok(()) From ecd9a9bd19ab9f86d8e48011b57af7af9cc8cb6a Mon Sep 17 00:00:00 2001 From: tgmichel Date: Fri, 22 Apr 2022 16:07:22 +0200 Subject: [PATCH 09/12] Rename to `chain-info` --- bin/node-template/node/src/cli.rs | 2 +- bin/node-template/node/src/command.rs | 2 +- bin/node/cli/src/cli.rs | 2 +- bin/node/cli/src/command.rs | 2 +- ...ockchain_info_cmd.rs => chain_info_cmd.rs} | 20 +++++++++---------- client/cli/src/commands/mod.rs | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) rename client/cli/src/commands/{blockchain_info_cmd.rs => chain_info_cmd.rs} (85%) diff --git a/bin/node-template/node/src/cli.rs b/bin/node-template/node/src/cli.rs index 7668e4225f398..4ab4d34210c98 100644 --- a/bin/node-template/node/src/cli.rs +++ b/bin/node-template/node/src/cli.rs @@ -49,5 +49,5 @@ pub enum Subcommand { TryRuntime, /// Db meta columns information. - BlockchainInfo(sc_cli::BlockchainInfoCmd), + ChainInfo(sc_cli::ChainInfoCmd), } diff --git a/bin/node-template/node/src/command.rs b/bin/node-template/node/src/command.rs index 72b4d46b8f657..809257f79007c 100644 --- a/bin/node-template/node/src/command.rs +++ b/bin/node-template/node/src/command.rs @@ -162,7 +162,7 @@ pub fn run() -> sc_cli::Result<()> { Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \ You can enable it with `--features try-runtime`." .into()), - Some(Subcommand::BlockchainInfo(cmd)) => { + Some(Subcommand::ChainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; runner.sync_run(|config| cmd.run::(&config)) }, diff --git a/bin/node/cli/src/cli.rs b/bin/node/cli/src/cli.rs index 1ff06b9acc934..5b2977599bab0 100644 --- a/bin/node/cli/src/cli.rs +++ b/bin/node/cli/src/cli.rs @@ -96,5 +96,5 @@ pub enum Subcommand { Revert(sc_cli::RevertCmd), /// Db meta columns information. - BlockchainInfo(sc_cli::BlockchainInfoCmd), + ChainInfo(sc_cli::ChainInfoCmd), } diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index 93be2c0100ff5..b98a38d2dbf5f 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -206,7 +206,7 @@ pub fn run() -> Result<()> { Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \ You can enable it with `--features try-runtime`." .into()), - Some(Subcommand::BlockchainInfo(cmd)) => { + Some(Subcommand::ChainInfo(cmd)) => { let runner = cli.create_runner(cmd)?; runner.sync_run(|config| cmd.run::(&config)) }, diff --git a/client/cli/src/commands/blockchain_info_cmd.rs b/client/cli/src/commands/chain_info_cmd.rs similarity index 85% rename from client/cli/src/commands/blockchain_info_cmd.rs rename to client/cli/src/commands/chain_info_cmd.rs index a5fedcc4f185b..2bc226a35d6ea 100644 --- a/client/cli/src/commands/blockchain_info_cmd.rs +++ b/client/cli/src/commands/chain_info_cmd.rs @@ -23,9 +23,9 @@ use sp_blockchain::Info; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use std::{fmt::Debug, io}; -/// The `blockchain-info` subcommand used to output db meta columns information. +/// The `chain-info` subcommand used to output db meta columns information. #[derive(Debug, Clone, clap::Parser)] -pub struct BlockchainInfoCmd { +pub struct ChainInfoCmd { #[allow(missing_docs)] #[clap(flatten)] pub pruning_params: PruningParams, @@ -35,9 +35,9 @@ pub struct BlockchainInfoCmd { pub shared_params: SharedParams, } -/// Serializable `blockchain-info` subcommand output. +/// Serializable `chain-info` subcommand output. #[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, serde::Serialize)] -struct BlockchainInfo { +struct ChainInfo { /// Best block hash. best_hash: B::Hash, /// Best block number. @@ -50,9 +50,9 @@ struct BlockchainInfo { finalized_number: <::Header as HeaderT>::Number, } -impl From> for BlockchainInfo { +impl From> for ChainInfo { fn from(info: Info) -> Self { - BlockchainInfo:: { + ChainInfo:: { best_hash: info.best_hash, best_number: info.best_number, genesis_hash: info.genesis_hash, @@ -62,8 +62,8 @@ impl From> for BlockchainInfo { } } -impl BlockchainInfoCmd { - /// Run the `blockchain-info` subcommand +impl ChainInfoCmd { + /// Run the `chain-info` subcommand pub fn run(&self, config: &sc_service::Configuration) -> CliResult<()> where B: BlockT, @@ -76,14 +76,14 @@ impl BlockchainInfoCmd { keep_blocks: config.keep_blocks.clone(), }; let backend = sc_service::new_db_backend::(db_config)?; - let info: BlockchainInfo = backend.blockchain().info().into(); + let info: ChainInfo = backend.blockchain().info().into(); let mut out = io::stdout(); serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; Ok(()) } } -impl CliConfiguration for BlockchainInfoCmd { +impl CliConfiguration for ChainInfoCmd { fn shared_params(&self) -> &SharedParams { &self.shared_params } diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index 10d98ea0989ef..4f5dfe6ee3b87 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -15,7 +15,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -mod blockchain_info_cmd; +mod chain_info_cmd; mod build_spec_cmd; mod check_block_cmd; mod export_blocks_cmd; @@ -36,7 +36,7 @@ mod vanity; mod verify; pub use self::{ - blockchain_info_cmd::BlockchainInfoCmd, build_spec_cmd::BuildSpecCmd, check_block_cmd::CheckBlockCmd, + chain_info_cmd::ChainInfoCmd, build_spec_cmd::BuildSpecCmd, check_block_cmd::CheckBlockCmd, export_blocks_cmd::ExportBlocksCmd, export_state_cmd::ExportStateCmd, generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, import_blocks_cmd::ImportBlocksCmd, insert_key::InsertKeyCmd, inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd, From 77a95f0d3d200c8328c6e75be6f9adeb88ef2951 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Fri, 22 Apr 2022 16:08:11 +0200 Subject: [PATCH 10/12] fmt --- client/cli/src/commands/chain_info_cmd.rs | 3 ++- client/cli/src/commands/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client/cli/src/commands/chain_info_cmd.rs b/client/cli/src/commands/chain_info_cmd.rs index 2bc226a35d6ea..d50ef88d02c56 100644 --- a/client/cli/src/commands/chain_info_cmd.rs +++ b/client/cli/src/commands/chain_info_cmd.rs @@ -78,7 +78,8 @@ impl ChainInfoCmd { let backend = sc_service::new_db_backend::(db_config)?; let info: ChainInfo = backend.blockchain().info().into(); let mut out = io::stdout(); - serde_json::to_writer_pretty(&mut out, &info).map_err(|e| format!("Error writing JSON: {}", e))?; + serde_json::to_writer_pretty(&mut out, &info) + .map_err(|e| format!("Error writing JSON: {}", e))?; Ok(()) } } diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index 4f5dfe6ee3b87..8e84afa34e24a 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -15,8 +15,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -mod chain_info_cmd; mod build_spec_cmd; +mod chain_info_cmd; mod check_block_cmd; mod export_blocks_cmd; mod export_state_cmd; @@ -36,7 +36,7 @@ mod vanity; mod verify; pub use self::{ - chain_info_cmd::ChainInfoCmd, build_spec_cmd::BuildSpecCmd, check_block_cmd::CheckBlockCmd, + build_spec_cmd::BuildSpecCmd, chain_info_cmd::ChainInfoCmd, check_block_cmd::CheckBlockCmd, export_blocks_cmd::ExportBlocksCmd, export_state_cmd::ExportStateCmd, generate::GenerateCmd, generate_node_key::GenerateNodeKeyCmd, import_blocks_cmd::ImportBlocksCmd, insert_key::InsertKeyCmd, inspect_key::InspectKeyCmd, inspect_node_key::InspectNodeKeyCmd, From f13751c560e84e515119b4de1720759fdb348654 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Mon, 25 Apr 2022 13:47:33 +0200 Subject: [PATCH 11/12] Copyright year MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/cli/src/commands/chain_info_cmd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/commands/chain_info_cmd.rs b/client/cli/src/commands/chain_info_cmd.rs index d50ef88d02c56..10eb3d658c101 100644 --- a/client/cli/src/commands/chain_info_cmd.rs +++ b/client/cli/src/commands/chain_info_cmd.rs @@ -1,6 +1,6 @@ // This file is part of Substrate. -// Copyright (C) 2018-2022 Parity Technologies (UK) Ltd. +// Copyright (C) 2022 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 // This program is free software: you can redistribute it and/or modify From 2d5a361dad531b5d71415ac0f99470abf57ab5c3 Mon Sep 17 00:00:00 2001 From: tgmichel Date: Mon, 25 Apr 2022 17:06:13 +0200 Subject: [PATCH 12/12] Expose `DatabaseParams` --- client/cli/src/commands/chain_info_cmd.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/client/cli/src/commands/chain_info_cmd.rs b/client/cli/src/commands/chain_info_cmd.rs index 10eb3d658c101..0e57d1677efbb 100644 --- a/client/cli/src/commands/chain_info_cmd.rs +++ b/client/cli/src/commands/chain_info_cmd.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{CliConfiguration, PruningParams, Result as CliResult, SharedParams}; +use crate::{CliConfiguration, DatabaseParams, PruningParams, Result as CliResult, SharedParams}; use parity_scale_codec::{Decode, Encode}; use sc_client_api::{backend::Backend as BackendT, blockchain::HeaderBackend}; use sp_blockchain::Info; @@ -33,6 +33,10 @@ pub struct ChainInfoCmd { #[allow(missing_docs)] #[clap(flatten)] pub shared_params: SharedParams, + + #[allow(missing_docs)] + #[clap(flatten)] + pub database_params: DatabaseParams, } /// Serializable `chain-info` subcommand output. @@ -92,4 +96,8 @@ impl CliConfiguration for ChainInfoCmd { fn pruning_params(&self) -> Option<&PruningParams> { Some(&self.pruning_params) } + + fn database_params(&self) -> Option<&DatabaseParams> { + Some(&self.database_params) + } }