Implement threshold_bits

This commit is contained in:
teor 2020-11-19 12:24:47 +10:00
parent f0a49d64bf
commit bb9c4918bf
2 changed files with 37 additions and 4 deletions

View File

@ -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<ExpandedDifficulty> for ExpandedDifficulty {
}
}
impl<T> Div<T> for ExpandedDifficulty
where
T: Into<U256>,
{
type Output = ExpandedDifficulty;
fn div(self, rhs: T) -> Self::Output {
ExpandedDifficulty(self.0 / rhs)
}
}
impl<T> Mul<T> for ExpandedDifficulty
where
U256: Mul<T>,
<U256 as Mul<T>>::Output: Into<U256>,
{
type Output = ExpandedDifficulty;
fn mul(self, rhs: T) -> ExpandedDifficulty {
ExpandedDifficulty((self.0 * rhs).into())
}
}
impl PartialEq<block::Hash> for ExpandedDifficulty {
/// Is `self` equal to `other`?
///

View File

@ -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