fixed median_timestamp calculation
This commit is contained in:
parent
42c6ca77f0
commit
a1034cf623
|
@ -1,4 +1,3 @@
|
||||||
use std::collections::BTreeSet;
|
|
||||||
use chain::BlockHeader;
|
use chain::BlockHeader;
|
||||||
use db::{BlockHeaderProvider, BlockAncestors};
|
use db::{BlockHeaderProvider, BlockAncestors};
|
||||||
use primitives::hash::H256;
|
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
|
/// The header should be later expected to have higher timestamp
|
||||||
/// than this median timestamp
|
/// than this median timestamp
|
||||||
pub fn median_timestamp_inclusive(previous_header_hash: H256, store: &BlockHeaderProvider) -> u32 {
|
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)
|
.take(11)
|
||||||
.map(|header| header.time)
|
.map(|header| header.time)
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -23,6 +22,6 @@ pub fn median_timestamp_inclusive(previous_header_hash: H256, store: &BlockHeade
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let timestamps = timestamps.into_iter().collect::<Vec<_>>();
|
timestamps.sort();
|
||||||
timestamps[timestamps.len() / 2]
|
timestamps[timestamps.len() / 2]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue