fixed median_timestamp calculation

This commit is contained in:
Svyatoslav Nikolsky 2017-08-22 13:36:35 +03:00
parent 42c6ca77f0
commit a1034cf623
1 changed files with 2 additions and 3 deletions

View File

@ -1,4 +1,3 @@
use std::collections::BTreeSet;
use chain::BlockHeader;
use db::{BlockHeaderProvider, BlockAncestors};
use primitives::hash::H256;
@ -14,7 +13,7 @@ pub fn median_timestamp(header: &BlockHeader, store: &BlockHeaderProvider) -> u3
/// The header should be later expected to have higher timestamp
/// than this median timestamp
pub fn median_timestamp_inclusive(previous_header_hash: H256, store: &BlockHeaderProvider) -> u32 {
let timestamps: BTreeSet<_> = BlockAncestors::new(previous_header_hash.clone().into(), store)
let mut timestamps: Vec<_> = BlockAncestors::new(previous_header_hash.clone().into(), store)
.take(11)
.map(|header| header.time)
.collect();
@ -23,6 +22,6 @@ pub fn median_timestamp_inclusive(previous_header_hash: H256, store: &BlockHeade
return 0;
}
let timestamps = timestamps.into_iter().collect::<Vec<_>>();
timestamps.sort();
timestamps[timestamps.len() / 2]
}