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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
pub use super::BlockHeight;
use super::ValidatorStake;
use chrono::{DateTime, TimeZone, Utc};
use core::ops::Deref;
use fuel_crypto::Hasher;
use fuel_tx::{Address, AssetId, Bytes32, Transaction};
use fuel_types::Word;
use std::collections::HashMap;
#[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,
pub metadata: Option<HeaderMetadata>,
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct HeaderMetadata {
id: Bytes32,
}
impl FuelBlockHeader {
pub fn recalculate_metadata(&mut self) {
self.metadata = Some(HeaderMetadata { id: self.hash() });
}
fn hash(&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()
}
pub fn id(&self) -> Bytes32 {
if let Some(ref metadata) = self.metadata {
metadata.id
} else {
self.hash()
}
}
}
impl Default for FuelBlockHeader {
fn default() -> Self {
Self {
time: Utc.timestamp(0, 0),
height: BlockHeight::default(),
number: BlockHeight::default(),
parent_hash: Bytes32::default(),
prev_root: Bytes32::default(),
transactions_root: Bytes32::default(),
producer: Address::default(),
metadata: None,
}
}
}
#[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(),
}
}
pub fn transaction_data_lenght(&self) -> usize {
self.transactions.len() * 100
}
pub fn transaction_data_hash(&self) -> Bytes32 {
Bytes32::zeroed()
}
pub fn validator_set_hash(&self) -> Bytes32 {
Bytes32::zeroed()
}
pub fn transaction_sum(&self) -> usize {
0
}
pub fn withdrawals(&self) -> Vec<(Address, Word, AssetId)> {
vec![]
}
pub fn withdrawals_root(&self) -> Bytes32 {
Bytes32::zeroed()
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FuelBlockConsensus {
pub required_stake: u64,
pub validators: HashMap<Address, (ValidatorStake, Address)>,
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SealedFuelBlock {
pub block: FuelBlock,
pub consensus: FuelBlockConsensus,
}
impl Deref for SealedFuelBlock {
type Target = FuelBlock;
fn deref(&self) -> &FuelBlock {
&self.block
}
}