From e4276d802214f88eb298a85a68e104814815c9c8 Mon Sep 17 00:00:00 2001 From: danda Date: Mon, 6 May 2024 19:55:49 -0700 Subject: [PATCH] chore: use block_selector in neptune-cli header 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 --- src/bin/neptune-cli.rs | 11 +++++------ src/models/blockchain/block/block_header.rs | 4 ++-- src/models/blockchain/block/block_info.rs | 10 ++++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/bin/neptune-cli.rs b/src/bin/neptune-cli.rs index 036a5306..6a8afdcc 100644 --- a/src/bin/neptune-cli.rs +++ b/src/bin/neptune-cli.rs @@ -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}; @@ -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 { @@ -33,7 +31,7 @@ enum Command { OwnInstanceId, BlockHeight, BlockInfo { - /// one of: genesis, tip, height/N, digest/hex + /// one of: genesis, tip, height/, digest/ block_selector: BlockSelector, }, Confirmations, @@ -45,7 +43,8 @@ enum Command { }, TipHeader, Header { - hash: Digest, + /// one of: genesis, tip, height/, digest/ + block_selector: BlockSelector, }, SyncedBalance, WalletStatus, @@ -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 { diff --git a/src/models/blockchain/block/block_header.rs b/src/models/blockchain/block/block_header.rs index ad4050ed..cb71ed37 100644 --- a/src/models/blockchain/block/block_header.rs +++ b/src/models/blockchain/block/block_header.rs @@ -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 ); diff --git a/src/models/blockchain/block/block_info.rs b/src/models/blockchain/block/block_info.rs index 0b699eea..cbb4fdbc 100644 --- a/src/models/blockchain/block/block_info.rs +++ b/src/models/blockchain/block/block_info.rs @@ -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; @@ -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, + pub proof_of_work_family: U32s, pub difficulty: U32s, pub num_inputs: usize, pub num_outputs: usize, @@ -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) @@ -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(),