Skip to content

Commit

Permalink
make fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Sep 25, 2024
1 parent a3d438c commit d6437f4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 49 deletions.
47 changes: 41 additions & 6 deletions zebra-chain/src/parameters/network/subsidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,26 @@ pub const BLOSSOM_POW_TARGET_SPACING_RATIO: u32 = 2;
/// `(60 * 60 * 24 * 365 * 4) / 150 = 840960`
pub const PRE_BLOSSOM_HALVING_INTERVAL: HeightDiff = 840_000;

/// The halving height interval in the regtest is 6 hours.
/// [zcashd regtest halving interval](https://github.com/zcash/zcash/blob/v5.10.0/src/consensus/params.h#L252)
pub const PRE_BLOSSOM_REGTEST_HALVING_INTERVAL: HeightDiff = 144;

/// After Blossom the block time is reduced to 75 seconds but halving period should remain around 4 years.
pub const POST_BLOSSOM_HALVING_INTERVAL: HeightDiff =
PRE_BLOSSOM_HALVING_INTERVAL * (BLOSSOM_POW_TARGET_SPACING_RATIO as HeightDiff);

/// After Blossom the block time is reduced to 75 seconds but halving period should remain 6 hours.
pub const POST_BLOSSOM_REGTEST_HALVING_INTERVAL: HeightDiff =
PRE_BLOSSOM_REGTEST_HALVING_INTERVAL * (BLOSSOM_POW_TARGET_SPACING_RATIO as HeightDiff);

/// The first halving height in the testnet is at block height `1_116_000`
/// as specified in [protocol specification §7.10.1][7.10.1]
///
/// [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 first halving height in the regtest is at block height `287`.
pub const FIRST_HALVING_REGTEST: Height = Height(287);

/// The funding stream receiver categories.
#[derive(Deserialize, Clone, Copy, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -384,6 +390,12 @@ pub trait ParameterSubsidy {
///
/// [7.10]: <https://zips.z.cash/protocol/protocol.pdf#fundingstreams>
fn height_for_first_halving(&self) -> Height;

/// Returns the halving interval after Blossom
fn post_blossom_halving_interval(&self) -> HeightDiff;

/// Returns the halving interval before Blossom
fn pre_blossom_halving_interval(&self) -> HeightDiff;
}

/// Network methods related to Block Subsidy and Funding Streams
Expand All @@ -402,7 +414,30 @@ impl ParameterSubsidy for Network {
} else if params.is_default_testnet() {
FIRST_HALVING_TESTNET
} else {
params.halving_interval()
Height(
self.post_blossom_halving_interval()
.try_into()
.expect("post blossom halving interval should fit in a Height"),
)
}
}
}
}

fn post_blossom_halving_interval(&self) -> HeightDiff {
self.pre_blossom_halving_interval() * (BLOSSOM_POW_TARGET_SPACING_RATIO as HeightDiff)
}

