diff --git a/zebra-state/src/service/check/difficulty.rs b/zebra-state/src/service/check/difficulty.rs index b75ae7c84..94875450d 100644 --- a/zebra-state/src/service/check/difficulty.rs +++ b/zebra-state/src/service/check/difficulty.rs @@ -271,11 +271,7 @@ impl AdjustedDifficulty { /// /// See [`median_timespan_bounded()`] for details. fn median_timespan(&self) -> Duration { - let newer_times: [DateTime; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times - [0..POW_MEDIAN_BLOCK_SPAN] - .try_into() - .expect("relevant times is the correct length"); - let newer_median = AdjustedDifficulty::median_time(newer_times); + let newer_median = self.median_time_past(); let older_times: [DateTime; POW_MEDIAN_BLOCK_SPAN] = self.relevant_times [POW_AVERAGING_WINDOW..] @@ -287,6 +283,21 @@ impl AdjustedDifficulty { 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 { + let median_times: [DateTime; 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 /// slice of `PoWMedianBlockSpan` (11) blocks in the relevant chain. ///