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

add(chain): Impl zcash_protocol::consensus::Parameters for Network #8425

Merged
merged 12 commits into from
May 2, 2024
Merged
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5898,6 +5898,7 @@ dependencies = [
"zcash_history",
"zcash_note_encryption",
"zcash_primitives",
"zcash_protocol",
"zebra-test",
]

Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ zcash_encoding = "0.2.0"
zcash_history = "0.4.0"
zcash_note_encryption = "0.4.0"
zcash_primitives = { version = "0.13.0", features = ["transparent-inputs"] }
zcash_protocol = { version = "0.1.1" }
zcash_address = { version = "0.3.2" }

# Time
chrono = { version = "0.4.38", default-features = false, features = ["clock", "std", "serde"] }
Expand Down Expand Up @@ -131,8 +133,6 @@ serde_json = { version = "1.0.116", optional = true }
# Production feature async-error and testing feature proptest-impl
tokio = { version = "1.37.0", optional = true }

zcash_address = { version = "0.3.2" }

# Experimental feature shielded-scan
zcash_client_backend = { version = "0.10.0-rc.1", optional = true }

Expand Down
58 changes: 35 additions & 23 deletions zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::{fmt, str::FromStr, sync::Arc};

use thiserror::Error;

use zcash_primitives::{consensus as zp_consensus, constants as zp_constants};

