Skip to content

Commit

Permalink
shim/cli: query block --json flag
Browse files Browse the repository at this point in the history
Which allows outputting block data as JSON.
  • Loading branch information
pepyakin authored and rphmeier committed Feb 23, 2024
1 parent 8ee7ea8 commit 50f8db0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions ikura/shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition.workspace = true

[dependencies]
ikura-nmt = { workspace = true, default-features = true, features = ["serde"] }
ikura-serde-util = { workspace = true }
ikura-subxt = { workspace = true }
ikura-primitives = { workspace = true, default-features = true }
ikura-shim-common-sovereign = { workspace = true, default-features = true, features = ["server"] }
Expand All @@ -24,6 +25,8 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "net"] }
async-trait = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
subxt = { workspace = true }
subxt-signer = { workspace = true, features = ["subxt"] }
sha2 = { workspace = true, default-features = true }
Expand Down
4 changes: 4 additions & 0 deletions ikura/shim/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ pub mod query {

#[clap(flatten)]
pub block: BlockParams,

/// Output the result as JSON.
#[clap(long)]
pub json: bool,
}
}

Expand Down
2 changes: 1 addition & 1 deletion ikura/shim/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn init_logging() -> anyhow::Result<()> {
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into())
.from_env_lossy();
tracing_subscriber::registry()
.with(fmt::layer())
.with(filter)
.with(fmt::layer().with_writer(std::io::stderr))
.try_init()?;
Ok(())
}
Expand Down
42 changes: 23 additions & 19 deletions ikura/shim/src/cmd/query/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ use super::{connect_rpc, get_block_at};
use crate::cli::query::block::Params;

pub async fn run(params: Params) -> anyhow::Result<()> {
let Params { rpc, block } = params;
let Params { rpc, block, json } = params;

let client = connect_rpc(rpc).await?;
let block = get_block_at(&client, block).await?;

println!("Block: #{}", block.number);
println!(" Hash: 0x{}", hex::encode(&block.hash[..]));
println!(" Parent Hash: 0x{}", hex::encode(&block.parent_hash[..]));
println!(" Blobs Root: 0x{}", hex::encode(&block.tree_root.root[..]));
println!(" Min Namespace: {}", block.tree_root.min_ns);
println!(" Max Namespace: {}", block.tree_root.max_ns);
println!(" Timestamp: {}", block.timestamp);
println!(
" Blob Count: {} ({} bytes)",
block.blobs.len(),
block.blobs.iter().map(|b| b.data.len()).sum::<usize>(),
);
for (i, blob) in block.blobs.into_iter().enumerate() {
println!(" Blob #{}", i);
println!(" Extrinsic Index: {}", blob.extrinsic_index);
println!(" Namespace: {}", &blob.namespace);
println!(" Size: {}", blob.data.len());
if json {
let json = serde_json::to_string_pretty(&block)?;
println!("{}", json);
} else {
println!("Block: #{}", block.number);
println!(" Hash: 0x{}", hex::encode(&block.hash[..]));
println!(" Parent Hash: 0x{}", hex::encode(&block.parent_hash[..]));
println!(" Blobs Root: 0x{}", hex::encode(&block.tree_root.root[..]));
println!(" Min Namespace: {}", block.tree_root.min_ns);
println!(" Max Namespace: {}", block.tree_root.max_ns);
println!(" Timestamp: {}", block.timestamp);
println!(
" Blob Count: {} ({} bytes)",
block.blobs.len(),
block.blobs.iter().map(|b| b.data.len()).sum::<usize>(),
);
for (i, blob) in block.blobs.into_iter().enumerate() {
println!(" Blob #{}", i);
println!(" Extrinsic Index: {}", blob.extrinsic_index);
println!(" Namespace: {}", &blob.namespace);
println!(" Size: {}", blob.data.len());
}
}

Ok(())
}
6 changes: 6 additions & 0 deletions ikura/shim/src/ikura_rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,12 @@ mod err {
}

/// Represents a ikura block.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Block {
pub number: u64,
#[serde(with = "ikura_serde_util::bytes32_hex")]
pub hash: [u8; 32],
#[serde(with = "ikura_serde_util::bytes32_hex")]
pub parent_hash: [u8; 32],
pub tree_root: ikura_nmt::TreeRoot,
pub timestamp: u64,
Expand Down Expand Up @@ -398,10 +401,13 @@ impl Block {
}

/// Represents a blob in a ikura block.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Blob {
pub extrinsic_index: u32,
pub namespace: Namespace,
#[serde(with = "ikura_serde_util::bytes32_hex")]
pub sender: [u8; 32],
#[serde(with = "ikura_serde_util::bytes_hex")]
pub data: Vec<u8>,
}

Expand Down

0 comments on commit 50f8db0

Please sign in to comment.