reth_primitives_traits/block/
body.rsuse crate::{
BlockHeader, FullSignedTx, InMemorySize, MaybeSerde, MaybeSerdeBincodeCompat, SignedTransaction,
};
use alloc::{fmt, vec::Vec};
use alloy_consensus::{Header, Transaction};
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals};
use alloy_primitives::{Bytes, B256};
pub trait FullBlockBody: BlockBody<Transaction: FullSignedTx> + MaybeSerdeBincodeCompat {}
impl<T> FullBlockBody for T where T: BlockBody<Transaction: FullSignedTx> + MaybeSerdeBincodeCompat {}
pub trait BlockBody:
Send
+ Sync
+ Unpin
+ Clone
+ Default
+ fmt::Debug
+ PartialEq
+ Eq
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ InMemorySize
+ MaybeSerde
+ 'static
{
type Transaction: SignedTransaction;
type OmmerHeader: BlockHeader;
fn transactions(&self) -> &[Self::Transaction];
fn into_transactions(self) -> Vec<Self::Transaction>;
fn calculate_tx_root(&self) -> B256 {
alloy_consensus::proofs::calculate_transaction_root(self.transactions())
}
fn withdrawals(&self) -> Option<&Withdrawals>;
fn calculate_withdrawals_root(&self) -> Option<B256> {
self.withdrawals().map(|withdrawals| {
alloy_consensus::proofs::calculate_withdrawals_root(withdrawals.as_slice())
})
}
fn ommers(&self) -> Option<&[Self::OmmerHeader]>;
fn calculate_ommers_root(&self) -> Option<B256> {
self.ommers().map(alloy_consensus::proofs::calculate_ommers_root)
}
fn blob_gas_used(&self) -> u64 {
self.transactions().iter().filter_map(|tx| tx.blob_gas_used()).sum()
}
fn blob_versioned_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
self.transactions().iter().filter_map(|tx| tx.blob_versioned_hashes()).flatten()
}
#[doc(alias = "raw_transactions_iter")]
fn encoded_2718_transactions_iter(&self) -> impl Iterator<Item = Vec<u8>> + '_ {
self.transactions().iter().map(|tx| tx.encoded_2718())
}
#[doc(alias = "raw_transactions")]
fn encoded_2718_transactions(&self) -> Vec<Bytes> {
self.encoded_2718_transactions_iter().map(Into::into).collect()
}
}
impl<T> BlockBody for alloy_consensus::BlockBody<T>
where
T: SignedTransaction,
{
type Transaction = T;
type OmmerHeader = Header;
fn transactions(&self) -> &[Self::Transaction] {
&self.transactions
}
fn into_transactions(self) -> Vec<Self::Transaction> {
self.transactions
}
fn withdrawals(&self) -> Option<&Withdrawals> {
self.withdrawals.as_ref()
}
fn ommers(&self) -> Option<&[Self::OmmerHeader]> {
Some(&self.ommers)
}
}
pub type BodyTx<N> = <N as BlockBody>::Transaction;
pub type BodyOmmer<N> = <N as BlockBody>::OmmerHeader;