Skip to content

Commit

Permalink
Remove customizable HRPs
Browse files Browse the repository at this point in the history
  • Loading branch information
upbqdn committed Jun 15, 2024
1 parent cb21d9f commit 4d03a56
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 175 deletions.
90 changes: 0 additions & 90 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Types and implementation for Testnet consensus parameters
use std::{collections::BTreeMap, fmt};

use zcash_primitives::constants as zp_constants;

use crate::{
block::{self, Height},
parameters::{
Expand Down Expand Up @@ -76,12 +74,6 @@ pub struct ParametersBuilder {
genesis_hash: block::Hash,
/// 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,
/// Slow start interval for this network
slow_start_interval: Height,
/// Target difficulty limit for this network
Expand All @@ -100,12 +92,6 @@ impl Default for ParametersBuilder {
//
// `Genesis` network upgrade activation height must always be 0
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(),
genesis_hash: TESTNET_GENESIS_HASH
.parse()
.expect("hard-coded hash parses"),
Expand Down Expand Up @@ -167,44 +153,6 @@ impl ParametersBuilder {
self
}

/// Checks that the provided Sapling human-readable prefixes (HRPs) are valid and unique, then
/// sets the Sapling HRPs to be used in the [`Parameters`] being built.
pub fn with_sapling_hrps(
mut self,
hrp_sapling_extended_spending_key: impl fmt::Display,
hrp_sapling_extended_full_viewing_key: impl fmt::Display,
hrp_sapling_payment_address: impl fmt::Display,
) -> Self {
self.hrp_sapling_extended_spending_key = hrp_sapling_extended_spending_key.to_string();
self.hrp_sapling_extended_full_viewing_key =
hrp_sapling_extended_full_viewing_key.to_string();
self.hrp_sapling_payment_address = hrp_sapling_payment_address.to_string();

let sapling_hrps = [
&self.hrp_sapling_extended_spending_key,
&self.hrp_sapling_extended_full_viewing_key,
&self.hrp_sapling_payment_address,
];

for sapling_hrp in sapling_hrps {
assert!(sapling_hrp.len() <= MAX_HRP_LENGTH, "Sapling human-readable prefix {sapling_hrp} is too long, must be {MAX_HRP_LENGTH} characters or less");
assert!(
sapling_hrp.chars().all(|c| c.is_ascii_lowercase() || c == '-'),
"human-readable prefixes should contain only lowercase ASCII characters and dashes, hrp: {sapling_hrp}"
);
assert_eq!(
sapling_hrps
.iter()
.filter(|&&hrp| hrp == sapling_hrp)
.count(),
1,
"Sapling human-readable prefixes must be unique, repeated Sapling HRP: {sapling_hrp}"
);
}

self
}

/// Parses the hex-encoded block hash and sets it as the genesis hash in the [`Parameters`] being built.
pub fn with_genesis_hash(mut self, genesis_hash: impl fmt::Display) -> Self {
self.genesis_hash = genesis_hash
Expand Down Expand Up @@ -309,9 +257,6 @@ impl ParametersBuilder {
network_magic,
genesis_hash,
activation_heights,
hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address,
slow_start_interval,
target_difficulty_limit,
disable_pow,
Expand All @@ -321,9 +266,6 @@ impl ParametersBuilder {
network_magic,
genesis_hash,
activation_heights,
hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address,
slow_start_interval,
slow_start_shift: Height(slow_start_interval.0 / 2),
target_difficulty_limit,
Expand Down Expand Up @@ -352,12 +294,6 @@ 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,
/// Slow start interval for this network
slow_start_interval: Height,
/// Slow start shift for this network, always half the slow start interval
Expand Down Expand Up @@ -400,11 +336,6 @@ impl Parameters {
.with_target_difficulty_limit(U256::from_big_endian(&[0x0f; 32]))
.with_disable_pow(true)
.with_slow_start_interval(Height::MIN)
.with_sapling_hrps(
zp_constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY,
zp_constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
zp_constants::regtest::HRP_SAPLING_PAYMENT_ADDRESS,
)
// Removes default Testnet activation heights if not configured,
// most network upgrades are disabled by default for Regtest in zcashd
.with_activation_heights(ConfiguredActivationHeights {
Expand All @@ -429,9 +360,6 @@ impl Parameters {
genesis_hash,
// Activation heights are configurable on Regtest
activation_heights: _,
hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address,
slow_start_interval,
slow_start_shift,
target_difficulty_limit,
Expand All @@ -441,9 +369,6 @@ impl Parameters {
self.network_name == network_name
&& self.network_magic == network_magic
&& self.genesis_hash == genesis_hash
&& self.hrp_sapling_extended_spending_key == hrp_sapling_extended_spending_key
&& self.hrp_sapling_extended_full_viewing_key == hrp_sapling_extended_full_viewing_key
&& self.hrp_sapling_payment_address == hrp_sapling_payment_address
&& self.slow_start_interval == slow_start_interval
&& self.slow_start_shift == slow_start_shift
&& self.target_difficulty_limit == target_difficulty_limit
Expand All @@ -470,21 +395,6 @@ impl Parameters {
&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
}

/// Returns slow start interval for this network
pub fn slow_start_interval(&self) -> Height {
self.slow_start_interval
Expand Down
87 changes: 2 additions & 85 deletions zebra-chain/src/parameters/network/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
//! Fixed test vectors for the network consensus parameters.

use zcash_primitives::{
consensus::{self as zp_consensus, Parameters},
constants as zp_constants,
};
use zcash_primitives::consensus::{self as zp_consensus, Parameters};
use zcash_protocol::consensus::NetworkConstants as _;

use crate::{
block::Height,
parameters::{
testnet::{
self, ConfiguredActivationHeights, MAX_HRP_LENGTH, MAX_NETWORK_NAME_LENGTH,
RESERVED_NETWORK_NAMES,
self, ConfiguredActivationHeights, MAX_NETWORK_NAME_LENGTH, RESERVED_NETWORK_NAMES,
},
Network, NetworkUpgrade, MAINNET_ACTIVATION_HEIGHTS, NETWORK_UPGRADES_IN_ORDER,
TESTNET_ACTIVATION_HEIGHTS,
Expand Down Expand Up @@ -217,85 +213,6 @@ fn check_configured_network_name() {
);
}

/// Checks that configured Sapling human-readable prefixes (HRPs) are validated and used correctly.
#[test]
fn check_configured_sapling_hrps() {
// Sets a no-op panic hook to avoid long output.
std::panic::set_hook(Box::new(|_| {}));

// Check that configured Sapling HRPs must be unique.
std::panic::catch_unwind(|| {
testnet::Parameters::build().with_sapling_hrps("", "", "");
})
.expect_err("should panic when setting non-unique Sapling HRPs");

// Check that max length is enforced, and that network names may only contain lowecase ASCII characters and dashes.
for invalid_hrp in [
"a".repeat(MAX_NETWORK_NAME_LENGTH + 1),
"!!!!non-alphabetical-name".to_string(),
"A".to_string(),
] {
std::panic::catch_unwind(|| {
testnet::Parameters::build().with_sapling_hrps(invalid_hrp, "dummy-hrp-a", "dummy-hrp-b");
})
.expect_err("should panic when setting Sapling HRPs that are too long or contain non-alphanumeric characters (except '-')");
}

// Restore the regular panic hook for any unexpected panics
drop(std::panic::take_hook());

// Check that Sapling HRPs can contain lowercase ascii characters and dashes.
let expected_hrp_sapling_extended_spending_key = "sapling-hrp-a";
let expected_hrp_sapling_extended_full_viewing_key = "sapling-hrp-b";
let expected_hrp_sapling_payment_address = "sapling-hrp-c";

let network = testnet::Parameters::build()
// Check that Sapling HRPs can contain `MAX_HRP_LENGTH` characters
.with_sapling_hrps("a".repeat(MAX_HRP_LENGTH), "dummy-hrp-a", "dummy-hrp-b")
.with_sapling_hrps(
expected_hrp_sapling_extended_spending_key,
expected_hrp_sapling_extended_full_viewing_key,
expected_hrp_sapling_payment_address,
)
.to_network();

// Check that configured Sapling HRPs are returned by `Parameters` trait methods
assert_eq!(
network.hrp_sapling_extended_spending_key(),
expected_hrp_sapling_extended_spending_key,
"should return expected Sapling extended spending key HRP"
);
assert_eq!(
network.hrp_sapling_extended_full_viewing_key(),
expected_hrp_sapling_extended_full_viewing_key,
"should return expected Sapling EFVK HRP"
);
assert_eq!(
network.hrp_sapling_payment_address(),
expected_hrp_sapling_payment_address,
"should return expected Sapling payment address HRP"
);

// Check that default Mainnet, Testnet, and Regtest HRPs are valid, these calls will panic
// if any of the values fail validation.
testnet::Parameters::build()
.with_sapling_hrps(
zp_constants::mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
zp_constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
zp_constants::mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
)
.with_sapling_hrps(
zp_constants::testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
zp_constants::testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
zp_constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS,
)
.with_sapling_hrps(
zp_constants::regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY,
zp_constants::regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
zp_constants::regtest::HRP_SAPLING_PAYMENT_ADDRESS,
);
}

/// Checks that configured testnet names are validated and used correctly.
#[test]
fn check_network_name() {
Expand Down

0 comments on commit 4d03a56

Please sign in to comment.