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.
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
/// 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 {

View File

@ -6,7 +6,7 @@ use std::{
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 non_finalized_state::{NonFinalizedState, QueuedBlocks};
use tokio::sync::oneshot;
@ -15,6 +15,7 @@ use tracing::instrument;
use zebra_chain::{
block::{self, Block},
parameters::Network,
parameters::POW_AVERAGING_WINDOW,
transaction,
transaction::Transaction,
transparent,

View File

@ -5,6 +5,7 @@ use std::borrow::Borrow;
use zebra_chain::{
block::{self, Block},
parameters::Network,
parameters::POW_AVERAGING_WINDOW,
work::difficulty::CompactDifficulty,
};
@ -13,7 +14,7 @@ use crate::{PreparedBlock, ValidateContextError};
use super::check;
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
/// `finalized_tip_height` and `relevant_chain`.