fix(consensus): Update `median_timespan()` method to align with zcashd implementation (#8491)
* Updates `median_timespan()` method to align with zcashd implementation of ActualTimespan * Minor refactor --------- Co-authored-by: Alfredo Garcia <oxarbitrage@gmail.com>
This commit is contained in:
parent
3e264c73a6
commit
0040c2be87
|
@ -295,15 +295,23 @@ impl AdjustedDifficulty {
|
||||||
fn median_timespan(&self) -> Duration {
|
fn median_timespan(&self) -> Duration {
|
||||||
let newer_median = self.median_time_past();
|
let newer_median = self.median_time_past();
|
||||||
|
|
||||||
|
// MedianTime(height : N) := median([ nTime(𝑖) for 𝑖 from max(0, height − PoWMedianBlockSpan) up to max(0, height − 1) ])
|
||||||
|
let older_median = if self.relevant_times.len() > POW_AVERAGING_WINDOW {
|
||||||
let older_times: Vec<_> = self
|
let older_times: Vec<_> = self
|
||||||
.relevant_times
|
.relevant_times
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.skip(POW_AVERAGING_WINDOW)
|
||||||
.cloned()
|
.cloned()
|
||||||
.take(POW_MEDIAN_BLOCK_SPAN)
|
.take(POW_MEDIAN_BLOCK_SPAN)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let older_median = AdjustedDifficulty::median_time(older_times);
|
AdjustedDifficulty::median_time(older_times)
|
||||||
|
} else {
|
||||||
|
self.relevant_times
|
||||||
|
.last()
|
||||||
|
.cloned()
|
||||||
|
.expect("there must be a Genesis block")
|
||||||
|
};
|
||||||
|
|
||||||
// `ActualTimespan` in the Zcash specification
|
// `ActualTimespan` in the Zcash specification
|
||||||
newer_median - older_median
|
newer_median - older_median
|
||||||
|
|
Loading…
Reference in New Issue