fn pre_blossom_halving_interval(&self) -> HeightDiff {
match self {
Network::Mainnet => PRE_BLOSSOM_HALVING_INTERVAL,
Network::Testnet(params) => {
if params.is_regtest() {
PRE_BLOSSOM_REGTEST_HALVING_INTERVAL
} else if params.is_default_testnet() {
PRE_BLOSSOM_HALVING_INTERVAL
} else {
params.pre_blossom_halving_interval()
}
}
}
Expand Down Expand Up @@ -515,7 +550,7 @@ pub fn funding_stream_address_period<N: ParameterSubsidy>(height: Height, networ

let height_after_first_halving = height - network.height_for_first_halving();

let address_period = (height_after_first_halving + POST_BLOSSOM_HALVING_INTERVAL)
let address_period = (height_after_first_halving + network.post_blossom_halving_interval())
/ FUNDING_STREAM_ADDRESS_CHANGE_INTERVAL;

address_period
Expand Down
47 changes: 30 additions & 17 deletions zebra-chain/src/parameters/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{collections::BTreeMap, fmt};

use crate::{
block::{self, Height},
block::{self, Height, HeightDiff},
parameters::{
constants::{magics, SLOW_START_INTERVAL, SLOW_START_SHIFT},
network_upgrade::TESTNET_ACTIVATION_HEIGHTS,
Expand All @@ -16,8 +16,10 @@ use super::{
magic::Magic,
subsidy::{
FundingStreamReceiver, FundingStreamRecipient, FundingStreams, ParameterSubsidy,
FIRST_HALVING_TESTNET, POST_NU6_FUNDING_STREAMS_MAINNET, POST_NU6_FUNDING_STREAMS_TESTNET,
PRE_NU6_FUNDING_STREAMS_MAINNET, PRE_NU6_FUNDING_STREAMS_TESTNET,
FIRST_HALVING_TESTNET, POST_BLOSSOM_HALVING_INTERVAL, POST_NU6_FUNDING_STREAMS_MAINNET,
POST_NU6_FUNDING_STREAMS_TESTNET, PRE_BLOSSOM_HALVING_INTERVAL,
PRE_BLOSSOM_REGTEST_HALVING_INTERVAL, PRE_NU6_FUNDING_STREAMS_MAINNET,
PRE_NU6_FUNDING_STREAMS_TESTNET,
},
};

Expand Down Expand Up @@ -57,6 +59,13 @@ impl ParameterSubsidy for TestnetParameterSubsidyImpl {
fn height_for_first_halving(&self) -> Height {
FIRST_HALVING_TESTNET
}
fn post_blossom_halving_interval(&self) -> block::HeightDiff {
POST_BLOSSOM_HALVING_INTERVAL
}

fn pre_blossom_halving_interval(&self) -> block::HeightDiff {
PRE_BLOSSOM_HALVING_INTERVAL
}
}

/// Configurable funding stream recipient for configured Testnets.
Expand Down Expand Up @@ -217,8 +226,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,
/// The pre-Blossom halving interval for this network
pre_blossom_halving_interval: HeightDiff,
}

impl Default for ParametersBuilder {
Expand Down Expand Up @@ -251,7 +260,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,
pre_blossom_halving_interval: PRE_BLOSSOM_HALVING_INTERVAL,
}
}
}
Expand Down Expand Up @@ -415,8 +424,11 @@ impl ParametersBuilder {
}

/// 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;
pub fn with_pre_blossom_halving_interval(
mut self,
pre_blossom_halving_interval: HeightDiff,
) -> Self {
self.pre_blossom_halving_interval = pre_blossom_halving_interval;
self
}

Expand All @@ -432,7 +444,7 @@ impl ParametersBuilder {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
pre_blossom_halving_interval,
} = self;
Parameters {
network_name,
Expand All @@ -445,7 +457,7 @@ impl ParametersBuilder {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
pre_blossom_halving_interval,
}
}

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

self.activation_heights == activation_heights
Expand All @@ -477,7 +489,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
&& self.pre_blossom_halving_interval == pre_blossom_halving_interval
}
}

Expand Down Expand Up @@ -509,7 +521,7 @@ pub struct Parameters {
/// 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,
pre_blossom_halving_interval: HeightDiff,
}

impl Default for Parameters {
Expand Down Expand Up @@ -555,6 +567,7 @@ impl Parameters {
nu6: nu6_activation_height,
..Default::default()
})
.with_pre_blossom_halving_interval(PRE_BLOSSOM_REGTEST_HALVING_INTERVAL)
.finish()
}
}
Expand Down Expand Up @@ -583,7 +596,7 @@ impl Parameters {
post_nu6_funding_streams,
target_difficulty_limit,
disable_pow,
halving_interval,
pre_blossom_halving_interval,
} = Self::new_regtest(None, None);

self.network_name == network_name
Expand All @@ -594,7 +607,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
&& self.pre_blossom_halving_interval == pre_blossom_halving_interval
}

/// Returns the network name
Expand Down Expand Up @@ -648,8 +661,8 @@ impl Parameters {
}

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

