2023-01-17 16:27:42 -08:00
|
|
|
|
//! Consensus parameters for each Zcash network.
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
use std::{fmt, str::FromStr, sync::Arc};
|
2022-05-05 20:31:52 -07:00
|
|
|
|
|
|
|
|
|
use thiserror::Error;
|
2020-10-30 12:36:20 -07:00
|
|
|
|
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
use zcash_primitives::{consensus as zp_consensus, constants as zp_constants};
|
2024-04-16 19:20:34 -07:00
|
|
|
|
|
2023-03-29 16:06:31 -07:00
|
|
|
|
use crate::{
|
2024-03-12 14:41:44 -07:00
|
|
|
|
block::{self, Height, HeightDiff},
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
parameters::NetworkUpgrade,
|
2023-03-29 16:06:31 -07:00
|
|
|
|
};
|
2021-06-17 17:05:28 -07:00
|
|
|
|
|
add(consensus/network): Adds `new_regtest()` constructors to `testnet::Parameters` and `Network` (#8413)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-24 21:04:05 -07:00
|
|
|
|
use self::testnet::ConfiguredActivationHeights;
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
pub mod testnet;
|
|
|
|
|
|
2022-03-16 17:37:50 -07:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
|
|
|
|
/// The ZIP-212 grace period length after the Canopy activation height.
|
|
|
|
|
///
|
|
|
|
|
/// # Consensus
|
|
|
|
|
///
|
|
|
|
|
/// ZIP-212 requires Zcash nodes to validate that Sapling spends and Orchard actions follows a
|
|
|
|
|
/// specific plaintext format after Canopy's activation.
|
|
|
|
|
///
|
|
|
|
|
/// > [Heartwood onward] All Sapling and Orchard outputs in coinbase transactions MUST decrypt to a
|
|
|
|
|
/// > note plaintext , i.e. the procedure in § 4.19.3 ‘Decryption using a Full Viewing Key (Sapling
|
|
|
|
|
/// > and Orchard)’ on p. 67 does not return ⊥, using a sequence of 32 zero bytes as the outgoing
|
|
|
|
|
/// > viewing key . (This implies that before Canopy activation, Sapling outputs of a coinbase
|
|
|
|
|
/// > transaction MUST have note plaintext lead byte equal to 0x01.)
|
|
|
|
|
///
|
|
|
|
|
/// > [Canopy onward] Any Sapling or Orchard output of a coinbase transaction decrypted to a note
|
|
|
|
|
/// > plaintext according to the preceding rule MUST have note plaintext lead byte equal to 0x02.
|
|
|
|
|
/// > (This applies even during the “grace period” specified in [ZIP-212].)
|
|
|
|
|
///
|
2022-05-30 13:12:11 -07:00
|
|
|
|
/// <https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus>
|
2022-03-16 17:37:50 -07:00
|
|
|
|
///
|
|
|
|
|
/// Wallets have a grace period of 32,256 blocks after Canopy's activation to validate those blocks,
|
|
|
|
|
/// but nodes do not.
|
|
|
|
|
///
|
|
|
|
|
/// > There is a "grace period" of 32256 blocks starting from the block at which this ZIP activates,
|
|
|
|
|
/// > during which note plaintexts with lead byte 0x01 MUST still be accepted [by wallets].
|
|
|
|
|
/// >
|
|
|
|
|
/// > Let ActivationHeight be the activation height of this ZIP, and let GracePeriodEndHeight be
|
|
|
|
|
/// > ActivationHeight + 32256.
|
|
|
|
|
///
|
2022-05-30 13:12:11 -07:00
|
|
|
|
/// <https://zips.z.cash/zip-0212#changes-to-the-process-of-receiving-sapling-or-orchard-notes>
|
2022-03-16 17:37:50 -07:00
|
|
|
|
///
|
|
|
|
|
/// Zebra uses `librustzcash` to validate that rule, but it won't validate it during the grace
|
|
|
|
|
/// period. Therefore Zebra must validate those blocks during the grace period using checkpoints.
|
|
|
|
|
/// Therefore the mandatory checkpoint height ([`Network::mandatory_checkpoint_height`]) must be
|
|
|
|
|
/// after the grace period.
|
2023-03-29 16:06:31 -07:00
|
|
|
|
const ZIP_212_GRACE_PERIOD_DURATION: HeightDiff = 32_256;
|
2022-03-16 17:37:50 -07:00
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
/// An enum describing the kind of network, whether it's the production mainnet or a testnet.
|
|
|
|
|
// Note: The order of these variants is important for correct bincode (de)serialization
|
|
|
|
|
// of history trees in the db format.
|
|
|
|
|
// TODO: Replace bincode (de)serialization of `HistoryTreeParts` in a db format upgrade?
|
|
|
|
|
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
|
|
|
|
pub enum NetworkKind {
|
|
|
|
|
/// The production mainnet.
|
|
|
|
|
#[default]
|
|
|
|
|
Mainnet,
|
|
|
|
|
|
|
|
|
|
/// A test network.
|
|
|
|
|
Testnet,
|
|
|
|
|
|
|
|
|
|
/// Regtest mode, not yet implemented
|
|
|
|
|
// TODO: Add `new_regtest()` and `is_regtest` methods on `Network`.
|
|
|
|
|
Regtest,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Network> for NetworkKind {
|
|
|
|
|
fn from(network: Network) -> Self {
|
|
|
|
|
network.kind()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 15:45:37 -07:00
|
|
|
|
/// An enum describing the possible network choices.
|
2024-04-16 19:20:34 -07:00
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize)]
|
|
|
|
|
#[serde(into = "NetworkKind")]
|
2020-08-15 15:45:37 -07:00
|
|
|
|
pub enum Network {
|
|
|
|
|
/// The production mainnet.
|
2023-01-17 16:27:42 -08:00
|
|
|
|
#[default]
|
2020-08-15 15:45:37 -07:00
|
|
|
|
Mainnet,
|
2023-01-17 16:27:42 -08:00
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
/// A test network such as the default public testnet,
|
|
|
|
|
/// a configured testnet, or Regtest.
|
|
|
|
|
Testnet(Arc<testnet::Parameters>),
|
2020-08-15 15:45:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
impl NetworkKind {
|
2024-03-12 14:41:44 -07:00
|
|
|
|
/// Returns the human-readable prefix for Base58Check-encoded transparent
|
|
|
|
|
/// pay-to-public-key-hash payment addresses for the network.
|
2024-04-16 19:20:34 -07:00
|
|
|
|
pub fn b58_pubkey_address_prefix(self) -> [u8; 2] {
|
|
|
|
|
match self {
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
Self::Mainnet => zp_constants::mainnet::B58_PUBKEY_ADDRESS_PREFIX,
|
|
|
|
|
Self::Testnet | Self::Regtest => zp_constants::testnet::B58_PUBKEY_ADDRESS_PREFIX,
|
2024-04-16 19:20:34 -07:00
|
|
|
|
}
|
2024-03-12 14:41:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the human-readable prefix for Base58Check-encoded transparent pay-to-script-hash
|
|
|
|
|
/// payment addresses for the network.
|
2024-04-16 19:20:34 -07:00
|
|
|
|
pub fn b58_script_address_prefix(self) -> [u8; 2] {
|
2024-03-12 14:41:44 -07:00
|
|
|
|
match self {
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
Self::Mainnet => zp_constants::mainnet::B58_SCRIPT_ADDRESS_PREFIX,
|
|
|
|
|
Self::Testnet | Self::Regtest => zp_constants::testnet::B58_SCRIPT_ADDRESS_PREFIX,
|
2024-04-16 19:20:34 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
|
if *self == Self::Mainnet {
|
|
|
|
|
"main".to_string()
|
|
|
|
|
} else {
|
|
|
|
|
"test".to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<NetworkKind> for &'static str {
|
|
|
|
|
fn from(network: NetworkKind) -> &'static str {
|
|
|
|
|
// These should be different from the `Display` impl for `Network` so that its lowercase form
|
|
|
|
|
// can't be parsed as the default Testnet in the `Network` `FromStr` impl, it's easy to
|
|
|
|
|
// distinguish them in logs, and so it's generally harder to confuse the two.
|
|
|
|
|
match network {
|
|
|
|
|
NetworkKind::Mainnet => "MainnetKind",
|
|
|
|
|
NetworkKind::Testnet => "TestnetKind",
|
|
|
|
|
NetworkKind::Regtest => "RegtestKind",
|
2024-03-12 14:41:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
impl fmt::Display for NetworkKind {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.write_str((*self).into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
add(chain): Adds a `network_name` field to `testnet::Parameters` (#8411)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-24 04:03:37 -07:00
|
|
|
|
impl<'a> From<&'a Network> for &'a str {
|
|
|
|
|
fn from(network: &'a Network) -> &'a str {
|
2021-04-19 13:48:14 -07:00
|
|
|
|
match network {
|
|
|
|
|
Network::Mainnet => "Mainnet",
|
add(chain): Adds a `network_name` field to `testnet::Parameters` (#8411)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-24 04:03:37 -07:00
|
|
|
|
Network::Testnet(params) => params.network_name(),
|
2021-04-19 13:48:14 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 12:36:20 -07:00
|
|
|
|
impl fmt::Display for Network {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-04-19 13:48:14 -07:00
|
|
|
|
f.write_str(self.into())
|
2020-10-30 12:36:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 10:19:20 -07:00
|
|
|
|
impl Network {
|
2024-04-16 19:20:34 -07:00
|
|
|
|
/// Creates a new [`Network::Testnet`] with the default Testnet [`testnet::Parameters`].
|
|
|
|
|
pub fn new_default_testnet() -> Self {
|
|
|
|
|
Self::Testnet(Arc::new(testnet::Parameters::default()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new configured [`Network::Testnet`] with the provided Testnet [`testnet::Parameters`].
|
|
|
|
|
pub fn new_configured_testnet(params: testnet::Parameters) -> Self {
|
|
|
|
|
Self::Testnet(Arc::new(params))
|
|
|
|
|
}
|
|
|
|
|
|
add(consensus/network): Adds `new_regtest()` constructors to `testnet::Parameters` and `Network` (#8413)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-24 21:04:05 -07:00
|
|
|
|
/// Creates a new [`Network::Testnet`] with `Regtest` parameters and the provided network upgrade activation heights.
|
|
|
|
|
pub fn new_regtest(activation_heights: ConfiguredActivationHeights) -> Self {
|
|
|
|
|
Self::new_configured_testnet(testnet::Parameters::new_regtest(activation_heights))
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
/// Returns true if the network is the default Testnet, or false otherwise.
|
|
|
|
|
pub fn is_default_testnet(&self) -> bool {
|
|
|
|
|
if let Self::Testnet(params) = self {
|
|
|
|
|
params.is_default_testnet()
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
add(consensus/network): Adds `new_regtest()` constructors to `testnet::Parameters` and `Network` (#8413)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
* Adds `network_name` field and accessor method on `Parameters`, uses it in the `Display` impl for `Network`
* Removes unnecessary `.filter()`
* adds constraints on valid network names and a test
* adds config field for setting network name, adds "with_" prefix to ParameterBuilder setter methods
* Adds `MainnetKind`, `TestnetKind`, and `RegtestKind` to reserved network names
* updates stored test configs and fixes `network_name()` docs
* Adds a `new_regtest()` method on `Network` and `testnet::Parameters`, updates config deserialization to return an error if the initial_testnet_peers include any default initial peers AND contain configured activation heights
* Updates `activates_network_upgrades_correctly` test to check Regtest activation heights (and default Mainnet/Testnet)
* Refactors if-let & match statement into general match statement, removes constraint against including `testnet_parameters` field/section in the config when using `Mainnet` or `Regtest`
* Removes outdated TODO
* Restores `testnet_parameters` section of the latest stored config.
* Adds `with_sapling_hrps()` method and uses it to set the Regtest HRPs in `new_regtest()`.
Adds a test for Sapling HRP validation
* Checks that default Mainnet/Testnet/Regtest Sapling HRPs pass validation in `with_sapling_hrps()`
* Uses the correct constant in test
* Adds `is_regtest()` methods
* Updates test docs
* drop custom panic hooks after expected panics
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-24 21:04:05 -07:00
|
|
|
|
/// Returns true if the network is Regtest, or false otherwise.
|
|
|
|
|
pub fn is_regtest(&self) -> bool {
|
|
|
|
|
if let Self::Testnet(params) = self {
|
|
|
|
|
params.is_regtest()
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
/// Returns the [`NetworkKind`] for this network.
|
|
|
|
|
pub fn kind(&self) -> NetworkKind {
|
|
|
|
|
match self {
|
|
|
|
|
Network::Mainnet => NetworkKind::Mainnet,
|
|
|
|
|
// TODO: Return `NetworkKind::Regtest` if the parameters match the default Regtest params
|
|
|
|
|
Network::Testnet(_) => NetworkKind::Testnet,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 15:45:38 -08:00
|
|
|
|
/// 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.
|
2024-04-16 19:20:34 -07:00
|
|
|
|
[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 testnet::Parameters (#8364)
|
|
|
|
|
Network::Testnet(_params) => height >= super::TESTNET_MAX_TIME_START_HEIGHT,
|
|
|
|
|
}
|
2024-02-19 15:45:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 10:19:20 -07:00
|
|
|
|
/// Get the default port associated to this network.
|
|
|
|
|
pub fn default_port(&self) -> u16 {
|
|
|
|
|
match self {
|
|
|
|
|
Network::Mainnet => 8233,
|
2024-04-16 19:20:34 -07:00
|
|
|
|
// TODO: Add a `default_port` field to `testnet::Parameters` to return here. (zcashd uses 18344 for Regtest)
|
|
|
|
|
Network::Testnet(_params) => 18233,
|
2020-09-04 10:19:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-17 17:05:28 -07:00
|
|
|
|
|
|
|
|
|
/// Get the mandatory minimum checkpoint height for this network.
|
|
|
|
|
///
|
|
|
|
|
/// Mandatory checkpoints are a Zebra-specific feature.
|
|
|
|
|
/// If a Zcash consensus rule only applies before the mandatory checkpoint,
|
|
|
|
|
/// Zebra can skip validation of that rule.
|
|
|
|
|
pub fn mandatory_checkpoint_height(&self) -> Height {
|
2022-03-16 17:37:50 -07:00
|
|
|
|
// Currently this is after the ZIP-212 grace period.
|
|
|
|
|
//
|
2023-10-19 15:18:22 -07:00
|
|
|
|
// See the `ZIP_212_GRACE_PERIOD_DURATION` documentation for more information.
|
2022-03-16 17:37:50 -07:00
|
|
|
|
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
let canopy_activation = NetworkUpgrade::Canopy
|
2024-03-19 13:45:27 -07:00
|
|
|
|
.activation_height(self)
|
2022-03-16 17:37:50 -07:00
|
|
|
|
.expect("Canopy activation height must be present for both networks");
|
|
|
|
|
|
|
|
|
|
(canopy_activation + ZIP_212_GRACE_PERIOD_DURATION)
|
|
|
|
|
.expect("ZIP-212 grace period ends at a valid block height")
|
2021-06-17 17:05:28 -07:00
|
|
|
|
}
|
2022-03-25 05:25:31 -07:00
|
|
|
|
|
|
|
|
|
/// 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 {
|
2024-04-16 19:20:34 -07:00
|
|
|
|
self.kind().bip70_network_name()
|
2022-03-25 05:25:31 -07:00
|
|
|
|
}
|
2023-06-06 01:28:14 -07:00
|
|
|
|
|
|
|
|
|
/// Return the lowercase network name.
|
|
|
|
|
pub fn lowercase_name(&self) -> String {
|
|
|
|
|
self.to_string().to_ascii_lowercase()
|
|
|
|
|
}
|
2023-06-19 19:42:06 -07:00
|
|
|
|
|
|
|
|
|
/// Returns `true` if this network is a testing network.
|
|
|
|
|
pub fn is_a_test_network(&self) -> bool {
|
|
|
|
|
*self != Network::Mainnet
|
|
|
|
|
}
|
2024-02-09 07:23:19 -08:00
|
|
|
|
|
|
|
|
|
/// Returns the Sapling activation height for this network.
|
2024-03-19 13:45:27 -07:00
|
|
|
|
pub fn sapling_activation_height(&self) -> Height {
|
2024-02-09 07:23:19 -08:00
|
|
|
|
super::NetworkUpgrade::Sapling
|
|
|
|
|
.activation_height(self)
|
|
|
|
|
.expect("Sapling activation height needs to be set")
|
|
|
|
|
}
|
2020-09-04 10:19:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:20:34 -07:00
|
|
|
|
// This is used for parsing a command-line argument for the `TipHeight` command in zebrad.
|
2022-05-05 20:31:52 -07:00
|
|
|
|
impl FromStr for Network {
|
|
|
|
|
type Err = InvalidNetworkError;
|
|
|
|
|
|
|
|
|
|
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
match string.to_lowercase().as_str() {
|
|
|
|
|
"mainnet" => Ok(Network::Mainnet),
|
2024-04-16 19:20:34 -07:00
|
|
|
|
"testnet" => Ok(Network::new_default_testnet()),
|
2022-05-05 20:31:52 -07:00
|
|
|
|
_ => Err(InvalidNetworkError(string.to_owned())),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Error)]
|
|
|
|
|
#[error("Invalid network: {0}")]
|
|
|
|
|
pub struct InvalidNetworkError(String);
|
add(consensus): Add `activation_heights` field to `NetworkParameters` and implement `Parameters` for `Network` (#8383)
* minor cleanup and rename
* Adds an empty NetworkParameters struct to Network::Testnet variant, updates production code.
* Updates tests
* Adds `NetworkKind` and uses it instead of `Network` in `HistoryTreeParts` and `transparent::Address`
* Adds a [network.testnet_parameters] section to the config, uses `NetworkKind` as zebra_network::Config::network field type, and converts 'Network' to `NetworkKind` before serializing
* Applies some suggestions from code review
* Applies suggestions from code review
* returns b58 prefix constants directly to remove From<NetworkKind> impl for zcash_primitives::consensus::Network
* Applies more suggestions from code review.
* moves conversions to zcash_primitives::consensus::Network to where they're used.
* Apply suggestions from code review
Co-authored-by: Marek <mail@marek.onl>
* rename `network` variables and method names typed as NetworkKind to `network_kind`
* use only test block heights for the network associated with them
* Applies more suggestions from code review.
* Rename `NetworkParameters` to `Parameters` and move it a new `testnet` module
* adds activation heights field
* updates stored test config, adds a quicker test for checking that stored configs can be parsed, adds an intermediate representation of activation heights
* implement Parameters for Network
* Passes &Network directly instead of converting to zp_consensus::Network where there were conversions
* fixes a bad merge (removes a network conversion in zcash_note_encryption)
* Adds a test for the Parameters impl for zebra_chain::Network
* fixes doc links
* - Makes the `activation_heights` config field optional by adding a #[serde(default)]
- Panics if a non-zero activation height is provided for the `Genesis` network upgrade
- Always sets the `Genesis` and `BeforeOverwinter` network upgrade activation heights to 0 and 1, `BeforeOverwinter` could be overwritten by a later network upgrade
- Makes the `activation_heights` field on `Parameters` private, adds/uses an accessor method instead, and adds a builder struct and `build()` method
* small refactor of activation_heights() method
* check that activation heights are in the right order
* Updates `NetworkUpgrade::activation_height()` to return the height of the next NetworkUpgrade if it doesn't find the activation height of `&self`
* checks that the miner address is of TestnetKind on Regtest and update assertion message
* Adds a DNetworkUpgradeActivationHeights struct for better control over how activation heights can be configured
* moves all ordered network upgrades to a constant, adds a no_duplicates test, moves struct with activation heights outside deserialization impl and accepts it in `ParametersBuilder::activation_heights()` instead of a Vec
* panics if any network upgrades are configured to activate at Height(0) because it's reserved for Genesis
* Simplifies the `ParametersBuilder::activation_heights()` method and removes an unnecessary test.
* Adds Sapling HRPs as fields on testnet params. (#8398)
* Applies suggestions from code review.
* Update zebra-chain/src/parameters/network_upgrade.rs
Co-authored-by: Marek <mail@marek.onl>
---------
Co-authored-by: Marek <mail@marek.onl>
2024-04-18 18:20:34 -07:00
|
|
|
|
|
|
|
|
|
impl zp_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
|
|
|
|
|
.activation_height(self)
|
|
|
|
|
.map(|Height(h)| zp_consensus::BlockHeight::from_u32(h))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn coin_type(&self) -> u32 {
|
|
|
|
|
match self {
|
|
|
|
|
Network::Mainnet => zp_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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn address_network(&self) -> Option<zcash_address::Network> {
|
|
|
|
|
match self {
|
|
|
|
|
Network::Mainnet => Some(zcash_address::Network::Main),
|
|
|
|
|
// 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) => 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) => 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) => params.hrp_sapling_payment_address(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn b58_pubkey_address_prefix(&self) -> [u8; 2] {
|
|
|
|
|
self.kind().b58_pubkey_address_prefix()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn b58_script_address_prefix(&self) -> [u8; 2] {
|
|
|
|
|
self.kind().b58_script_address_prefix()
|
|
|
|
|
}
|
|
|
|
|
}
|