use crate::block::Block as FullBlock;
use vapory_types::{H256, Bloom, U256, Address};
use hash::keccak;
use crate::header::Header as FullHeader;
use tetsy_util_mem::MallocSizeOf;
use tetsy_rlp::{self, Rlp, RlpStream};
use crate::transaction::UnverifiedTransaction;
use crate::views::{self, BlockView, HeaderView, BodyView};
use crate::BlockNumber;
#[derive(Debug, Clone, PartialEq, Eq, MallocSizeOf)]
pub struct Header(Vec<u8>);
impl Header {
pub fn new(encoded: Vec<u8>) -> Self { Header(encoded) }
pub fn decode(&self) -> Result<FullHeader, tetsy_rlp::DecoderError> {
tetsy_rlp::decode(&self.0)
}
#[inline]
pub fn view(&self) -> HeaderView { view!(HeaderView, &self.0) }
#[inline]
pub fn rlp(&self) -> Rlp { Rlp::new(&self.0) }
pub fn into_inner(self) -> Vec<u8> { self.0 }
}
impl std::fmt::LowerHex for Header {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in &self.0 {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl Header {
pub fn hash(&self) -> H256 { keccak(&self.0) }
pub fn parent_hash(&self) -> H256 { self.view().parent_hash() }
pub fn uncles_hash(&self) -> H256 { self.view().uncles_hash() }
pub fn author(&self) -> Address { self.view().author() }
pub fn state_root(&self) -> H256 { self.view().state_root() }
pub fn transactions_root(&self) -> H256 { self.view().transactions_root() }
pub fn receipts_root(&self) -> H256 { self.view().receipts_root() }
pub fn log_bloom(&self) -> Bloom { self.view().log_bloom() }
pub fn difficulty(&self) -> U256 { self.view().difficulty() }
pub fn number(&self) -> BlockNumber { self.view().number() }
pub fn timestamp(&self) -> u64 { self.view().timestamp() }
pub fn gas_limit(&self) -> U256 { self.view().gas_limit() }
pub fn gas_used(&self) -> U256 { self.view().gas_used() }
pub fn extra_data(&self) -> Vec<u8> { self.view().extra_data() }
pub fn seal(&self) -> Vec<Vec<u8>> { self.view().seal() }
}
#[derive(Debug, Clone, PartialEq, Eq, MallocSizeOf)]
pub struct Body(Vec<u8>);
impl Body {
pub fn new(raw: Vec<u8>) -> Self { Body(raw) }
#[inline]
pub fn view(&self) -> BodyView { view!(BodyView, &self.0) }
pub fn decode(&self) -> (Vec<UnverifiedTransaction>, Vec<FullHeader>) {
(self.view().transactions(), self.view().uncles())
}
#[inline]
pub fn rlp(&self) -> Rlp {
Rlp::new(&self.0)
}
pub fn into_inner(self) -> Vec<u8> { self.0 }
}
impl Body {
pub fn transactions_rlp(&self) -> Rlp { self.view().transactions_rlp().rlp }
pub fn transactions(&self) -> Vec<UnverifiedTransaction> { self.view().transactions() }
pub fn transactions_count(&self) -> usize { self.view().transactions_count() }
pub fn transaction_views(&self) -> Vec<views::TransactionView> { self.view().transaction_views() }
pub fn transaction_hashes(&self) -> Vec<H256> { self.view().transaction_hashes() }
pub fn uncles_rlp(&self) -> Rlp { self.view().uncles_rlp().rlp }
pub fn uncles(&self) -> Vec<FullHeader> { self.view().uncles() }
pub fn uncles_count(&self) -> usize { self.view().uncles_count() }
pub fn uncle_views(&self) -> Vec<views::HeaderView> { self.view().uncle_views() }
pub fn uncle_hashes(&self) -> Vec<H256> { self.view().uncle_hashes() }
}
#[derive(Debug, Clone, PartialEq, Eq, MallocSizeOf)]
pub struct Block(Vec<u8>);
impl Block {
pub fn new(raw: Vec<u8>) -> Self { Block(raw) }
pub fn new_from_header_and_body(header: &views::HeaderView, body: &views::BodyView) -> Self {
let mut stream = RlpStream::new_list(3);
stream.append_raw(header.rlp().as_raw(), 1);
stream.append_raw(body.transactions_rlp().as_raw(), 1);
stream.append_raw(body.uncles_rlp().as_raw(), 1);
Block::new(stream.out())
}
#[inline]
pub fn view(&self) -> BlockView { view!(BlockView, &self.0) }
#[inline]
pub fn header_view(&self) -> HeaderView { self.view().header_view() }
pub fn decode(&self) -> Result<FullBlock, tetsy_rlp::DecoderError> { tetsy_rlp::decode(&self.0) }
pub fn decode_header(&self) -> FullHeader { self.view().rlp().val_at(0) }
pub fn header(&self) -> Header { Header(self.view().rlp().at(0).as_raw().to_vec()) }
#[inline]
pub fn rlp(&self) -> Rlp {
Rlp::new(&self.0)
}
pub fn into_inner(self) -> Vec<u8> { self.0 }
pub fn raw(&self) -> &[u8] {
&self.0
}
}
impl Block {
pub fn hash(&self) -> H256 { self.header_view().hash() }
pub fn parent_hash(&self) -> H256 { self.header_view().parent_hash() }
pub fn uncles_hash(&self) -> H256 { self.header_view().uncles_hash() }
pub fn author(&self) -> Address { self.header_view().author() }
pub fn state_root(&self) -> H256 { self.header_view().state_root() }
pub fn transactions_root(&self) -> H256 { self.header_view().transactions_root() }
pub fn receipts_root(&self) -> H256 { self.header_view().receipts_root() }
pub fn log_bloom(&self) -> Bloom { self.header_view().log_bloom() }
pub fn difficulty(&self) -> U256 { self.header_view().difficulty() }
pub fn number(&self) -> BlockNumber { self.header_view().number() }
pub fn timestamp(&self) -> u64 { self.header_view().timestamp() }
pub fn gas_limit(&self) -> U256 { self.header_view().gas_limit() }
pub fn gas_used(&self) -> U256 { self.header_view().gas_used() }
pub fn extra_data(&self) -> Vec<u8> { self.header_view().extra_data() }
pub fn seal(&self) -> Vec<Vec<u8>> { self.header_view().seal() }
}
impl Block {
pub fn transactions(&self) -> Vec<UnverifiedTransaction> { self.view().transactions() }
pub fn transactions_count(&self) -> usize { self.view().transactions_count() }
pub fn transaction_views(&self) -> Vec<views::TransactionView> { self.view().transaction_views() }
pub fn transaction_hashes(&self) -> Vec<H256> { self.view().transaction_hashes() }
pub fn uncles(&self) -> Vec<FullHeader> { self.view().uncles() }
pub fn uncles_count(&self) -> usize { self.view().uncles_count() }
pub fn uncle_views(&self) -> Vec<views::HeaderView> { self.view().uncle_views() }
pub fn uncle_hashes(&self) -> Vec<H256> { self.view().uncle_hashes() }
}