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(consensus): Adds Sapling HRPs as fields on testnet::Parameters #8398

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,43 +308,29 @@ impl zp_consensus::Parameters for Network {
fn address_network(&self) -> Option<zcash_address::Network> {
match self {
Network::Mainnet => Some(zcash_address::Network::Main),
Network::Testnet(params) if params.is_default_testnet() => {
Some(zcash_address::Network::Test)
}
_other => None,
// TODO: Check if network is `Regtest` first, and if it is, return `zcash_address::Network::Regtest`
Network::Testnet(_params) => Some(zcash_address::Network::Test),
}
}

fn hrp_sapling_extended_spending_key(&self) -> &str {
match self {
Network::Mainnet => zp_constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
Network::Testnet(params) if params.is_default_testnet() => {
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY
}
// TODO: Add this as a field to testnet params
_other => "secret-extended-key-unknown-test",
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::Testnet(params) if params.is_default_testnet() => {
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY
}
// TODO: Add this as a field to testnet params
_other => "zxviewunknowntestsapling",
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::Testnet(params) if params.is_default_testnet() => {
zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS
}
// TODO: Add this as a field to testnet params
_other => "zunknowntestsapling",
Network::Testnet(params) => params.hrp_sapling_payment_address(),
}
}

Expand Down
57 changes: 54 additions & 3 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Types and implementation for Testnet consensus parameters
use std::collections::BTreeMap;

use zcash_primitives::constants as zp_constants;

use crate::{
block::Height,
parameters::{
Expand Down Expand Up @@ -35,6 +37,12 @@ pub struct ConfiguredActivationHeights {
pub struct ParametersBuilder {
/// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details.
activation_heights: BTreeMap<Height, NetworkUpgrade>,
/// Sapling extended spending key human-readable prefix for this network
hrp_sapling_extended_spending_key: String,
/// Sapling extended full viewing key human-readable prefix for this network
hrp_sapling_extended_full_viewing_key: String,
/// Sapling payment address human-readable prefix for this network
hrp_sapling_payment_address: String,
}

impl Default for ParametersBuilder {
Expand All @@ -50,6 +58,12 @@ impl Default for ParametersBuilder {
]
.into_iter()
.collect(),
hrp_sapling_extended_spending_key:
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY.to_string(),
hrp_sapling_extended_full_viewing_key:
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY.to_string(),
hrp_sapling_payment_address: zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS
.to_string(),
}
}
}
Expand Down Expand Up @@ -120,8 +134,18 @@ impl ParametersBuilder {

/// Converts the builder to a [`Parameters`] struct
pub fn finish(self) -> Parameters {
let Self { activation_heights } = self;
Parameters { activation_heights }
let Self {
activation_heights,
hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address,
} = self;
Parameters {
activation_heights,
hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address,
}
}

/// Converts the builder to a configured [`Network::Testnet`]
Expand All @@ -139,13 +163,25 @@ pub struct Parameters {
/// compiled with the `zebra-test` feature flag AND the `TEST_FAKE_ACTIVATION_HEIGHTS`
/// environment variable is set.
activation_heights: BTreeMap<Height, NetworkUpgrade>,
/// Sapling extended spending key human-readable prefix for this network
hrp_sapling_extended_spending_key: String,
/// Sapling extended full viewing key human-readable prefix for this network
hrp_sapling_extended_full_viewing_key: String,
/// Sapling payment address human-readable prefix for this network
hrp_sapling_payment_address: String,
}

impl Default for Parameters {
/// Returns an instance of the default public testnet [`Parameters`].
fn default() -> Self {
Self {
activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
hrp_sapling_extended_spending_key:
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY.to_string(),
hrp_sapling_extended_full_viewing_key:
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY.to_string(),
hrp_sapling_payment_address: zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS
.to_string(),
}
}
}
Expand All @@ -161,8 +197,23 @@ impl Parameters {
self == &Self::default()
}

/// Returns a reference to the network upgrade activation heights
/// Returns the network upgrade activation heights
pub fn activation_heights(&self) -> &BTreeMap<Height, NetworkUpgrade> {
&self.activation_heights
}

/// Returns the `hrp_sapling_extended_spending_key` field
pub fn hrp_sapling_extended_spending_key(&self) -> &str {
&self.hrp_sapling_extended_spending_key
}

/// Returns the `hrp_sapling_extended_full_viewing_key` field
pub fn hrp_sapling_extended_full_viewing_key(&self) -> &str {
&self.hrp_sapling_extended_full_viewing_key
}

/// Returns the `hrp_sapling_payment_address` field
pub fn hrp_sapling_payment_address(&self) -> &str {
&self.hrp_sapling_payment_address
}
}
Loading