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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pub use super::BlockHeight;
use chrono::{DateTime, TimeZone, Utc};
use fuel_crypto::Hasher;
use fuel_tx::{Address, Bytes32, Transaction};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FuelBlockHeader {
pub height: BlockHeight,
pub number: BlockHeight,
pub parent_hash: Bytes32,
pub prev_root: Bytes32,
pub transactions_root: Bytes32,
pub time: DateTime<Utc>,
pub producer: Address,
}
impl FuelBlockHeader {
pub fn id(&self) -> Bytes32 {
let mut hasher = Hasher::default();
hasher.input(&self.height.to_bytes()[..]);
hasher.input(&self.number.to_bytes()[..]);
hasher.input(self.parent_hash.as_ref());
hasher.input(self.prev_root.as_ref());
hasher.input(self.transactions_root.as_ref());
hasher.input(self.time.timestamp_millis().to_be_bytes());
hasher.input(self.producer.as_ref());
hasher.digest()
}
}
impl Default for FuelBlockHeader {
fn default() -> Self {
Self {
height: 0u32.into(),
number: 0u32.into(),
parent_hash: Default::default(),
time: Utc.timestamp(0, 0),
producer: Default::default(),
transactions_root: Default::default(),
prev_root: Default::default(),
}
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FuelBlockDb {
pub headers: FuelBlockHeader,
pub transactions: Vec<Bytes32>,
}
impl FuelBlockDb {
pub fn id(&self) -> Bytes32 {
self.headers.id()
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FuelBlock {
pub header: FuelBlockHeader,
pub transactions: Vec<Transaction>,
}
impl FuelBlock {
pub fn id(&self) -> Bytes32 {
self.header.id()
}
pub fn to_db_block(&self) -> FuelBlockDb {
FuelBlockDb {
headers: self.header.clone(),
transactions: self.transactions.iter().map(|tx| tx.id()).collect(),
}
}
}