diff --git a/zebra-chain/src/parameters/network_upgrade.rs b/zebra-chain/src/parameters/network_upgrade.rs index d3db8f74..0734bb1c 100644 --- a/zebra-chain/src/parameters/network_upgrade.rs +++ b/zebra-chain/src/parameters/network_upgrade.rs @@ -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 { diff --git a/zebra-state/src/service.rs b/zebra-state/src/service.rs index 01ed6d8c..8470ebbd 100644 --- a/zebra-state/src/service.rs +++ b/zebra-state/src/service.rs @@ -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, diff --git a/zebra-state/src/service/check.rs b/zebra-state/src/service/check.rs index 5847ab4b..c718e31a 100644 --- a/zebra-state/src/service/check.rs +++ b/zebra-state/src/service/check.rs @@ -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`.