1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::HeaderProvider;
use ckb_types::{core::BlockNumber, packed::Byte32};
pub trait BlockMedianTimeContext: HeaderProvider {
fn median_block_count(&self) -> u64;
fn timestamp_and_parent(&self, block_hash: &Byte32) -> (u64, BlockNumber, Byte32) {
let header = self
.get_header(block_hash)
.expect("[BlockMedianTimeContext] blocks used for median time exist");
(
header.timestamp(),
header.number(),
header.data().raw().parent_hash(),
)
}
fn block_median_time(&self, block_hash: &Byte32) -> u64 {
let median_time_span = self.median_block_count();
let mut timestamps: Vec<u64> = Vec::with_capacity(median_time_span as usize);
let mut block_hash = block_hash.clone();
for _ in 0..median_time_span {
let (timestamp, block_number, parent_hash) = self.timestamp_and_parent(&block_hash);
timestamps.push(timestamp);
block_hash = parent_hash;
if block_number == 0 {
break;
}
}
timestamps.sort();
timestamps[timestamps.len() >> 1]
}
}