mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-08 11:59:27 +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,7 +170,8 @@ 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 self.last_accounted_height + 1 == u64::try_from(numb_blocks).unwrap() {
|
||||||
// if the chain height is equal to `numb_blocks` add the genesis block.
|
// if the chain height is equal to `numb_blocks` add the genesis block.
|
||||||
// otherwise if the chain height is less than `numb_blocks` None is returned
|
// otherwise if the chain height is less than `numb_blocks` None is returned
|
||||||
// and if its more than it would be excluded from calculations.
|
// and if its more than it would be excluded from calculations.
|
||||||
|
@ -185,13 +186,16 @@ impl DifficultyCache {
|
||||||
.copied()
|
.copied()
|
||||||
.collect::<Vec<_>>()
|
.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