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
use chrono::{DateTime, TimeZone, Utc};
use derive_more::{Add, Display, From, Into};
use fuel_tx::{crypto::Hasher, Address, Bytes32};
use serde::{Deserialize, Serialize};
use std::{
array::TryFromSliceError,
convert::{TryFrom, TryInto},
iter::FromIterator,
};
#[derive(
Copy,
Clone,
Debug,
Default,
PartialEq,
PartialOrd,
Deserialize,
Serialize,
Add,
Display,
Into,
From,
)]
pub struct BlockHeight(u32);
impl From<BlockHeight> for Vec<u8> {
fn from(height: BlockHeight) -> Self {
height.0.to_be_bytes().to_vec()
}
}
impl From<u64> for BlockHeight {
fn from(height: u64) -> Self {
Self(height as u32)
}
}
impl From<BlockHeight> for u64 {
fn from(b: BlockHeight) -> Self {
b.0 as u64
}
}
impl TryFrom<Vec<u8>> for BlockHeight {
type Error = TryFromSliceError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let block_height_bytes: [u8; 4] = value.as_slice().try_into()?;
Ok(BlockHeight(u32::from_be_bytes(block_height_bytes)))
}
}
impl From<usize> for BlockHeight {
fn from(n: usize) -> Self {
BlockHeight(n as u32)
}
}
impl BlockHeight {
pub(crate) fn to_bytes(self) -> [u8; 4] {
self.0.to_be_bytes()
}
pub(crate) fn to_usize(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FuelBlock {
pub fuel_height: BlockHeight,
pub transactions: Vec<Bytes32>,
pub time: DateTime<Utc>,
pub producer: Address,
}
impl Default for FuelBlock {
fn default() -> Self {
Self {
fuel_height: 0u32.into(),
transactions: vec![],
time: Utc.timestamp(0, 0),
producer: Default::default(),
}
}
}
impl FuelBlock {
pub fn id(&self) -> Bytes32 {
let mut hasher = Hasher::from_iter(&self.transactions);
hasher.input(&self.fuel_height.to_bytes()[..]);
hasher.input(self.time.timestamp_millis().to_be_bytes());
hasher.input(self.producer.as_ref());
hasher.digest()
}
}