Skip to content

Commit

Permalink
devtools: Add --lookup argument to zcash-inspect
Browse files Browse the repository at this point in the history
This queries `lightwalletd` for things that might be txids, and could be
extended to other queryable formats in future.
  • Loading branch information
str4d committed Jul 24, 2024
1 parent 104c0c2 commit 8640923
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 32 deletions.
88 changes: 79 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion devtools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ equihash.workspace = true
group.workspace = true
sha2.workspace = true
zcash_address.workspace = true
zcash_client_backend ={ workspace = true, features = ["lightwalletd-tonic-transport"] }
zcash_encoding.workspace = true
zcash_keys.workspace = true
zcash_note_encryption.workspace = true
zcash_primitives = { workspace = true, features = ["transparent-inputs"] }
zcash_proofs.workspace = true
zcash_proofs = { workspace = true, features = ["directories"] }
zcash_protocol.workspace = true

# Transparent
Expand All @@ -39,11 +40,14 @@ sapling.workspace = true
orchard.workspace = true

# zcash-inspect tool
anyhow = "1"
hex.workspace = true
lazy_static.workspace = true
secrecy.workspace = true
serde.workspace = true
serde_json.workspace = true
tonic = { workspace = true, features = ["gzip", "tls-webpki-roots"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
uint = "0.9"

[[bin]]
Expand Down
85 changes: 85 additions & 0 deletions devtools/src/bin/inspect/lookup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use tonic::transport::{Channel, ClientTlsConfig};
use zcash_client_backend::proto::service::{
compact_tx_streamer_client::CompactTxStreamerClient, TxFilter,
};
use zcash_primitives::transaction::Transaction;
use zcash_protocol::consensus::{BlockHeight, BranchId, Network};

const MAINNET: Server = Server {
host: "zec.rocks",
port: 443,
};

const TESTNET: Server = Server {
host: "testnet.zec.rocks",
port: 443,
};

struct Server {
host: &'static str,
port: u16,
}

impl Server {
fn endpoint(&self) -> String {
format!("https://{}:{}", self.host, self.port)
}
}

async fn connect(server: &Server) -> anyhow::Result<CompactTxStreamerClient<Channel>> {
let channel = Channel::from_shared(server.endpoint())?;

let tls = ClientTlsConfig::new()
.domain_name(server.host.to_string())
.with_webpki_roots();
let channel = channel.tls_config(tls)?;

Ok(CompactTxStreamerClient::new(channel.connect().await?))
}

#[derive(Debug)]
pub(crate) struct Lightwalletd {
inner: CompactTxStreamerClient<Channel>,
parameters: Network,
}

impl Lightwalletd {
pub(crate) async fn mainnet() -> anyhow::Result<Self> {
Ok(Self {
inner: connect(&MAINNET).await?,
parameters: Network::MainNetwork,
})
}

pub(crate) async fn testnet() -> anyhow::Result<Self> {
Ok(Self {
inner: connect(&TESTNET).await?,
parameters: Network::TestNetwork,
})
}

pub(crate) async fn lookup_txid(
&mut self,
candidate: [u8; 32],
) -> Option<(Transaction, Option<BlockHeight>)> {
let request = TxFilter {
hash: candidate.into(),
..Default::default()
};
let response = self.inner.get_transaction(request).await.ok()?.into_inner();

// `RawTransaction.height` has type u64 in the protobuf format, but is documented
// as using -1 for the "not mined" sentinel. Given that we only support u32 block
// heights, -1 in two's complement will fall outside that range.
let mined_height = response.height.try_into().ok();

Transaction::read(
&response.data[..],
mined_height
.map(|height| BranchId::for_height(&self.parameters, height))
.unwrap_or(BranchId::Nu5),
)
.ok()
.map(|tx| (tx, mined_height))
}
}
Loading

0 comments on commit 8640923

Please sign in to comment.