Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] #1754: Add Kura inspector CLI #1817

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ members = [
"schema/derive",
"substrate",
"telemetry",
"tools/kura_inspector",
"version",
"version/derive",
]
2 changes: 1 addition & 1 deletion core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ impl ValidBlock {
///
/// # Panics
/// If generating keys or block signing fails.
#[cfg(test)]
s8sato marked this conversation as resolved.
Show resolved Hide resolved
#[allow(clippy::restriction)]
#[cfg(test)]
pub fn new_dummy() -> Self {
Self {
header: BlockHeader {
Expand Down
18 changes: 18 additions & 0 deletions tools/kura_inspector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "kura_inspector"
version = "2.0.0-pre.1"
authors = ["Iroha 2 team <https://github.com/orgs/soramitsu/teams/iroha2>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iroha_core = { path = "../../core" }
iroha_config = { path = "../../config" }

clap = { version = "3.0", features = ["derive"] }
futures-util = "0.3"
tokio = { version = "1.6.0", features = ["rt"]}

[dev-dependencies]
tempfile = "3"
80 changes: 80 additions & 0 deletions tools/kura_inspector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! General objects independent from executables.

use std::path::Path;

use iroha_config::Configurable;
use iroha_core::kura;

pub mod print;

#[allow(missing_docs)]
#[derive(Clone, Copy)]
pub enum Config {
Print(print::Config),
}

/// Where to write the results of the inspection.
pub struct Output<T, E>
where
T: std::io::Write + Send,
E: std::io::Write + Send,
{
/// Writer for valid data
pub ok: T,
/// Writer for invalid data
pub err: E,
}

impl Config {
/// Configure [`kura::BlockStore`] and route to the subcommand.
///
/// # Errors
/// Fails if
/// 1. Fails to configure [`kura::BlockStore`].
/// 2. Fails to run the subcommand.
pub async fn run<T, E>(&self, output: &mut Output<T, E>) -> Result<(), Error>
where
T: std::io::Write + Send,
E: std::io::Write + Send,
{
let block_store = block_store().await?;
match self {
Self::Print(cfg) => cfg.run(&block_store, output).await.map_err(Error::Print)?,
}
Ok(())
}
}

async fn block_store() -> Result<kura::BlockStore<kura::DefaultIO>, Error> {
let mut kura_config = kura::config::KuraConfiguration::default();
kura_config.load_environment().map_err(Error::KuraConfig)?;
kura::BlockStore::new(
Path::new(&kura_config.block_store_path),
kura_config.blocks_per_storage_file,
kura::DefaultIO,
)
.await
.map_err(Error::SetBlockStore)
}

/// [`Output`] for CLI use.
pub type DefaultOutput = Output<std::io::Stdout, std::io::Stderr>;

impl DefaultOutput {
/// Construct [`DefaultOutput`].
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
ok: std::io::stdout(),
err: std::io::stderr(),
}
}
}

#[derive(Debug)]
#[allow(missing_docs)]
pub enum Error {
KuraConfig(iroha_config::derive::Error),
SetBlockStore(kura::Error),
Print(print::Error),
}
46 changes: 46 additions & 0 deletions tools/kura_inspector/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use clap::{Parser, Subcommand};
use kura_inspector::{print, Config, DefaultOutput};

/// Kura inspector
#[derive(Parser)]
#[clap(version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"))]
struct Opts {
/// Height of the block from which start the inspection.
/// Defaults to the latest block height
#[clap(short, long, name = "BLOCK_HEIGHT")]
from: Option<usize>,
#[clap(subcommand)]
command: Command,
}

#[derive(Subcommand)]
enum Command {
/// Print contents of a certain length of the blocks
Print {
/// Number of the blocks to print.
/// The excess will be truncated
#[clap(short = 'n', long, default_value_t = 1)]
length: usize,
},
}

#[tokio::main]
#[allow(clippy::use_debug, clippy::print_stderr)]
async fn main() {
let opts = Opts::parse();
let mut output = DefaultOutput::new();
Config::from(opts)
.run(&mut output)
.await
.unwrap_or_else(|e| eprintln!("{:?}", e))
}

impl From<Opts> for Config {
fn from(src: Opts) -> Self {
let Opts { from, command } = src;

match command {
Command::Print { length } => Config::Print(print::Config { from, length }),
}
}
}
Loading