Skip to content

Commit

Permalink
Simplify various type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed Dec 14, 2023
1 parent 9e16ad5 commit c0c2101
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
7 changes: 7 additions & 0 deletions zebra-chain/src/block/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::ops::{Add, Sub};
use thiserror::Error;
use zcash_primitives::consensus::BlockHeight;

use crate::{serialization::SerializationError, BoxError};

Expand Down Expand Up @@ -105,6 +106,12 @@ impl Height {
}
}

impl From<Height> for BlockHeight {
fn from(height: Height) -> Self {
BlockHeight::from_u32(height.0)
}
}

/// A difference between two [`Height`]s, possibly negative.
///
/// This can represent the difference between any height values,
Expand Down
9 changes: 9 additions & 0 deletions zebra-chain/src/primitives/zcash_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,12 @@ impl From<Network> for zcash_primitives::consensus::Network {
}
}
}

impl From<zcash_primitives::consensus::Network> for Network {
fn from(network: zcash_primitives::consensus::Network) -> Self {
match network {
zcash_primitives::consensus::Network::MainNetwork => Network::Mainnet,
zcash_primitives::consensus::Network::TestNetwork => Network::Testnet,
}
}
}
27 changes: 7 additions & 20 deletions zebra-utils/src/bin/scanning-results-reader/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::collections::HashMap;

use hex::ToHex;
use itertools::Itertools;
use jsonrpc::simple_http::SimpleHttpTransport;
use jsonrpc::Client;
Expand Down Expand Up @@ -42,12 +43,12 @@ use zebra_scan::{storage::Storage, Config};
#[allow(clippy::print_stdout)]
pub fn main() {
let network = zcash_primitives::consensus::Network::MainNetwork;
let storage = Storage::new(&Config::default(), zebra_network(&network), true);
let storage = Storage::new(&Config::default(), network.into(), true);
// If the first memo is empty, it doesn't get printed. But we never print empty memos anyway.
let mut prev_memo = "".to_owned();

for (key, _) in storage.sapling_keys_last_heights().iter() {
let dfvk = sapling_key_to_scan_block_keys(key, zebra_network(&network))
let dfvk = sapling_key_to_scan_block_keys(key, network.into())
.expect("Scanning key from the storage should be valid")
.0
.into_iter()
Expand All @@ -60,11 +61,11 @@ pub fn main() {
)]);

for (height, txids) in storage.sapling_results(key) {
let height = BlockHeight::from_u32(height.0);
let height = BlockHeight::from(height);

for txid in txids.iter() {
let tx = Transaction::read(
&hex::decode(&get_tx_via_rpc(hex::encode(txid.bytes_in_display_order())))
&hex::decode(&get_tx_via_rpc(txid.encode_hex()))
.expect("RPC response should be decodable from hex string to bytes")[..],
BranchId::for_height(&network, height),
)
Expand All @@ -88,10 +89,10 @@ pub fn main() {
}
}

/// Trims trailing zeroes from a memo, and returns the memo as a string.
/// Trims trailing zeroes from a memo, and returns the memo as a [`String`].
fn memo_bytes_to_string(memo: &[u8; 512]) -> String {
match memo.iter().rposition(|&byte| byte != 0) {
Some(i) => std::str::from_utf8(&memo[..=i]).unwrap_or("").to_owned(),
Some(i) => String::from_utf8_lossy(&memo[..=i]).into_owned(),
None => "".to_owned(),
}
}
Expand All @@ -115,17 +116,3 @@ fn get_tx_via_rpc(txid: String) -> String {
.result()
.expect("Zebra's RPC response should contain a valid result")
}

/// Converts [`zcash_primitives::consensus::Network`] to [`zebra_chain::parameters::Network`].
fn zebra_network(
network: &zcash_primitives::consensus::Network,
) -> zebra_chain::parameters::Network {
match network {
zcash_primitives::consensus::Network::MainNetwork => {
zebra_chain::parameters::Network::Mainnet
}
zcash_primitives::consensus::Network::TestNetwork => {
zebra_chain::parameters::Network::Testnet
}
}
}

0 comments on commit c0c2101

Please sign in to comment.