Skip to content

Commit

Permalink
devtools: Add block hash lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 24, 2024
1 parent bc08f0c commit 9e20253
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions devtools/src/bin/inspect/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ fn inspect_header_inner(header: &BlockHeader, params: Option<Network>) {
}
}

/// Used when a block hash is resolved via lightwalletd.
pub(crate) fn inspect_block_hash(block: &CompactBlock, network: &'static str) {
eprintln!("Zcash block hash");
eprintln!(" - Network: {}", network);
eprintln!(" - Height: {}", block.height());
}

pub(crate) fn inspect(block: &Block, context: Option<Context>) {
eprintln!("Zcash block");
let params = block
Expand Down
17 changes: 15 additions & 2 deletions devtools/src/bin/inspect/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use tonic::transport::{Channel, ClientTlsConfig};
use zcash_client_backend::proto::service::{
compact_tx_streamer_client::CompactTxStreamerClient, TxFilter,
use zcash_client_backend::proto::{
compact_formats::CompactBlock,
service::{compact_tx_streamer_client::CompactTxStreamerClient, BlockId, TxFilter},
};
use zcash_primitives::transaction::Transaction;
use zcash_protocol::consensus::{BlockHeight, BranchId, Network};
Expand Down Expand Up @@ -58,6 +59,18 @@ impl Lightwalletd {
})
}

pub(crate) async fn lookup_block_hash(&mut self, candidate: [u8; 32]) -> Option<CompactBlock> {
let request = BlockId {
hash: candidate.into(),
..Default::default()
};
self.inner
.get_block(request)
.await
.ok()
.map(|b| b.into_inner())
}

pub(crate) async fn lookup_txid(
&mut self,
candidate: [u8; 32],
Expand Down
14 changes: 13 additions & 1 deletion devtools/src/bin/inspect/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn inspect_bytes(bytes: Vec<u8>, context: Option<Context>, lookup: bool) {
}

fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool) {
let maybe_mainnet_block_hash = bytes.iter().take(4).all(|c| c == &0);
let mut maybe_mainnet_block_hash = bytes.iter().take(4).all(|c| c == &0);

if lookup {
// Block hashes and txids are byte-reversed; we didn't do this when parsing the
Expand All @@ -134,6 +134,13 @@ fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool
match lookup::Lightwalletd::mainnet().await {
Err(e) => eprintln!("Error: Failed to connect to mainnet lightwalletd: {:?}", e),
Ok(mut mainnet) => {
if let Some(block) = mainnet.lookup_block_hash(candidate).await {
block::inspect_block_hash(&block, "main");
return true;
} else {
maybe_mainnet_block_hash = false;
}

if let Some((tx, mined_height)) = mainnet.lookup_txid(candidate).await {
transaction::inspect(tx, context, mined_height);
return true;
Expand All @@ -144,6 +151,11 @@ fn inspect_possible_hash(bytes: [u8; 32], context: Option<Context>, lookup: bool
match lookup::Lightwalletd::testnet().await {
Err(e) => eprintln!("Error: Failed to connect to testnet lightwalletd: {:?}", e),
Ok(mut testnet) => {
if let Some(block) = testnet.lookup_block_hash(candidate).await {
block::inspect_block_hash(&block, "main");
return true;
}

if let Some((tx, mined_height)) = testnet.lookup_txid(candidate).await {
transaction::inspect(tx, context, mined_height);
return true;
Expand Down

0 comments on commit 9e20253

Please sign in to comment.