From 0bac2dafcc4a7e7aeae8ed1e140eecc84700f1f0 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 3 Dec 2020 13:58:12 +1000 Subject: [PATCH] Split out a separate `median_time_past` function --- zebra-state/src/service/check/difficulty.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/zebra-state/src/service/check/difficulty.rs b/zebra-state/src/service/check/difficulty.rs index b75ae7c8..94875450 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. ///