use crate::{
block::{self, Height, HeightDiff},
parameters::NetworkUpgrade,
Expand Down Expand Up @@ -96,17 +94,21 @@ impl NetworkKind {
/// pay-to-public-key-hash payment addresses for the network.
pub fn b58_pubkey_address_prefix(self) -> [u8; 2] {
match self {
Self::Mainnet => zp_constants::mainnet::B58_PUBKEY_ADDRESS_PREFIX,
Self::Testnet | Self::Regtest => zp_constants::testnet::B58_PUBKEY_ADDRESS_PREFIX,
Self::Mainnet => zcash_primitives::constants::mainnet::B58_PUBKEY_ADDRESS_PREFIX,
Self::Testnet | Self::Regtest => {
zcash_primitives::constants::testnet::B58_PUBKEY_ADDRESS_PREFIX
}
}
}

/// Returns the human-readable prefix for Base58Check-encoded transparent pay-to-script-hash
/// payment addresses for the network.
pub fn b58_script_address_prefix(self) -> [u8; 2] {
match self {
Self::Mainnet => zp_constants::mainnet::B58_SCRIPT_ADDRESS_PREFIX,
Self::Testnet | Self::Regtest => zp_constants::testnet::B58_SCRIPT_ADDRESS_PREFIX,
Self::Mainnet => zcash_primitives::constants::mainnet::B58_SCRIPT_ADDRESS_PREFIX,
Self::Testnet | Self::Regtest => {
zcash_primitives::constants::testnet::B58_SCRIPT_ADDRESS_PREFIX
}
}
}

Expand Down Expand Up @@ -296,32 +298,23 @@ impl FromStr for Network {
#[error("Invalid network: {0}")]
pub struct InvalidNetworkError(String);

impl zp_consensus::Parameters for Network {
impl zcash_primitives::consensus::Parameters for Network {
fn activation_height(
&self,
nu: zcash_primitives::consensus::NetworkUpgrade,
) -> Option<zcash_primitives::consensus::BlockHeight> {
let target_nu = match nu {
zp_consensus::NetworkUpgrade::Overwinter => NetworkUpgrade::Overwinter,
zp_consensus::NetworkUpgrade::Sapling => NetworkUpgrade::Sapling,
zp_consensus::NetworkUpgrade::Blossom => NetworkUpgrade::Blossom,
zp_consensus::NetworkUpgrade::Heartwood => NetworkUpgrade::Heartwood,
zp_consensus::NetworkUpgrade::Canopy => NetworkUpgrade::Canopy,
zp_consensus::NetworkUpgrade::Nu5 => NetworkUpgrade::Nu5,
};

// Heights are hard-coded below Height::MAX or checked when the config is parsed.
target_nu
NetworkUpgrade::from(nu)
.activation_height(self)
.map(|Height(h)| zp_consensus::BlockHeight::from_u32(h))
.map(|Height(h)| zcash_primitives::consensus::BlockHeight::from_u32(h))
}

fn coin_type(&self) -> u32 {
match self {
Network::Mainnet => zp_constants::mainnet::COIN_TYPE,
Network::Mainnet => zcash_primitives::constants::mainnet::COIN_TYPE,
// The regtest cointype reuses the testnet cointype,
// See <https://github.com/satoshilabs/slips/blob/master/slip-0044.md>
Network::Testnet(_) => zp_constants::testnet::COIN_TYPE,
Network::Testnet(_) => zcash_primitives::constants::testnet::COIN_TYPE,
}
}

Expand All @@ -335,21 +328,25 @@ impl zp_consensus::Parameters for Network {

fn hrp_sapling_extended_spending_key(&self) -> &str {
match self {
Network::Mainnet => zp_constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
Network::Mainnet => {
zcash_primitives::constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY
}
Network::Testnet(params) => params.hrp_sapling_extended_spending_key(),
}
}

fn hrp_sapling_extended_full_viewing_key(&self) -> &str {
match self {
Network::Mainnet => zp_constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
Network::Mainnet => {
zcash_primitives::constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY
}
Network::Testnet(params) => params.hrp_sapling_extended_full_viewing_key(),
}
}

fn hrp_sapling_payment_address(&self) -> &str {
match self {
Network::Mainnet => zp_constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
Network::Mainnet => zcash_primitives::constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
arya2 marked this conversation as resolved.
Show resolved Hide resolved
Network::Testnet(params) => params.hrp_sapling_payment_address(),
}
}
Expand All @@ -362,3 +359,18 @@ impl zp_consensus::Parameters for Network {
self.kind().b58_script_address_prefix()
}
}

impl zcash_protocol::consensus::Parameters for Network {
fn network_type(&self) -> zcash_address::Network {
self.kind().into()
}

fn activation_height(
&self,
nu: zcash_protocol::consensus::NetworkUpgrade,
) -> Option<zcash_protocol::consensus::BlockHeight> {
NetworkUpgrade::from(nu)
.activation_height(self)
.map(|Height(h)| zcash_protocol::consensus::BlockHeight::from_u32(h))
}
}
26 changes: 26 additions & 0 deletions zebra-chain/src/parameters/network_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/// Network upgrades can change the Zcash network protocol or consensus rules in
/// incompatible ways.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]

Check warning on line 34 in zebra-chain/src/parameters/network_upgrade.rs

View workflow job for this annotation

GitHub Actions / Build and Deploy Zebra Internal Docs

non-local `impl` definition, they should be avoided as they go against expectation

Check warning on line 34 in zebra-chain/src/parameters/network_upgrade.rs

View workflow job for this annotation

GitHub Actions / Test beta on ubuntu-latest

non-local `impl` definition, they should be avoided as they go against expectation

Check warning on line 34 in zebra-chain/src/parameters/network_upgrade.rs

View workflow job for this annotation

GitHub Actions / Test beta on windows-latest

non-local `impl` definition, they should be avoided as they go against expectation
pub enum NetworkUpgrade {
/// The Zcash protocol for a Genesis block.
///
Expand Down Expand Up @@ -493,6 +493,32 @@
}
}

impl From<zcash_protocol::consensus::NetworkUpgrade> for NetworkUpgrade {
fn from(nu: zcash_protocol::consensus::NetworkUpgrade) -> Self {
match nu {
zcash_protocol::consensus::NetworkUpgrade::Overwinter => Self::Overwinter,
zcash_protocol::consensus::NetworkUpgrade::Sapling => Self::Sapling,
zcash_protocol::consensus::NetworkUpgrade::Blossom => Self::Blossom,
zcash_protocol::consensus::NetworkUpgrade::Heartwood => Self::Heartwood,
zcash_protocol::consensus::NetworkUpgrade::Canopy => Self::Canopy,
zcash_protocol::consensus::NetworkUpgrade::Nu5 => Self::Nu5,
}
}
}

impl From<zcash_primitives::consensus::NetworkUpgrade> for NetworkUpgrade {
fn from(value: zcash_primitives::consensus::NetworkUpgrade) -> Self {
match value {
zcash_primitives::consensus::NetworkUpgrade::Overwinter => NetworkUpgrade::Overwinter,
zcash_primitives::consensus::NetworkUpgrade::Sapling => NetworkUpgrade::Sapling,
zcash_primitives::consensus::NetworkUpgrade::Blossom => NetworkUpgrade::Blossom,
zcash_primitives::consensus::NetworkUpgrade::Heartwood => NetworkUpgrade::Heartwood,
zcash_primitives::consensus::NetworkUpgrade::Canopy => NetworkUpgrade::Canopy,
zcash_primitives::consensus::NetworkUpgrade::Nu5 => NetworkUpgrade::Nu5,
}
}
}
upbqdn marked this conversation as resolved.
Show resolved Hide resolved

impl ConsensusBranchId {
/// The value used by `zcashd` RPCs for missing consensus branch IDs.
///
Expand Down
2 changes: 0 additions & 2 deletions zebra-chain/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

mod proofs;

#[cfg(feature = "getblocktemplate-rpcs")]
mod address;

#[cfg(feature = "getblocktemplate-rpcs")]
pub use address::Address;

#[cfg(feature = "shielded-scan")]
Expand Down
32 changes: 17 additions & 15 deletions zebra-chain/src/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl zcash_address::TryFromAddress for Address {
data: [u8; 20],
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
Ok(Self::Transparent(transparent::Address::from_pub_key_hash(
NetworkKind::from_zcash_address(network),
network.into(),
data,
)))
}
Expand All @@ -59,7 +59,7 @@ impl zcash_address::TryFromAddress for Address {
data: [u8; 20],
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
Ok(Self::Transparent(transparent::Address::from_script_hash(
NetworkKind::from_zcash_address(network),
network.into(),
data,
)))
}
Expand All @@ -68,7 +68,7 @@ impl zcash_address::TryFromAddress for Address {
network: zcash_address::Network,
data: [u8; 43],
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
let network = NetworkKind::from_zcash_address(network);
let network = network.into();
sapling::PaymentAddress::from_bytes(&data)
.map(|address| Self::Sapling { address, network })
.ok_or_else(|| BoxError::from("not a valid sapling address").into())
Expand All @@ -78,7 +78,7 @@ impl zcash_address::TryFromAddress for Address {
network: zcash_address::Network,
unified_address: zcash_address::unified::Address,
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
let network = NetworkKind::from_zcash_address(network);
let network = network.into();
let mut orchard = None;
let mut sapling = None;
let mut transparent = None;
Expand Down Expand Up @@ -163,34 +163,36 @@ impl Address {
Self::Transparent(address) => Some(address.to_string()),
Self::Sapling { address, network } => {
let data = address.to_bytes();
let address = ZcashAddress::from_sapling(network.to_zcash_address(), data);
let address = ZcashAddress::from_sapling(network.into(), data);
Some(address.encode())
}
Self::Unified { .. } => None,
}
}
}

impl NetworkKind {
/// Converts a [`zcash_address::Network`] to a [`NetworkKind`].
///
/// This method is meant to be used for decoding Zcash addresses in zebra-rpc methods.
fn from_zcash_address(network: zcash_address::Network) -> Self {
impl From<zcash_address::Network> for NetworkKind {
fn from(network: zcash_address::Network) -> Self {
match network {
zcash_address::Network::Main => NetworkKind::Mainnet,
zcash_address::Network::Test => NetworkKind::Testnet,
zcash_address::Network::Regtest => NetworkKind::Regtest,
}
}
}

/// Converts a [`zcash_address::Network`] to a [`NetworkKind`].
///
/// This method is meant to be used for encoding Zcash addresses in zebra-rpc methods.
fn to_zcash_address(self) -> zcash_address::Network {
match self {
impl From<NetworkKind> for zcash_address::Network {
fn from(network: NetworkKind) -> Self {
match network {
NetworkKind::Mainnet => zcash_address::Network::Main,
NetworkKind::Testnet => zcash_address::Network::Test,
NetworkKind::Regtest => zcash_address::Network::Regtest,
}
}
}

impl From<&NetworkKind> for zcash_address::Network {
fn from(network: &NetworkKind) -> Self {
(*network).into()
}
}
Loading