Move height check to NetworkUpgrade (#1505)
* move height check to NetworkUpgrade * refactor the max block time height check Co-authored-by: teor <teor@riseup.net>
This commit is contained in:
parent
7468dae88d
commit
436ce00fc2
|
@ -118,6 +118,12 @@ const TESTNET_MINIMUM_DIFFICULTY_GAP_MULTIPLIER: i32 = 6;
|
||||||
/// Based on https://zips.z.cash/zip-0208#minimum-difficulty-blocks-on-the-test-network
|
/// Based on https://zips.z.cash/zip-0208#minimum-difficulty-blocks-on-the-test-network
|
||||||
const TESTNET_MINIMUM_DIFFICULTY_START_HEIGHT: block::Height = block::Height(299_188);
|
const TESTNET_MINIMUM_DIFFICULTY_START_HEIGHT: block::Height = block::Height(299_188);
|
||||||
|
|
||||||
|
/// The activation height for the block maximum time rule on Testnet.
|
||||||
|
///
|
||||||
|
/// Part of the block header consensus rules in the Zcash specification at
|
||||||
|
/// https://zips.z.cash/protocol/protocol.pdf#blockheader
|
||||||
|
pub const TESTNET_MAX_TIME_START_HEIGHT: block::Height = block::Height(653_606);
|
||||||
|
|
||||||
impl NetworkUpgrade {
|
impl NetworkUpgrade {
|
||||||
/// Returns a BTreeMap of activation heights and network upgrades for
|
/// Returns a BTreeMap of activation heights and network upgrades for
|
||||||
/// `network`.
|
/// `network`.
|
||||||
|
@ -274,6 +280,21 @@ impl NetworkUpgrade {
|
||||||
) -> Duration {
|
) -> Duration {
|
||||||
NetworkUpgrade::current(network, height).averaging_window_timespan()
|
NetworkUpgrade::current(network, height).averaging_window_timespan()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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(network: Network, height: block::Height) -> bool {
|
||||||
|
match network {
|
||||||
|
Network::Mainnet => true,
|
||||||
|
Network::Testnet => height >= TESTNET_MAX_TIME_START_HEIGHT,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConsensusBranchId {
|
impl ConsensusBranchId {
|
||||||
|
|
|
@ -5,8 +5,8 @@ use std::borrow::Borrow;
|
||||||
use chrono::Duration;
|
use chrono::Duration;
|
||||||
use zebra_chain::{
|
use zebra_chain::{
|
||||||
block::{self, Block},
|
block::{self, Block},
|
||||||
parameters::Network,
|
|
||||||
parameters::POW_AVERAGING_WINDOW,
|
parameters::POW_AVERAGING_WINDOW,
|
||||||
|
parameters::{Network, NetworkUpgrade},
|
||||||
work::difficulty::CompactDifficulty,
|
work::difficulty::CompactDifficulty,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use crate::{PreparedBlock, ValidateContextError};
|
||||||
|
|
||||||
use super::check;
|
use super::check;
|
||||||
|
|
||||||
use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN, TESTNET_MAX_TIME_START_HEIGHT};
|
use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN};
|
||||||
|
|
||||||
pub(crate) mod difficulty;
|
pub(crate) mod difficulty;
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ fn difficulty_threshold_is_valid(
|
||||||
}
|
}
|
||||||
|
|
||||||
// The maximum time rule is only active on Testnet from a specific height
|
// The maximum time rule is only active on Testnet from a specific height
|
||||||
if (network == Network::Mainnet || candidate_height >= TESTNET_MAX_TIME_START_HEIGHT)
|
if NetworkUpgrade::is_max_block_time_enforced(network, candidate_height)
|
||||||
&& candidate_time > block_time_max
|
&& candidate_time > block_time_max
|
||||||
{
|
{
|
||||||
Err(ValidateContextError::TimeTooLate {
|
Err(ValidateContextError::TimeTooLate {
|
||||||
|
|
|
@ -42,11 +42,6 @@ pub const POW_MAX_ADJUST_DOWN_PERCENT: i32 = 32;
|
||||||
/// Part of the block header consensus rules in the Zcash specification.
|
/// Part of the block header consensus rules in the Zcash specification.
|
||||||
pub const BLOCK_MAX_TIME_SINCE_MEDIAN: i64 = 90 * 60;
|
pub const BLOCK_MAX_TIME_SINCE_MEDIAN: i64 = 90 * 60;
|
||||||
|
|
||||||
/// The activation height for the block maximum time rule on Testnet.
|
|
||||||
///
|
|
||||||
/// Part of the block header consensus rules in the Zcash specification.
|
|
||||||
pub const TESTNET_MAX_TIME_START_HEIGHT: block::Height = block::Height(653_606);
|
|
||||||
|
|
||||||
/// Contains the context needed to calculate the adjusted difficulty for a block.
|
/// Contains the context needed to calculate the adjusted difficulty for a block.
|
||||||
pub(super) struct AdjustedDifficulty {
|
pub(super) struct AdjustedDifficulty {
|
||||||
/// The `header.time` field from the candidate block
|
/// The `header.time` field from the candidate block
|
||||||
|
|
Loading…
Reference in New Issue