Expand Down
6 changes: 3 additions & 3 deletions zebra-consensus/src/block/subsidy/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn halving_divisor(height: Height, network: &Network) -> Option<u64> {

if height < blossom_height {
let pre_blossom_height = height - network.slow_start_shift();
let halving_shift = pre_blossom_height / PRE_BLOSSOM_HALVING_INTERVAL;
let halving_shift = pre_blossom_height / network.pre_blossom_halving_interval();

let halving_div = 1u64
.checked_shl(
Expand All @@ -47,8 +47,8 @@ pub fn halving_divisor(height: Height, network: &Network) -> Option<u64> {

let post_blossom_height = height - blossom_height;

let halving_shift =
(scaled_pre_blossom_height + post_blossom_height) / POST_BLOSSOM_HALVING_INTERVAL;
let halving_shift = (scaled_pre_blossom_height + post_blossom_height)
/ network.post_blossom_halving_interval();

// Some far-future shifts can be more than 63 bits
1u64.checked_shl(
Expand Down
5 changes: 2 additions & 3 deletions zebra-network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,8 @@ impl<'de> Deserialize<'de> for Config {
}

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

// Return an error if the initial testnet peers includes any of the default initial Mainnet or Testnet
Expand Down
4 changes: 2 additions & 2 deletions zebra-rpc/qa/base_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ listen_addr = "127.0.0.1:0"
cache_dir = ""

[network.testnet_parameters.activation_heights]
NU5 = 150
NU6 = 151
NU5 = 290
NU6 = 291
36 changes: 18 additions & 18 deletions zebra-rpc/qa/rpc-tests/nuparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def run_test(self):

nu5 = upgrades[nustr(NU5_BRANCH_ID)]
assert_equal(nu5['name'], 'NU5')
assert_equal(nu5['activationheight'], 150)
assert_equal(nu5['activationheight'], 290)
assert_equal(nu5['status'], 'pending')

nu6 = upgrades[nustr(NU6_BRANCH_ID)]
assert_equal(nu6['name'], 'NU6')
assert_equal(nu6['activationheight'], 151)
assert_equal(nu6['activationheight'], 291)
assert_equal(nu6['status'], 'pending')

# Zebra can't call `getblocksubsidy` before the first halving.
Expand Down Expand Up @@ -118,20 +118,20 @@ def run_test(self):

nu5 = upgrades[nustr(NU5_BRANCH_ID)]
assert_equal(nu5['name'], 'NU5')
assert_equal(nu5['activationheight'], 150)
assert_equal(nu5['activationheight'], 290)
assert_equal(nu5['status'], 'pending')

nu6 = upgrades[nustr(NU6_BRANCH_ID)]
assert_equal(nu6['name'], 'NU6')
assert_equal(nu6['activationheight'], 151)
assert_equal(nu6['activationheight'], 291)
assert_equal(nu6['status'], 'pending')

# Zebra can't call `getblocksubsidy` before the first halving.

# Activate First Halving
node.generate(144)
node.generate(287)
bci = node.getblockchaininfo()
assert_equal(bci['blocks'], 145)
assert_equal(bci['blocks'], 288)
upgrades = bci['upgrades']

overwinter = upgrades[nustr(OVERWINTER_BRANCH_ID)]
Expand Down Expand Up @@ -161,22 +161,22 @@ def run_test(self):

nu5 = upgrades[nustr(NU5_BRANCH_ID)]
assert_equal(nu5['name'], 'NU5')
assert_equal(nu5['activationheight'], 150)
assert_equal(nu5['activationheight'], 290)
assert_equal(nu5['status'], 'pending')

nu6 = upgrades[nustr(NU6_BRANCH_ID)]
assert_equal(nu6['name'], 'NU6')
assert_equal(nu6['activationheight'], 151)
assert_equal(nu6['activationheight'], 291)
assert_equal(nu6['status'], 'pending')

# The founders' reward ends at Canopy and there are no funding streams
# configured by default for regtest.
assert_equal(node.getblocksubsidy()["miner"], Decimal("6.25"))
assert_equal(node.getblocksubsidy()["miner"], Decimal("3.125"))

# Activate NU5
node.generate(5)
node.generate(2)
bci = node.getblockchaininfo()
assert_equal(bci['blocks'], 150)
assert_equal(bci['blocks'], 290)
upgrades = bci['upgrades']

overwinter = upgrades[nustr(OVERWINTER_BRANCH_ID)]
Expand Down Expand Up @@ -206,21 +206,21 @@ def run_test(self):

nu5 = upgrades[nustr(NU5_BRANCH_ID)]
assert_equal(nu5['name'], 'NU5')
assert_equal(nu5['activationheight'], 150)
assert_equal(nu5['activationheight'], 290)
assert_equal(nu5['status'], 'active')

nu6 = upgrades[nustr(NU6_BRANCH_ID)]
assert_equal(nu6['name'], 'NU6')
assert_equal(nu6['activationheight'], 151)
assert_equal(nu6['activationheight'], 291)
assert_equal(nu6['status'], 'pending')

# Block subsidy remains the same after NU5
assert_equal(node.getblocksubsidy()["miner"], Decimal("6.25"))
assert_equal(node.getblocksubsidy()["miner"], Decimal("3.125"))

# Activate NU6
node.generate(1)
bci = node.getblockchaininfo()
assert_equal(bci['blocks'], 151)
assert_equal(bci['blocks'], 291)
upgrades = bci['upgrades']

overwinter = upgrades[nustr(OVERWINTER_BRANCH_ID)]
Expand Down Expand Up @@ -250,17 +250,17 @@ def run_test(self):

nu5 = upgrades[nustr(NU5_BRANCH_ID)]
assert_equal(nu5['name'], 'NU5')
assert_equal(nu5['activationheight'], 150)
assert_equal(nu5['activationheight'], 290)
assert_equal(nu5['status'], 'active')

nu6 = upgrades[nustr(NU6_BRANCH_ID)]
assert_equal(nu6['name'], 'NU6')
assert_equal(nu6['activationheight'], 151)
assert_equal(nu6['activationheight'], 291)
assert_equal(nu6['status'], 'active')

# Block subsidy remains the same after NU6 as there are not funding streams
# nor lockbox configured by default for regtest.
assert_equal(node.getblocksubsidy()["miner"], Decimal("6.25"))
assert_equal(node.getblocksubsidy()["miner"], Decimal("3.125"))

if __name__ == '__main__':
NuparamsTest().main()

0 comments on commit d6437f4

Please sign in to comment.