use chrono::{DateTime, TimeZone, Utc};
use derive_more::{Add, Display, From, Into};
use fuel_crypto::Hasher;
use fuel_tx::{Address, Bytes32, Transaction};
use serde::{Deserialize, Serialize};
use std::{
array::TryFromSliceError,
convert::{TryFrom, TryInto},
};
#[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 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, Deserialize, Default, Serialize)]
pub struct FuelBlockDb {
pub headers: FuelBlockHeader,
pub transactions: Vec<Bytes32>,
}
impl FuelBlockDb {
pub fn id(&self) -> Bytes32 {
self.headers.id()
}
}
#[derive(Clone, Debug, Deserialize, Default, Serialize)]
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(),
}
}
}