From bb9c4918bf9fed3ff9adccda1bd3d6c3fc5a138f Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 19 Nov 2020 12:24:47 +1000 Subject: [PATCH] Implement threshold_bits --- zebra-chain/src/work/difficulty.rs | 25 +++++++++++++++++++++ zebra-state/src/service/check/difficulty.rs | 16 +++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/zebra-chain/src/work/difficulty.rs b/zebra-chain/src/work/difficulty.rs index 1057e13f2..e6303cee8 100644 --- a/zebra-chain/src/work/difficulty.rs +++ b/zebra-chain/src/work/difficulty.rs @@ -19,6 +19,8 @@ use std::{ fmt, iter::Sum, ops::Add, + ops::Div, + ops::Mul, }; use primitive_types::U256; @@ -399,6 +401,29 @@ impl Sum for ExpandedDifficulty { } } +impl Div for ExpandedDifficulty +where + T: Into, +{ + type Output = ExpandedDifficulty; + + fn div(self, rhs: T) -> Self::Output { + ExpandedDifficulty(self.0 / rhs) + } +} + +impl Mul for ExpandedDifficulty +where + U256: Mul, + >::Output: Into, +{ + type Output = ExpandedDifficulty; + + fn mul(self, rhs: T) -> ExpandedDifficulty { + ExpandedDifficulty((self.0 * rhs).into()) + } +} + impl PartialEq for ExpandedDifficulty { /// Is `self` equal to `other`? /// diff --git a/zebra-state/src/service/check/difficulty.rs b/zebra-state/src/service/check/difficulty.rs index 5bb622cc3..1a5cd2a79 100644 --- a/zebra-state/src/service/check/difficulty.rs +++ b/zebra-state/src/service/check/difficulty.rs @@ -162,11 +162,19 @@ impl AdjustedDifficulty { /// Implements `ThresholdBits` from the Zcash specification. (Which excludes the /// Testnet minimum difficulty adjustment.) fn threshold_bits(&self) -> CompactDifficulty { - let mean_target = self.mean_target_difficulty(); - let _median_timespan = self.median_timespan_bounded(); + let averaging_window_timespan = NetworkUpgrade::averaging_window_timespan_for_height( + self.network, + self.candidate_height, + ); - // TODO: calculate the actual value - mean_target.to_compact() + let threshold = (self.mean_target_difficulty() / averaging_window_timespan.num_seconds()) + * self.median_timespan_bounded().num_seconds(); + let threshold = min( + ExpandedDifficulty::target_difficulty_limit(self.network), + threshold, + ); + + threshold.to_compact() } /// Calculate the arithmetic mean of the averaging window thresholds: the