Implement averaging_window_timespan

This commit is contained in:
teor 2020-11-19 11:55:34 +10:00
parent bcabf75fe9
commit 75519b0ae9
3 changed files with 26 additions and 2 deletions

View File

@ -104,6 +104,11 @@ const PRE_BLOSSOM_POW_TARGET_SPACING: i64 = 150;
/// The target block spacing after Blossom activation. /// The target block spacing after Blossom activation.
const POST_BLOSSOM_POW_TARGET_SPACING: i64 = 75; const POST_BLOSSOM_POW_TARGET_SPACING: i64 = 75;
/// The averaging window for difficulty threshold arithmetic mean calculations.
///
/// `PoWAveragingWindow` in the Zcash specification.
pub const POW_AVERAGING_WINDOW: usize = 17;
/// The multiplier used to derive the testnet minimum difficulty block time gap /// The multiplier used to derive the testnet minimum difficulty block time gap
/// threshold. /// threshold.
/// ///
@ -223,6 +228,23 @@ impl NetworkUpgrade {
} }
} }
} }
/// Returns the averaging window timespan for the network upgrade.
///
/// `AveragingWindowTimespan` from the Zcash specification.
pub fn averaging_window_timespan(&self) -> Duration {
self.target_spacing() * POW_AVERAGING_WINDOW as _
}
/// Returns the averaging window timespan for `network` and `height`.
///
/// See `averaging_window_timespan` for details.
pub fn averaging_window_timespan_for_height(
network: Network,
height: block::Height,
) -> Duration {
NetworkUpgrade::current(network, height).averaging_window_timespan()
}
} }
impl ConsensusBranchId { impl ConsensusBranchId {

View File

@ -6,7 +6,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use check::difficulty::{POW_AVERAGING_WINDOW, POW_MEDIAN_BLOCK_SPAN}; use check::difficulty::POW_MEDIAN_BLOCK_SPAN;
use futures::future::FutureExt; use futures::future::FutureExt;
use non_finalized_state::{NonFinalizedState, QueuedBlocks}; use non_finalized_state::{NonFinalizedState, QueuedBlocks};
use tokio::sync::oneshot; use tokio::sync::oneshot;
@ -15,6 +15,7 @@ use tracing::instrument;
use zebra_chain::{ use zebra_chain::{
block::{self, Block}, block::{self, Block},
parameters::Network, parameters::Network,
parameters::POW_AVERAGING_WINDOW,
transaction, transaction,
transaction::Transaction, transaction::Transaction,
transparent, transparent,

View File

@ -5,6 +5,7 @@ use std::borrow::Borrow;
use zebra_chain::{ use zebra_chain::{
block::{self, Block}, block::{self, Block},
parameters::Network, parameters::Network,
parameters::POW_AVERAGING_WINDOW,
work::difficulty::CompactDifficulty, work::difficulty::CompactDifficulty,
}; };
@ -13,7 +14,7 @@ use crate::{PreparedBlock, ValidateContextError};
use super::check; use super::check;
pub mod difficulty; pub mod difficulty;
use difficulty::{AdjustedDifficulty, POW_AVERAGING_WINDOW, POW_MEDIAN_BLOCK_SPAN}; use difficulty::{AdjustedDifficulty, POW_MEDIAN_BLOCK_SPAN};
/// Check that `block` is contextually valid for `network`, based on the /// Check that `block` is contextually valid for `network`, based on the
/// `finalized_tip_height` and `relevant_chain`. /// `finalized_tip_height` and `relevant_chain`.