Implement threshold_bits
This commit is contained in:
parent
f0a49d64bf
commit
bb9c4918bf
|
|
@ -19,6 +19,8 @@ use std::{
|
||||||
fmt,
|
fmt,
|
||||||
iter::Sum,
|
iter::Sum,
|
||||||
ops::Add,
|
ops::Add,
|
||||||
|
ops::Div,
|
||||||
|
ops::Mul,
|
||||||
};
|
};
|
||||||
|
|
||||||
use primitive_types::U256;
|
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 {
|
impl PartialEq<block::Hash> for ExpandedDifficulty {
|
||||||
/// Is `self` equal to `other`?
|
/// Is `self` equal to `other`?
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -162,11 +162,19 @@ impl AdjustedDifficulty {
|
||||||
/// Implements `ThresholdBits` from the Zcash specification. (Which excludes the
|
/// Implements `ThresholdBits` from the Zcash specification. (Which excludes the
|
||||||
/// Testnet minimum difficulty adjustment.)
|
/// Testnet minimum difficulty adjustment.)
|
||||||
fn threshold_bits(&self) -> CompactDifficulty {
|
fn threshold_bits(&self) -> CompactDifficulty {
|
||||||
let mean_target = self.mean_target_difficulty();
|
let averaging_window_timespan = NetworkUpgrade::averaging_window_timespan_for_height(
|
||||||
let _median_timespan = self.median_timespan_bounded();
|
self.network,
|
||||||
|
self.candidate_height,
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: calculate the actual value
|
let threshold = (self.mean_target_difficulty() / averaging_window_timespan.num_seconds())
|
||||||
mean_target.to_compact()
|
* 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
|
/// Calculate the arithmetic mean of the averaging window thresholds: the
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue