Split out a separate `median_time_past` function

This commit is contained in:
teor 2020-12-03 13:58:12 +10:00
parent ab486d336f
commit 0bac2dafcc
1 changed files with 16 additions and 5 deletions

View File

@ -271,11 +271,7 @@ impl AdjustedDifficulty {
/// ///
/// See [`median_timespan_bounded()`] for details. /// See [`median_timespan_bounded()`] for details.
fn median_timespan(&self) -> Duration { fn median_timespan(&self) -> Duration {
let newer_times: [DateTime<Utc>; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times let newer_median = self.median_time_past();
[0..POW_MEDIAN_BLOCK_SPAN]
.try_into()
.expect("relevant times is the correct length");
let newer_median = AdjustedDifficulty::median_time(newer_times);
let older_times: [DateTime<Utc>; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times let older_times: [DateTime<Utc>; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times
[POW_AVERAGING_WINDOW..] [POW_AVERAGING_WINDOW..]
@ -287,6 +283,21 @@ impl AdjustedDifficulty {
newer_median - older_median newer_median - older_median
} }
/// Calculate the median of the `time`s from the previous
/// `PoWMedianBlockSpan` (11) blocks in the relevant chain.
///
/// Implements `median-time-past` and `MedianTime(candidate_height)` from the
/// Zcash specification. (These functions are identical, but they are
/// specified in slightly different ways.)
pub fn median_time_past(&self) -> DateTime<Utc> {
let median_times: [DateTime<Utc>; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times
[0..POW_MEDIAN_BLOCK_SPAN]
.try_into()
.expect("relevant times is the correct length");
AdjustedDifficulty::median_time(median_times)
}
/// Calculate the median of the `median_block_span_times`: the `time`s from a /// Calculate the median of the `median_block_span_times`: the `time`s from a
/// slice of `PoWMedianBlockSpan` (11) blocks in the relevant chain. /// slice of `PoWMedianBlockSpan` (11) blocks in the relevant chain.
/// ///