Skip to content

Commit

Permalink
chore: use block_selector in neptune-cli header
Browse files Browse the repository at this point in the history
THis updates `neptune-cli header` to accept a block_selector instead
of a hash, just like the `block-info` command.

Also:
 * BlockHeader Display prints timestamp with standard_format()
 * BlockHeader Display prints prev_block_digest as hex
 * Adds fields to BlockInfo:
   * prev_block_digest
   * proof_of_work_line
   * proof_of_work_family
  • Loading branch information
dan-da committed May 7, 2024
1 parent 3e21a69 commit e4276d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/bin/neptune-cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use neptune_core::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
use neptune_core::models::state::wallet::coin_with_possible_timelock::CoinWithPossibleTimeLock;
use neptune_core::prelude::twenty_first;

use anyhow::{bail, Result};
use clap::{CommandFactory, Parser};
Expand All @@ -20,7 +19,6 @@ use neptune_core::models::blockchain::block::block_selector::BlockSelector;
use neptune_core::models::state::wallet::wallet_status::WalletStatus;
use neptune_core::rpc_server::RPCClient;
use std::io::stdout;
use twenty_first::math::digest::Digest;

#[derive(Debug, Parser)]
enum Command {
Expand All @@ -33,7 +31,7 @@ enum Command {
OwnInstanceId,
BlockHeight,
BlockInfo {
/// one of: genesis, tip, height/N, digest/hex
/// one of: genesis, tip, height/<n>, digest/<hex>
block_selector: BlockSelector,
},
Confirmations,
Expand All @@ -45,7 +43,8 @@ enum Command {
},
TipHeader,
Header {
hash: Digest,
/// one of: genesis, tip, height/<n>, digest/<hex>
block_selector: BlockSelector,
},
SyncedBalance,
WalletStatus,
Expand Down Expand Up @@ -345,8 +344,8 @@ async fn main() -> Result<()> {
.expect("Tip header should be found");
println!("{val}")
}
Command::Header { hash } => {
let res = client.header(ctx, BlockSelector::Digest(hash)).await?;
Command::Header { block_selector } => {
let res = client.header(ctx, block_selector).await?;
if res.is_none() {
println!("Block did not exist in database.");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/models/blockchain/block/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl Display for BlockHeader {
Proof-of-work-line: {}\n\
Proof-of-work-family: {}",
self.height,
self.timestamp,
self.prev_block_digest,
self.timestamp.standard_format(),
self.prev_block_digest.to_hex(),
self.proof_of_work_line,
self.proof_of_work_family
);
Expand Down
10 changes: 10 additions & 0 deletions src/models/blockchain/block/block_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! BlockInfo is a concise summary of a block intended for human
//! consumption/reporting in block explorers, cli, dashboard, etc.

use super::block_header::PROOF_OF_WORK_COUNT_U32_SIZE;
use super::block_header::TARGET_DIFFICULTY_U32_SIZE;
use crate::models::blockchain::block::block_height::BlockHeight;
use crate::models::blockchain::block::Block;
Expand All @@ -16,7 +17,10 @@ use twenty_first::prelude::U32s;
pub struct BlockInfo {
pub height: BlockHeight,
pub digest: Digest,
pub prev_block_digest: Digest,
pub timestamp: Timestamp,
pub proof_of_work_line: U32s<PROOF_OF_WORK_COUNT_U32_SIZE>,
pub proof_of_work_family: U32s<PROOF_OF_WORK_COUNT_U32_SIZE>,
pub difficulty: U32s<TARGET_DIFFICULTY_U32_SIZE>,
pub num_inputs: usize,
pub num_outputs: usize,
Expand All @@ -33,7 +37,10 @@ impl std::fmt::Display for BlockInfo {
let buf = String::new()
+ &format!("height: {}\n", self.height)
+ &format!("digest: {}\n", self.digest.to_hex())
+ &format!("prev_block_digest: {}\n", self.prev_block_digest.to_hex())
+ &format!("timestamp: {}\n", self.timestamp.standard_format())
+ &format!("proof_of_work_line: {}\n", self.proof_of_work_line)
+ &format!("proof_of_work_family: {}\n", self.proof_of_work_family)
+ &format!("difficulty: {}\n", self.difficulty)
+ &format!("num_inputs: {}\n", self.num_inputs)
+ &format!("num_outputs: {}\n", self.num_outputs)
Expand All @@ -58,9 +65,12 @@ impl BlockInfo {
let digest = block.hash();
Self {
digest,
prev_block_digest: header.prev_block_digest,
height: header.height,
timestamp: header.timestamp,
difficulty: header.difficulty,
proof_of_work_line: header.proof_of_work_line,
proof_of_work_family: header.proof_of_work_family,
num_inputs: body.transaction.kernel.inputs.len(),
num_outputs: body.transaction.kernel.outputs.len(),
num_uncle_blocks: body.uncle_blocks.len(),
Expand Down

0 comments on commit e4276d8

Please sign in to comment.