-
Notifications
You must be signed in to change notification settings - Fork 55
/
header.rs
46 lines (36 loc) · 1.04 KB
/
header.rs
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
41
42
43
44
45
46
use preamble::*;
#[derive(Clone, Copy)]
pub struct BlockHeader<'a>(&'a [u8; 80]);
impl<'a> BlockHeader<'a> {
pub fn new(slice: &[u8; 80]) -> BlockHeader {
BlockHeader(slice)
}
pub fn as_slice(&self) -> &'a [u8; 80] {
&self.0
}
pub fn version(&self) -> i32 {
let mut slice = &self.0[0..];
slice.read_i32::<LittleEndian>().unwrap()
}
pub fn cur_hash(&self) -> Hash {
Hash::from_data(self.0)
}
pub fn prev_hash(&self) -> &'a Hash {
Hash::from_slice(array_ref!(self.0, 4, 32))
}
pub fn merkle_root(&self) -> &'a Hash {
Hash::from_slice(array_ref!(self.0, 36, 32))
}
pub fn timestamp(&self) -> u32 {
let mut slice = &self.0[68..];
slice.read_u32::<LittleEndian>().unwrap()
}
pub fn bits(&self) -> u32 {
let mut slice = &self.0[72..];
slice.read_u32::<LittleEndian>().unwrap()
}
pub fn nonce(&self) -> u32 {
let mut slice = &self.0[76..];
slice.read_u32::<LittleEndian>().unwrap()
}
}