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:
Arya 2024-05-07 16:51:13 -04:00 committed by GitHub
parent 3e264c73a6
commit 0040c2be87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 8 deletions

View File

@ -295,15 +295,23 @@ impl AdjustedDifficulty {
fn median_timespan(&self) -> Duration {
let newer_median = self.median_time_past();
let older_times: Vec<_> = self
.relevant_times
.iter()
.rev()
.cloned()
.take(POW_MEDIAN_BLOCK_SPAN)
.collect();
// 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
.relevant_times
.iter()
.skip(POW_AVERAGING_WINDOW)
.cloned()
.take(POW_MEDIAN_BLOCK_SPAN)
.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
newer_median - older_median