adds activation heights field

This commit is contained in:
Arya 2024-04-02 19:29:00 -04:00
parent eca7f6b677
commit 60248e3f1f
4 changed files with 48 additions and 34 deletions

View File

@ -2,7 +2,7 @@
use proptest::prelude::*;
use super::NetworkUpgrade;
use super::{Network, NetworkUpgrade};
impl NetworkUpgrade {
/// Generates network upgrades.
@ -32,3 +32,13 @@ impl NetworkUpgrade {
.boxed()
}
}
impl Arbitrary for Network {
type Parameters = ();
fn arbitrary_with(_args: ()) -> Self::Strategy {
prop_oneof![Just(Self::Mainnet), Just(Self::new_default_testnet())].boxed()
}
type Strategy = BoxedStrategy<Self>;
}

View File

@ -8,14 +8,11 @@ use zcash_primitives::constants;
use crate::{
block::{self, Height, HeightDiff},
parameters::NetworkUpgrade::Canopy,
parameters::NetworkUpgrade,
};
pub mod testnet;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
#[cfg(test)]
mod tests;
@ -81,7 +78,6 @@ impl From<Network> for NetworkKind {
/// An enum describing the possible network choices.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
#[serde(into = "NetworkKind")]
pub enum Network {
/// The production mainnet.
@ -231,7 +227,7 @@ impl Network {
//
// See the `ZIP_212_GRACE_PERIOD_DURATION` documentation for more information.
let canopy_activation = Canopy
let canopy_activation = NetworkUpgrade::Canopy
.activation_height(self)
.expect("Canopy activation height must be present for both networks");

View File

@ -1,15 +1,33 @@
//! Types and implementation for Testnet consensus parameters
use std::collections::BTreeMap;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
use crate::{
block::Height,
parameters::{network_upgrade::TESTNET_ACTIVATION_HEIGHTS, NetworkUpgrade},
};
/// Network consensus parameters for test networks such as Regtest and the default Testnet.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct Parameters {}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Parameters {
/// The network upgrade activation heights for this network.
///
/// Note: This value is ignored by [`Network::activation_list()`] when `zebra-chain` is
/// compiled with the `zebra-test` feature flag AND the `TEST_FAKE_ACTIVATION_HEIGHTS`
/// environment variable is set.
pub activation_heights: BTreeMap<Height, NetworkUpgrade>,
}
impl Default for Parameters {
/// Returns an instance of the default public testnet [`NetworkParameters`].
fn default() -> Self {
Self {
activation_heights: TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
}
}
}
impl Parameters {
/// Returns true if the instance of [`Parameters`] represents the default public Testnet.
/// Returns true if the instance of [`NetworkParameters`] represents the default public Testnet.
pub fn is_default_testnet(&self) -> bool {
self == &Self::default()
}

View File

@ -242,12 +242,7 @@ impl Network {
/// and it's a test build, this returns a list of fake activation heights
/// used by some tests.
pub fn activation_list(&self) -> BTreeMap<block::Height, NetworkUpgrade> {
let (mainnet_heights, testnet_heights) = {
#[cfg(not(feature = "zebra-test"))]
{
(MAINNET_ACTIVATION_HEIGHTS, TESTNET_ACTIVATION_HEIGHTS)
}
match self {
// To prevent accidentally setting this somehow, only check the env var
// when being compiled for tests. We can't use cfg(test) since the
// test that uses this is in zebra-state, and cfg(test) is not
@ -260,25 +255,20 @@ impl Network {
// feature should only be enabled for tests:
// https://doc.rust-lang.org/cargo/reference/features.html#resolver-version-2-command-line-flags
#[cfg(feature = "zebra-test")]
if std::env::var_os("TEST_FAKE_ACTIVATION_HEIGHTS").is_some() {
(
FAKE_MAINNET_ACTIVATION_HEIGHTS,
FAKE_TESTNET_ACTIVATION_HEIGHTS,
)
} else {
(MAINNET_ACTIVATION_HEIGHTS, TESTNET_ACTIVATION_HEIGHTS)
Mainnet if std::env::var_os("TEST_FAKE_ACTIVATION_HEIGHTS").is_some() => {
FAKE_MAINNET_ACTIVATION_HEIGHTS.iter().cloned().collect()
}
};
match self {
Mainnet => mainnet_heights,
// TODO: Add an `activation_heights` field to `testnet::Parameters` to return here. (#7970)
Testnet(_params) => testnet_heights,
#[cfg(feature = "zebra-test")]
Testnet(_) if std::env::var_os("TEST_FAKE_ACTIVATION_HEIGHTS").is_some() => {
FAKE_TESTNET_ACTIVATION_HEIGHTS.iter().cloned().collect()
}
Mainnet => MAINNET_ACTIVATION_HEIGHTS.iter().cloned().collect(),
Testnet(params) => params.activation_heights.clone(),
}
.iter()
.cloned()
.collect()
}
}
impl NetworkUpgrade {
/// Returns the current network upgrade for `network` and `height`.
pub fn current(network: &Network, height: block::Height) -> NetworkUpgrade {