moves conversions to zcash_primitives::consensus::Network to where they're used.

This commit is contained in:
Arya 2024-04-01 21:27:58 -04:00
parent 574b1f9487
commit 05e41a2803
7 changed files with 39 additions and 81 deletions

View File

@ -12,7 +12,6 @@
//! Typically, consensus parameters are accessed via a function that takes a
//! `Network` and `block::Height`.
mod error;
mod genesis;
mod network;
mod network_upgrade;
@ -21,7 +20,6 @@ mod transaction;
#[cfg(any(test, feature = "proptest-impl"))]
pub mod arbitrary;
pub use error::*;
pub use genesis::*;
pub use network::{Network, NetworkKind, NetworkParameters};
pub use network_upgrade::*;

View File

@ -1,8 +0,0 @@
//! Error types for zebra-chain parameters
use thiserror::Error;
/// An error indicating that Zebra network is not supported in type conversions.
#[derive(Clone, Debug, Error)]
#[error("Unsupported Zcash network parameters: {0}")]
pub struct UnsupportedNetwork(pub String);

View File

@ -5,10 +5,7 @@
use zcash_address::unified::{self, Container};
use zcash_primitives::sapling;
use crate::{
parameters::{Network, NetworkKind, UnsupportedNetwork},
transparent, BoxError,
};
use crate::{parameters::NetworkKind, transparent, BoxError};
/// Zcash address variants
pub enum Address {
@ -43,39 +40,6 @@ pub enum Address {
},
}
impl TryFrom<zcash_address::Network> for Network {
// TODO: better error type
type Error = BoxError;
fn try_from(network: zcash_address::Network) -> Result<Self, Self::Error> {
match network {
zcash_address::Network::Main => Ok(Network::Mainnet),
zcash_address::Network::Test => Ok(Network::new_default_testnet()),
// TODO: Add conversion to regtest (#7839)
zcash_address::Network::Regtest => Err("unsupported Zcash network parameters".into()),
}
}
}
impl TryFrom<&Network> for zcash_address::Network {
type Error = UnsupportedNetwork;
fn try_from(network: &Network) -> Result<Self, Self::Error> {
match network {
Network::Mainnet => Ok(zcash_address::Network::Main),
Network::Testnet(_params) if network.is_default_testnet() => {
Ok(zcash_address::Network::Test)
}
// TODO: If the network parameters match `Regtest` parameters, convert to
// `zcash_address::Network::Regtest instead of returning `UnsupportedAddress` error.
// (#7119, #7839)
Network::Testnet(_params) => Err(UnsupportedNetwork(
"could not convert configured testnet to zcash_address::Network".to_string(),
)),
}
}
}
impl zcash_address::TryFromAddress for Address {
// TODO: crate::serialization::SerializationError
type Error = BoxError;

View File

@ -25,10 +25,24 @@ pub fn decrypts_successfully(transaction: &Transaction, network: &Network, heigh
if let Some(bundle) = alt_tx.sapling_bundle() {
for output in bundle.shielded_outputs().iter() {
let network = match network {
Network::Mainnet => zcash_primitives::consensus::Network::MainNetwork,
Network::Testnet(params) => {
// # Correctness:
//
// There are differences between the `TestNetwork` parameters and those returned by
// `CRegTestParams()` in zcashd, so this function can't return `TestNetwork` unless
// Zebra is using the default public Testnet.
//
// TODO: Remove this conversion by implementing `zcash_primitives::consensus::Parameters`
// for `Network` (#8365).
assert!(params.is_default_testnet(), "could not convert configured testnet to zcash_primitives::consensus::Network");
zcash_primitives::consensus::Network::TestNetwork
}
};
let recovery = zcash_primitives::sapling::note_encryption::try_sapling_output_recovery(
// TODO: Implement `Parameters` trait for `Network` and pass network here without conversion (#8365)
&<zcash_primitives::consensus::Network>::try_from(network)
.expect("network must match zcash_primitives network"),
&network,
alt_height,
&null_sapling_ovk,
output,

View File

@ -7,7 +7,7 @@ use zcash_primitives::transaction as zp_tx;
use crate::{
amount::{Amount, NonNegative},
parameters::{Network, NetworkUpgrade, UnsupportedNetwork},
parameters::{Network, NetworkUpgrade},
serialization::ZcashSerialize,
transaction::{AuthDigest, HashType, SigHash, Transaction},
transparent::{self, Script},
@ -336,28 +336,3 @@ pub(crate) fn transparent_output_address(
None => None,
}
}
impl TryFrom<&Network> for zcash_primitives::consensus::Network {
type Error = UnsupportedNetwork;
fn try_from(network: &Network) -> Result<Self, Self::Error> {
match network {
Network::Mainnet => Ok(zcash_primitives::consensus::Network::MainNetwork),
// # Correctness:
//
// There are differences between the `TestNetwork` parameters and those returned by
// `CRegTestParams()` in zcashd, so this function can't return `TestNetwork` unless
// Zebra is using the default public Testnet.
//
// TODO: Remove this conversion by implementing `zcash_primitives::consensus::Parameters`
// for `Network` (#8365).
Network::Testnet(_params) if network.is_default_testnet() => {
Ok(zcash_primitives::consensus::Network::TestNetwork)
}
Network::Testnet(_params) => Err(UnsupportedNetwork(
"could not convert configured testnet to zcash_primitives::consensus::Network"
.to_string(),
)),
}
}
}

View File

@ -384,10 +384,24 @@ pub fn scan_block<K: ScanningKey>(
// TODO: Implement a check that returns early when the block height is below the Sapling
// activation height.
// TODO: Implement `zcash_primitives::consensus::Parameters` for `Network` and remove this conversion (#8365)
let network: zcash_primitives::consensus::Network = network
.try_into()
.expect("must match zcash_primitives network");
let network = match network {
Network::Mainnet => zcash_primitives::consensus::Network::MainNetwork,
Network::Testnet(params) => {
// # Correctness:
//
// There are differences between the `TestNetwork` parameters and those returned by
// `CRegTestParams()` in zcashd, so this function can't return `TestNetwork` unless
// Zebra is using the default public Testnet.
//
// TODO: Remove this conversion by implementing `zcash_primitives::consensus::Parameters`
// for `Network` (#8365).
assert!(
params.is_default_testnet(),
"could not convert configured testnet to zcash_primitives::consensus::Network"
);
zcash_primitives::consensus::Network::TestNetwork
}
};
let chain_metadata = ChainMetadata {
sapling_commitment_tree_size: sapling_tree_size,

View File

@ -41,8 +41,9 @@ use zebra_scan::{storage::Storage, Config};
/// - The transaction fetched via RPC cannot be deserialized from raw bytes.
#[allow(clippy::print_stdout)]
pub fn main() {
// TODO: Implement `zcash_primitives::consensus::Parameters` for `Network` and remove this variable (#8365).
let network = zcash_primitives::consensus::Network::MainNetwork;
let zebra_network: zebra_chain::parameters::Network = network.into();
let zebra_network = zebra_chain::parameters::Network::Mainnet;
let storage = Storage::new(&Config::default(), &zebra_network, 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();