use bytes::Bytes;
use vapory_types::{H256, U256};
use tetsy_util_mem::MallocSizeOf;
use crate::BlockNumber;
use crate::header::Header;
use tetsy_rlp::{Rlp, RlpStream, Decodable, DecoderError};
use crate::transaction::{UnverifiedTransaction, SignedTransaction};
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Block {
pub header: Header,
pub transactions: Vec<UnverifiedTransaction>,
pub uncles: Vec<Header>,
}
impl Block {
pub fn rlp_bytes(&self) -> Bytes {
let mut block_rlp = RlpStream::new_list(3);
block_rlp.append(&self.header);
block_rlp.append_list(&self.transactions);
block_rlp.append_list(&self.uncles);
block_rlp.out()
}
}
impl Decodable for Block {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
if rlp.as_raw().len() != rlp.payload_info()?.total() {
return Err(DecoderError::RlpIsTooBig);
}
if rlp.item_count()? != 3 {
return Err(DecoderError::RlpIncorrectListLen);
}
Ok(Block {
header: rlp.val_at(0)?,
transactions: rlp.list_at(1)?,
uncles: rlp.list_at(2)?,
})
}
}
#[derive(MallocSizeOf)]
pub struct PreverifiedBlock {
pub header: Header,
pub transactions: Vec<SignedTransaction>,
pub uncles: Vec<Header>,
pub bytes: Bytes,
}
#[derive(Clone)]
pub struct BlockInfo {
pub hash: H256,
pub number: BlockNumber,
pub total_difficulty: U256,
pub location: BlockLocation
}
#[derive(Debug, Clone, PartialEq)]
pub enum BlockLocation {
CanonChain,
Branch,
BranchBecomingCanonChain(BranchBecomingCanonChainData),
}
#[derive(Debug, Clone, PartialEq)]
pub struct BranchBecomingCanonChainData {
pub ancestor: H256,
pub enacted: Vec<H256>,
pub retracted: Vec<H256>,
}