fix getting the median timestamp

we were excluding the genesis block
This commit is contained in:
Boog900 2023-10-24 15:34:41 +01:00
parent edccf83481
commit 9b7f778f60
No known key found for this signature in database
GPG key ID: 5401367FB7302004
2 changed files with 17 additions and 7 deletions

View file

@ -166,17 +166,27 @@ impl DifficultyCache {
(windowed_work * hf.block_time().as_secs() as u128 + time_span - 1) / time_span (windowed_work * hf.block_time().as_secs() as u128 + time_span - 1) / time_span
} }
/// Returns the median timestamp over the last `numb_blocks`. /// Returns the median timestamp over the last `numb_blocks`, including the genesis block if the block height is low enough.
/// ///
/// Will return [`None`] if there aren't enough blocks. /// Will return [`None`] if there aren't enough blocks.
pub fn median_timestamp(&self, numb_blocks: usize) -> Option<u64> { pub fn median_timestamp(&self, numb_blocks: usize) -> Option<u64> {
Some(median( let timestamps = if self.last_accounted_height + 1 == u64::try_from(numb_blocks).unwrap() {
&self // if the chain height is equal to `numb_blocks` add the genesis block.
.timestamps // otherwise if the chain height is less than `numb_blocks` None is returned
// and if its more than it would be excluded from calculations.
let mut timestamps = self.timestamps.clone();
// all genesis blocks have a timestamp of 0.
// https://cuprate.github.io/monero-book/consensus_rules/genesis_block.html
timestamps.push_front(0);
timestamps.into()
} else {
self.timestamps
.range(self.timestamps.len().checked_sub(numb_blocks)?..) .range(self.timestamps.len().checked_sub(numb_blocks)?..)
.copied() .copied()
.collect::<Vec<_>>(), .collect::<Vec<_>>()
)) };
Some(median(&timestamps))
} }
/// Returns the cumulative difficulty of the chain. /// Returns the cumulative difficulty of the chain.

View file

@ -197,7 +197,7 @@ impl BlockWeightsCache {
self.short_term_block_weights.clone().into(); self.short_term_block_weights.clone().into();
sorted_short_term_weights.par_sort_unstable(); sorted_short_term_weights.par_sort_unstable();
/// TODO: this sometimes takes a while (>5s) // TODO: this sometimes takes a while (>5s)
let mut sorted_long_term_weights: Vec<usize> = self.long_term_weights.clone().into(); let mut sorted_long_term_weights: Vec<usize> = self.long_term_weights.clone().into();
sorted_long_term_weights.par_sort_unstable(); sorted_long_term_weights.par_sort_unstable();