Skip to content

Commit

Permalink
feat: reth's block body fns (#1775)
Browse files Browse the repository at this point in the history
* feat: reth's block body fns

* rm serde

* rm redundant fns

* nits

* touchups

---------

Co-authored-by: Steven <steven@MacBook-Pro.local>
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 84b24a1 commit da40969
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/consensus/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ pub use header::{BlockHeader, Header};
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(crate) use header::serde_bincode_compat;

use crate::Typed2718;
use alloc::vec::Vec;
use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::B256;
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};

/// Ethereum full block.
Expand Down Expand Up @@ -58,6 +60,44 @@ impl<T> BlockBody<T> {
pub fn transactions(&self) -> impl Iterator<Item = &T> + '_ {
self.transactions.iter()
}

/// Create a [`Block`] from the body and its header.
pub const fn into_block(self, header: Header) -> Block<T> {
Block { header, body: self }
}
}

impl<T> BlockBody<T> {
/// Calculate the ommers root for the block body.
pub fn calculate_ommers_root(&self) -> B256 {
crate::proofs::calculate_ommers_root(&self.ommers)
}

/// Calculate the withdrawals root for the block body, if withdrawals exist. If there are no
/// withdrawals, this will return `None`.
pub fn calculate_withdrawals_root(&self) -> Option<B256> {
self.withdrawals.as_ref().map(|w| crate::proofs::calculate_withdrawals_root(w))
}
}

impl<T: Typed2718> BlockBody<T> {
/// Returns whether or not the block body contains any blob transactions.
#[inline]
pub fn has_eip4844_transactions(&self) -> bool {
self.transactions.iter().any(|tx| tx.is_eip4844())
}

/// Returns whether or not the block body contains any EIP-7702 transactions.
#[inline]
pub fn has_eip7702_transactions(&self) -> bool {
self.transactions.iter().any(|tx| tx.is_eip7702())
}

/// Returns an iterator over all blob transactions of the block
#[inline]
pub fn eip4844_transactions_iter(&self) -> impl Iterator<Item = &T> + '_ {
self.transactions.iter().filter(|tx| tx.is_eip4844())
}
}

/// We need to implement RLP traits manually because we currently don't have a way to flatten
Expand Down

0 comments on commit da40969

Please sign in to comment.