Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`

This commit is contained in:
Arya 2024-04-17 21:10:09 -04:00
parent fea2162419
commit a94dcabaca
2 changed files with 22 additions and 11 deletions

View File

@ -138,15 +138,13 @@ impl fmt::Display for NetworkKind {
} }
} }
impl From<&Network> for &'static str { impl<'a> From<&'a Network> for &'a str {
fn from(network: &Network) -> &'static str { fn from(network: &'a Network) -> &'a str {
match network { match network {
Network::Mainnet => "Mainnet", Network::Mainnet => "Mainnet",
// TODO: // TODO:
// - Add a `name` field to use here instead of checking `is_default_testnet()`
// - zcashd calls the Regtest cache dir 'regtest' (#8327). // - zcashd calls the Regtest cache dir 'regtest' (#8327).
Network::Testnet(params) if params.is_default_testnet() => "Testnet", Network::Testnet(params) => params.network_name(),
Network::Testnet(_params) => "UnknownTestnet",
} }
} }
} }

View File

@ -35,6 +35,8 @@ pub struct ConfiguredActivationHeights {
/// Builder for the [`Parameters`] struct. /// Builder for the [`Parameters`] struct.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ParametersBuilder { pub struct ParametersBuilder {
/// The name of this network to be used by the `Display` trait impl.
network_name: String,
/// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details. /// The network upgrade activation heights for this network, see [`Parameters::activation_heights`] for more details.
activation_heights: BTreeMap<Height, NetworkUpgrade>, activation_heights: BTreeMap<Height, NetworkUpgrade>,
/// Sapling extended spending key human-readable prefix for this network /// Sapling extended spending key human-readable prefix for this network
@ -48,6 +50,7 @@ pub struct ParametersBuilder {
impl Default for ParametersBuilder { impl Default for ParametersBuilder {
fn default() -> Self { fn default() -> Self {
Self { Self {
network_name: "Testnet".to_string(),
// # Correctness // # Correctness
// //
// `Genesis` network upgrade activation height must always be 0 // `Genesis` network upgrade activation height must always be 0
@ -69,6 +72,12 @@ impl Default for ParametersBuilder {
} }
impl ParametersBuilder { impl ParametersBuilder {
/// Sets the network name to be used in the [`Parameters`] being built.
pub fn network_name(mut self, network_name: String) -> Self {
self.network_name = network_name;
self
}
/// Checks that the provided network upgrade activation heights are in the correct order, then /// Checks that the provided network upgrade activation heights are in the correct order, then
/// sets them as the new network upgrade activation heights. /// sets them as the new network upgrade activation heights.
pub fn activation_heights( pub fn activation_heights(
@ -136,12 +145,14 @@ impl ParametersBuilder {
/// Converts the builder to a [`Parameters`] struct /// Converts the builder to a [`Parameters`] struct
pub fn finish(self) -> Parameters { pub fn finish(self) -> Parameters {
let Self { let Self {
network_name,
activation_heights, activation_heights,
hrp_sapling_extended_spending_key, hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key, hrp_sapling_extended_full_viewing_key,
hrp_sapling_payment_address, hrp_sapling_payment_address,
} = self; } = self;
Parameters { Parameters {
network_name,
activation_heights, activation_heights,
hrp_sapling_extended_spending_key, hrp_sapling_extended_spending_key,
hrp_sapling_extended_full_viewing_key, hrp_sapling_extended_full_viewing_key,
@ -158,6 +169,8 @@ impl ParametersBuilder {
/// Network consensus parameters for test networks such as Regtest and the default Testnet. /// Network consensus parameters for test networks such as Regtest and the default Testnet.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Parameters { pub struct Parameters {
/// The name of this network to be used by the `Display` trait impl.
network_name: String,
/// The network upgrade activation heights for this network. /// The network upgrade activation heights for this network.
/// ///
/// Note: This value is ignored by `Network::activation_list()` when `zebra-chain` is /// Note: This value is ignored by `Network::activation_list()` when `zebra-chain` is
@ -177,12 +190,7 @@ impl Default for Parameters {
fn default() -> Self { fn default() -> Self {
Self { Self {
activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(), activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
hrp_sapling_extended_spending_key: ..Self::build().finish()
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(),
} }
} }
} }
@ -198,6 +206,11 @@ impl Parameters {
self == &Self::default() self == &Self::default()
} }
/// Returns the network upgrade activation heights
pub fn network_name(&self) -> &str {
&self.network_name
}
/// Returns the network upgrade activation heights /// Returns the network upgrade activation heights
pub fn activation_heights(&self) -> &BTreeMap<Height, NetworkUpgrade> { pub fn activation_heights(&self) -> &BTreeMap<Height, NetworkUpgrade> {
&self.activation_heights &self.activation_heights