Skip to content

Commit

Permalink
fix: fix median timestamp index (#3349)
Browse files Browse the repository at this point in the history
Description
---
The current code will ask for the last 12 block heights and not 11. This code fixes it to only work on the last 11

Motivation and Context
---
The consensus rules are stated to work on the last 11 blocks and not 12. The current code incorrectly includes a second index.

How Has This Been Tested?
---
Unit tests
  • Loading branch information
SWvheerden committed Sep 15, 2021
1 parent fc1e120 commit 0757e9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base_layer/core/src/validation/header_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl HeaderValidator {
}

let height = block_header.height - 1;
let min_height = height.saturating_sub(
let min_height = block_header.height.saturating_sub(
self.rules
.consensus_constants(block_header.height)
.get_median_timestamp_count() as u64,
Expand Down
6 changes: 6 additions & 0 deletions base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ pub fn calc_median_timestamp(timestamps: &[EpochTime]) -> EpochTime {

let mid_index = timestamps.len() / 2;
let median_timestamp = if timestamps.len() % 2 == 0 {
trace!(
target: LOG_TARGET,
"No median timestamp available, estimating median as avg of [{}] and [{}]",
timestamps[mid_index - 1],
timestamps[mid_index],
);
(timestamps[mid_index - 1] + timestamps[mid_index]) / 2
} else {
timestamps[mid_index]
Expand Down

0 comments on commit 0757e9b

Please sign in to comment.