mirror of
https://github.com/Cuprate/cuprate.git
synced 2024-12-23 03:59:31 +00:00
fix cumulative diff calculations + sort timestamps before getting median
we were not accounting for the genesis blocks difficulty of 1.
This commit is contained in:
parent
2033a2d16c
commit
cb7d8b7b5e
1 changed files with 20 additions and 16 deletions
|
@ -170,28 +170,32 @@ impl DifficultyCache {
|
||||||
///
|
///
|
||||||
/// 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> {
|
||||||
let timestamps = if self.last_accounted_height + 1 == u64::try_from(numb_blocks).unwrap() {
|
let mut timestamps =
|
||||||
// if the chain height is equal to `numb_blocks` add the genesis block.
|
if self.last_accounted_height + 1 == u64::try_from(numb_blocks).unwrap() {
|
||||||
// otherwise if the chain height is less than `numb_blocks` None is returned
|
// if the chain height is equal to `numb_blocks` add the genesis block.
|
||||||
// and if its more than it would be excluded from calculations.
|
// otherwise if the chain height is less than `numb_blocks` None is returned
|
||||||
let mut timestamps = self.timestamps.clone();
|
// and if its more than it would be excluded from calculations.
|
||||||
// all genesis blocks have a timestamp of 0.
|
let mut timestamps = self.timestamps.clone();
|
||||||
// https://cuprate.github.io/monero-book/consensus_rules/genesis_block.html
|
// all genesis blocks have a timestamp of 0.
|
||||||
timestamps.push_front(0);
|
// https://cuprate.github.io/monero-book/consensus_rules/genesis_block.html
|
||||||
timestamps.into()
|
timestamps.push_front(0);
|
||||||
} else {
|
timestamps.into()
|
||||||
self.timestamps
|
} else {
|
||||||
.range(self.timestamps.len().checked_sub(numb_blocks)?..)
|
self.timestamps
|
||||||
.copied()
|
.range(self.timestamps.len().checked_sub(numb_blocks)?..)
|
||||||
.collect::<Vec<_>>()
|
.copied()
|
||||||
};
|
.collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
timestamps.sort_unstable();
|
||||||
|
debug_assert_eq!(timestamps.len(), numb_blocks);
|
||||||
|
|
||||||
Some(median(×tamps))
|
Some(median(×tamps))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the cumulative difficulty of the chain.
|
/// Returns the cumulative difficulty of the chain.
|
||||||
pub fn cumulative_difficulty(&self) -> u128 {
|
pub fn cumulative_difficulty(&self) -> u128 {
|
||||||
self.cumulative_difficulties.back().copied().unwrap_or(0)
|
// the genesis block has a difficulty of 1
|
||||||
|
self.cumulative_difficulties.back().copied().unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn top_block_timestamp(&self) -> Option<u64> {
|
pub fn top_block_timestamp(&self) -> Option<u64> {
|
||||||
|
|
Loading…
Reference in a new issue