Skip to content

Commit

Permalink
Adds NetworkKind and uses it instead of Network in `HistoryTreePa…
Browse files Browse the repository at this point in the history
…rts` and `transparent::Address`
  • Loading branch information
arya2 committed Mar 22, 2024
1 parent 5cd9941 commit dbf3296
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 144 deletions.
2 changes: 1 addition & 1 deletion zebra-chain/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod arbitrary;

pub use error::*;
pub use genesis::*;
pub use network::Network;
pub use network::{Network, NetworkKind};
pub use network_upgrade::*;
pub use transaction::*;

Expand Down
101 changes: 71 additions & 30 deletions zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ impl NetworkParameters {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
/// An enum describing the kind of network, whether it's the production mainnet or a testnet.
pub enum NetworkKind {
/// The production mainnet.
Mainnet,

/// A test network.
Testnet,
}

/// An enum describing the possible network choices.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
Expand Down Expand Up @@ -106,43 +117,52 @@ impl<'de> Deserialize<'de> for Network {
}
}

impl Network {
impl NetworkKind {
/// Returns the human-readable prefix for Base58Check-encoded transparent
/// pay-to-public-key-hash payment addresses for the network.
pub fn b58_pubkey_address_prefix(&self) -> [u8; 2] {
<ZcashPrimitivesNetwork>::try_from(self)
// This prefix is the same for Testnet and Regtest in zcashd.
// TODO: Use the constants directly when implementing `Parameters` for `Network` (#8365)
.unwrap_or(ZcashPrimitivesNetwork::TestNetwork)
.b58_pubkey_address_prefix()
pub fn b58_pubkey_address_prefix(self) -> [u8; 2] {
<ZcashPrimitivesNetwork>::from(self).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] {
<ZcashPrimitivesNetwork>::try_from(self)
// This prefix is the same for Testnet and Regtest in zcashd.
// TODO: Use the constants directly when implementing `Parameters` for `Network` (#8365)
.unwrap_or(ZcashPrimitivesNetwork::TestNetwork)
.b58_script_address_prefix()
pub fn b58_script_address_prefix(self) -> [u8; 2] {
<ZcashPrimitivesNetwork>::from(self).b58_script_address_prefix()
}
/// Returns true if the maximum block time rule is active for `network` and `height`.
///
/// Always returns true if `network` is the Mainnet.
/// If `network` is the Testnet, the `height` should be at least
/// TESTNET_MAX_TIME_START_HEIGHT to return true.
/// Returns false otherwise.
///
/// Part of the consensus rules at <https://zips.z.cash/protocol/protocol.pdf#blockheader>
pub fn is_max_block_time_enforced(&self, height: block::Height) -> bool {

/// Return the network name as defined in
/// [BIP70](https://github.com/bitcoin/bips/blob/master/bip-0070.mediawiki#paymentdetailspaymentrequest)
pub fn bip70_network_name(&self) -> String {
match self {
Network::Mainnet => true,
// TODO: Move `TESTNET_MAX_TIME_START_HEIGHT` to a field on NetworkParameters (#8364)
Network::Testnet(_params) => height >= super::TESTNET_MAX_TIME_START_HEIGHT,
Self::Mainnet => "main".to_string(),
Self::Testnet => "test".to_string(),
}
}

/// Converts a [`zcash_address::Network`] to a [`NetworkKind`].
pub fn from_zcash_address(network: zcash_address::Network) -> Self {
match network {
zcash_address::Network::Main => NetworkKind::Mainnet,
zcash_address::Network::Test | zcash_address::Network::Regtest => NetworkKind::Testnet,
}
}
}

impl From<NetworkKind> for &'static str {
fn from(network: NetworkKind) -> &'static str {
match network {
NetworkKind::Mainnet => "MainnetKind",
NetworkKind::Testnet => "TestnetKind",
}
}
}

impl fmt::Display for NetworkKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str((*self).into())
}
}

impl From<&Network> for &'static str {
fn from(network: &Network) -> &'static str {
match network {
Expand Down Expand Up @@ -178,17 +198,41 @@ impl Network {
}
}

/// Returns the [`NetworkKind`] for this network.
pub fn kind(&self) -> NetworkKind {
match self {
Network::Mainnet => NetworkKind::Mainnet,
Network::Testnet(_) => NetworkKind::Testnet,
}
}

/// Returns an iterator over [`Network`] variants.
pub fn iter() -> impl Iterator<Item = Self> {
// TODO: Use default values of `Testnet` variant when adding fields for #7845.
[Self::Mainnet, Self::new_default_testnet()].into_iter()
}

/// Returns true if the maximum block time rule is active for `network` and `height`.
///
/// Always returns true if `network` is the Mainnet.
/// If `network` is the Testnet, the `height` should be at least
/// TESTNET_MAX_TIME_START_HEIGHT to return true.
/// Returns false otherwise.
///
/// Part of the consensus rules at <https://zips.z.cash/protocol/protocol.pdf#blockheader>
pub fn is_max_block_time_enforced(&self, height: block::Height) -> bool {
match self {
Network::Mainnet => true,
// TODO: Move `TESTNET_MAX_TIME_START_HEIGHT` to a field on NetworkParameters (#8364)
Network::Testnet(_params) => height >= super::TESTNET_MAX_TIME_START_HEIGHT,
}
}

