Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devtools: Add block hash lookups #1457

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::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 @@
}
}

/// 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 @@
})
}

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_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 @@
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 @@
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
Loading