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

feat(regtest): Add regtest halving interval and port test #8888

Merged
merged 17 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 14 additions & 2 deletions zebra-chain/src/parameters/network/subsidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ pub const POST_BLOSSOM_HALVING_INTERVAL: HeightDiff =
/// [7.10.1]: https://zips.z.cash/protocol/protocol.pdf#zip214fundingstreams
pub const FIRST_HALVING_TESTNET: Height = Height(1_116_000);

/// The first halving height in the regtest is at block height `114`
/// as [zcashd regtest halving interval](https://github.com/zcash/zcash/blob/v5.10.0/src/consensus/params.h#L252)
/// is 114 blocks.
pub const FIRST_HALVING_REGTEST: Height = Height(114);

/// The funding stream receiver categories.
#[derive(Deserialize, Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum FundingStreamReceiver {
Expand Down Expand Up @@ -391,8 +396,15 @@ impl ParameterSubsidy for Network {
Network::Mainnet => NetworkUpgrade::Canopy
.activation_height(self)
.expect("canopy activation height should be available"),
// TODO: Check what zcashd does here, consider adding a field to `testnet::Parameters` to make this configurable.
Network::Testnet(_params) => FIRST_HALVING_TESTNET,
Network::Testnet(params) => {
if params.is_regtest() {
FIRST_HALVING_REGTEST
} else if params.is_default_testnet() {
FIRST_HALVING_TESTNET
} else {
params.halving_interval()
}
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ pub struct ParametersBuilder {
target_difficulty_limit: ExpandedDifficulty,
/// A flag for disabling proof-of-work checks when Zebra is validating blocks
disable_pow: bool,
/// The halving interval for this network
halving_interval: Height,
}

impl Default for ParametersBuilder {
Expand Down Expand Up @@ -249,6 +251,7 @@ impl Default for ParametersBuilder {
disable_pow: false,
pre_nu6_funding_streams: PRE_NU6_FUNDING_STREAMS_TESTNET.clone(),
post_nu6_funding_streams: POST_NU6_FUNDING_STREAMS_TESTNET.clone(),
halving_interval: FIRST_HALVING_TESTNET,
}
}
}
Expand Down Expand Up @@ -411,6 +414,12 @@ impl ParametersBuilder {
self
}

/// Sets the halving interval to be used in the [`Parameters`] being built.
pub fn with_halving_interval(mut self, halving_interval: Height) -> Self {
self.halving_interval = halving_interval;
self
}

/// Converts the builder to a [`Parameters`] struct
pub fn finish(self) -> Parameters {
let Self {
Expand All @@ -423,6 +432,7 @@ impl ParametersBuilder {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
} = self;
Parameters {
network_name,
Expand All @@ -435,6 +445,7 @@ impl ParametersBuilder {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
}
}

Expand All @@ -455,6 +466,7 @@ impl ParametersBuilder {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
} = Self::default();

self.activation_heights == activation_heights
Expand All @@ -465,6 +477,7 @@ impl ParametersBuilder {
&& self.post_nu6_funding_streams == post_nu6_funding_streams
&& self.target_difficulty_limit == target_difficulty_limit
&& self.disable_pow == disable_pow
&& self.halving_interval == halving_interval
}
}

Expand Down Expand Up @@ -495,6 +508,8 @@ pub struct Parameters {
target_difficulty_limit: ExpandedDifficulty,
/// A flag for disabling proof-of-work checks when Zebra is validating blocks
disable_pow: bool,
/// The halving interval for this network
halving_interval: Height,
}

impl Default for Parameters {
Expand Down Expand Up @@ -568,6 +583,7 @@ impl Parameters {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
} = Self::new_regtest(None, None);

self.network_name == network_name
Expand All @@ -578,6 +594,7 @@ impl Parameters {
&& self.post_nu6_funding_streams == post_nu6_funding_streams
&& self.target_difficulty_limit == target_difficulty_limit
&& self.disable_pow == disable_pow
&& self.halving_interval == halving_interval
}

/// Returns the network name
Expand Down Expand Up @@ -629,6 +646,11 @@ impl Parameters {
pub fn disable_pow(&self) -> bool {
self.disable_pow
}

/// Returns the halving interval for this network
pub fn halving_interval(&self) -> Height {
self.halving_interval
}
}

impl Network {
Expand Down
1 change: 1 addition & 0 deletions zebra-chain/src/parameters/network_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum NetworkUpgrade {
#[serde(rename = "NU5")]
Nu5,
/// The Zcash protocol after the NU6 upgrade.
#[serde(rename = "NU6")]
Nu6,
}

Expand Down
8 changes: 8 additions & 0 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ impl<'de> Deserialize<'de> for Config {
activation_heights: Option<ConfiguredActivationHeights>,
pre_nu6_funding_streams: Option<ConfiguredFundingStreams>,
post_nu6_funding_streams: Option<ConfiguredFundingStreams>,
halving_interval: Option<u32>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -686,6 +687,7 @@ impl<'de> Deserialize<'de> for Config {
activation_heights,
pre_nu6_funding_streams,
post_nu6_funding_streams,
halving_interval,
}),
) => {
let mut params_builder = testnet::Parameters::build();
Expand Down Expand Up @@ -733,6 +735,12 @@ impl<'de> Deserialize<'de> for Config {
params_builder = params_builder.with_activation_heights(activation_heights)
}

if let Some(halving_interval) = halving_interval {
params_builder = params_builder.with_halving_interval(
halving_interval.try_into().map_err(de::Error::custom)?,
)
}

// Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet
// peers and the configured network parameters are incompatible with the default public Testnet.
if !params_builder.is_compatible_with_default_parameters()
Expand Down
4 changes: 4 additions & 0 deletions zebra-rpc/qa/base_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ listen_addr = "127.0.0.1:0"

[state]
cache_dir = ""

[network.testnet_parameters.activation_heights]
NU5 = 150
NU6 = 151
3 changes: 2 additions & 1 deletion zebra-rpc/qa/pull-tester/rpc-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
# Scripts that are run by the travis build process
# Longest test should go first, to favor running tests in parallel
'reindex.py',
'getmininginfo.py']
'getmininginfo.py',
'nuparams.py']

ZMQ_SCRIPTS = [
# ZMQ test can only be run if bitcoin was built with zmq-enabled.
Expand Down
Loading
Loading