/// Get the default port associated to this network.
pub fn default_port(&self) -> u16 {
match self {
Network::Mainnet => 8233,
// TODO: Add a `default_port` field to `NetworkParameters` to return here.
// TODO: Add a `default_port` field to `NetworkParameters` to return here. (zcashd uses 18344 for Regtest)
Network::Testnet(_params) => 18233,
}
}
Expand All @@ -214,10 +258,7 @@ impl Network {
/// Return the network name as defined in
/// [BIP70](https://github.com/bitcoin/bips/blob/master/bip-0070.mediawiki#paymentdetailspaymentrequest)
pub fn bip70_network_name(&self) -> String {
match self {
Network::Mainnet => "main".to_string(),
Network::Testnet(_params) => "test".to_string(),
}
self.kind().bip70_network_name()
}

/// Return the lowercase network name.
Expand Down
42 changes: 22 additions & 20 deletions zebra-chain/src/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zcash_address::unified::{self, Container};
use zcash_primitives::sapling;

use crate::{
parameters::{Network, UnsupportedNetwork},
parameters::{Network, NetworkKind, UnsupportedNetwork},
transparent, BoxError,
};

Expand All @@ -17,17 +17,17 @@ pub enum Address {

/// Sapling address
Sapling {
/// Address' network
network: Network,
/// Address' network kind
network: NetworkKind,

/// Sapling address
address: sapling::PaymentAddress,
},

/// Unified address
Unified {
/// Address' network
network: Network,
/// Address' network kind
network: NetworkKind,

/// Unified address
unified_address: zcash_address::unified::Address,
Expand Down Expand Up @@ -73,6 +73,15 @@ impl TryFrom<&Network> for zcash_address::Network {
}
}

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,
}
}
}

impl zcash_address::TryFromAddress for Address {
// TODO: crate::serialization::SerializationError
type Error = BoxError;
Expand All @@ -82,7 +91,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(
&network.try_into()?,
NetworkKind::from_zcash_address(network),
data,
)))
}
Expand All @@ -92,7 +101,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(
&network.try_into()?,
NetworkKind::from_zcash_address(network),
data,
)))
}
Expand All @@ -101,7 +110,7 @@ impl zcash_address::TryFromAddress for Address {
network: zcash_address::Network,
data: [u8; 43],
) -> Result<Self, zcash_address::ConversionError<Self::Error>> {
let network = network.try_into()?;
let network = NetworkKind::from_zcash_address(network);
sapling::PaymentAddress::from_bytes(&data)
.map(|address| Self::Sapling { address, network })
.ok_or_else(|| BoxError::from("not a valid sapling address").into())
Expand All @@ -111,7 +120,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 = &network.try_into()?;
let network = NetworkKind::from_zcash_address(network);
let mut orchard = None;
let mut sapling = None;
let mut transparent = None;
Expand Down Expand Up @@ -155,7 +164,7 @@ impl zcash_address::TryFromAddress for Address {
}

Ok(Self::Unified {
network: network.clone(),
network,
unified_address,
orchard,
sapling,
Expand All @@ -166,10 +175,10 @@ impl zcash_address::TryFromAddress for Address {

impl Address {
/// Returns the network for the address.
pub fn network(&self) -> Network {
pub fn network(&self) -> NetworkKind {
match &self {
Self::Transparent(address) => address.network(),
Self::Sapling { network, .. } | Self::Unified { network, .. } => network.clone(),
Self::Sapling { network, .. } | Self::Unified { network, .. } => *network,
}
}

Expand All @@ -196,14 +205,7 @@ impl Address {
Self::Transparent(address) => Some(address.to_string()),
Self::Sapling { address, network } => {
let data = address.to_bytes();
let network = network
.try_into()
.map_err(|err| {
warn!(?err, "could not convert address network to zcash network")
})
.ok()?;

let address = ZcashAddress::from_sapling(network, data);
let address = ZcashAddress::from_sapling((*network).into(), data);
Some(address.encode())
}
Self::Unified { .. } => None,
Expand Down
19 changes: 14 additions & 5 deletions zebra-chain/src/primitives/zcash_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zcash_primitives::transaction as zp_tx;

use crate::{
amount::{Amount, NonNegative},
parameters::{Network, NetworkUpgrade, UnsupportedNetwork},
parameters::{Network, NetworkKind, NetworkUpgrade, UnsupportedNetwork},
serialization::ZcashSerialize,
transaction::{AuthDigest, HashType, SigHash, Transaction},
transparent::{self, Script},
Expand Down Expand Up @@ -328,11 +328,11 @@ pub(crate) fn transparent_output_address(

match alt_addr {
Some(zcash_primitives::legacy::TransparentAddress::PublicKey(pub_key_hash)) => Some(
transparent::Address::from_pub_key_hash(network, pub_key_hash),
transparent::Address::from_pub_key_hash(network.kind(), pub_key_hash),
),
Some(zcash_primitives::legacy::TransparentAddress::Script(script_hash)) => Some(
transparent::Address::from_script_hash(network.kind(), script_hash),
),
Some(zcash_primitives::legacy::TransparentAddress::Script(script_hash)) => {
Some(transparent::Address::from_script_hash(network, script_hash))
}
None => None,
}
}
Expand All @@ -356,6 +356,15 @@ impl TryFrom<&Network> for zcash_primitives::consensus::Network {
}
}

impl From<NetworkKind> for zcash_primitives::consensus::Network {
fn from(network: NetworkKind) -> Self {
match network {
NetworkKind::Mainnet => zcash_primitives::consensus::Network::MainNetwork,
NetworkKind::Testnet => zcash_primitives::consensus::Network::TestNetwork,
}
}
}

impl From<zcash_primitives::consensus::Network> for Network {
fn from(network: zcash_primitives::consensus::Network) -> Self {
match network {
Expand Down
Loading

0 comments on commit dbf3296

Please sign in to comment.