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 8640923 commit 1c71264
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
8 changes: 8 additions & 0 deletions devtools/src/bin/inspect/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::convert::{TryFrom, TryInto};
use std::io::{self, Read};

use sha2::{Digest, Sha256};
use zcash_client_backend::proto::compact_formats::CompactBlock;
use zcash_encoding::Vector;
use zcash_primitives::{
block::BlockHeader,
Expand Down Expand Up @@ -282,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());

Check warning on line 290 in devtools/src/bin/inspect/block.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/block.rs#L287-L290

Added lines #L287 - L290 were not covered by tests
}

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> {

Check warning on line 62 in devtools/src/bin/inspect/lookup.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/lookup.rs#L62

Added line #L62 was not covered by tests
let request = BlockId {
hash: candidate.into(),

Check warning on line 64 in devtools/src/bin/inspect/lookup.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/lookup.rs#L64

Added line #L64 was not covered by tests
..Default::default()
};
self.inner
.get_block(request)
.await

Check warning on line 69 in devtools/src/bin/inspect/lookup.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/lookup.rs#L67-L69

Added lines #L67 - L69 were not covered by tests
.ok()
.map(|b| b.into_inner())

Check warning on line 71 in devtools/src/bin/inspect/lookup.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/lookup.rs#L71

Added line #L71 was not covered by tests
}

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);

Check warning on line 124 in devtools/src/bin/inspect/main.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/main.rs#L124

Added line #L124 was not covered by tests

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;

Check warning on line 139 in devtools/src/bin/inspect/main.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/main.rs#L137-L139

Added lines #L137 - L139 were not covered by tests
} else {
maybe_mainnet_block_hash = false;

Check warning on line 141 in devtools/src/bin/inspect/main.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/main.rs#L141

Added line #L141 was not covered by tests
}

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;

Check warning on line 156 in devtools/src/bin/inspect/main.rs

View check run for this annotation

Codecov / codecov/patch

devtools/src/bin/inspect/main.rs#L154-L156

Added lines #L154 - L156 were not covered by tests
}

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 1c71264

Please sign in to comment.