use unc_account_id::AccountId;
use unc_primitives::views::{BlockHeaderView, BlockView};
use crate::types::{ChunkHeader, UncToken};
use crate::{BlockHeight, CryptoHash};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Block {
author: AccountId,
header: BlockHeader,
chunks: Vec<ChunkHeader>,
}
impl Block {
pub fn author(&self) -> &AccountId {
&self.author
}
pub fn header(&self) -> &BlockHeader {
&self.header
}
pub fn chunks(&self) -> &[ChunkHeader] {
&self.chunks
}
pub fn timestamp(&self) -> u64 {
self.header.timestamp_nanosec
}
pub fn height(&self) -> BlockHeight {
self.header.height
}
pub fn hash(&self) -> &CryptoHash {
&self.header.hash
}
pub fn epoch_id(&self) -> &CryptoHash {
&self.header.epoch_id
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BlockHeader {
height: BlockHeight,
epoch_id: CryptoHash,
next_epoch_id: CryptoHash,
hash: CryptoHash,
prev_hash: CryptoHash,
timestamp_nanosec: u64,
random_value: CryptoHash,
gas_price: UncToken,
block_ordinal: Option<u64>,
total_supply: UncToken,
last_final_block: CryptoHash,
last_ds_final_block: CryptoHash,
next_bp_hash: CryptoHash,
latest_protocol_version: u32,
prev_state_root: CryptoHash,
chunk_receipts_root: CryptoHash,
chunk_headers_root: CryptoHash,
chunk_tx_root: CryptoHash,
outcome_root: CryptoHash,
challenges_root: CryptoHash,
block_merkle_root: CryptoHash,
}
impl BlockHeader {
pub fn height(&self) -> BlockHeight {
self.height
}
pub fn epoch_id(&self) -> &CryptoHash {
&self.epoch_id
}
pub fn next_epoch_id(&self) -> &CryptoHash {
&self.next_epoch_id
}
pub fn hash(&self) -> &CryptoHash {
&self.hash
}
pub fn prev_hash(&self) -> &CryptoHash {
&self.prev_hash
}
pub fn timestamp_nanosec(&self) -> u64 {
self.timestamp_nanosec
}
pub fn random_value(&self) -> &CryptoHash {
&self.random_value
}
pub fn gas_price(&self) -> UncToken {
self.gas_price
}
pub fn block_ordinal(&self) -> Option<u64> {
self.block_ordinal
}
pub fn total_supply(&self) -> UncToken {
self.total_supply
}
pub fn last_final_block(&self) -> &CryptoHash {
&self.last_final_block
}
pub fn last_ds_final_block(&self) -> &CryptoHash {
&self.last_ds_final_block
}
pub fn next_bp_hash(&self) -> &CryptoHash {
&self.next_bp_hash
}
pub fn latest_protocol_version(&self) -> u32 {
self.latest_protocol_version
}
pub fn prev_state_root(&self) -> &CryptoHash {
&self.prev_state_root
}
pub fn chunk_receipts_root(&self) -> &CryptoHash {
&self.chunk_receipts_root
}
pub fn chunk_headers_root(&self) -> &CryptoHash {
&self.chunk_headers_root
}
pub fn chunk_tx_root(&self) -> &CryptoHash {
&self.chunk_tx_root
}
pub fn outcome_root(&self) -> &CryptoHash {
&self.outcome_root
}
pub fn challenges_root(&self) -> &CryptoHash {
&self.challenges_root
}
pub fn block_merkle_root(&self) -> &CryptoHash {
&self.block_merkle_root
}
}
impl From<BlockView> for Block {
fn from(view: BlockView) -> Self {
Self {
author: view.author,
header: view.header.into(),
chunks: view.chunks.into_iter().map(Into::into).collect(),
}
}
}
impl From<BlockHeaderView> for BlockHeader {
fn from(header_view: BlockHeaderView) -> Self {
Self {
height: header_view.height,
epoch_id: header_view.epoch_id.into(),
next_epoch_id: header_view.next_epoch_id.into(),
hash: header_view.hash.into(),
prev_hash: header_view.prev_hash.into(),
timestamp_nanosec: header_view.timestamp_nanosec,
random_value: header_view.random_value.into(),
gas_price: UncToken::from_attounc(header_view.gas_price),
block_ordinal: header_view.block_ordinal,
total_supply: UncToken::from_attounc(header_view.total_supply),
last_final_block: header_view.last_final_block.into(),
last_ds_final_block: header_view.last_ds_final_block.into(),
next_bp_hash: header_view.next_bp_hash.into(),
latest_protocol_version: header_view.latest_protocol_version,
prev_state_root: header_view.prev_state_root.into(),
chunk_receipts_root: header_view.chunk_receipts_root.into(),
chunk_headers_root: header_view.chunk_headers_root.into(),
chunk_tx_root: header_view.chunk_tx_root.into(),
outcome_root: header_view.outcome_root.into(),
challenges_root: header_view.challenges_root.into(),
block_merkle_root: header_view.block_merkle_root.into(),
}
}
}