From da409693ee2a24b46b70e4d154ce8e9084ebfdc3 Mon Sep 17 00:00:00 2001 From: Steven <112043913+stevencartavia@users.noreply.github.com> Date: Wed, 11 Dec 2024 11:31:25 -0600 Subject: [PATCH] feat: reth's block body fns (#1775) * feat: reth's block body fns * rm serde * rm redundant fns * nits * touchups --------- Co-authored-by: Steven Co-authored-by: Matthias Seitz --- crates/consensus/src/block/mod.rs | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index 177d24b8664..1263173e536 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -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. @@ -58,6 +60,44 @@ impl BlockBody { pub fn transactions(&self) -> impl Iterator + '_ { self.transactions.iter() } + + /// Create a [`Block`] from the body and its header. + pub const fn into_block(self, header: Header) -> Block { + Block { header, body: self } + } +} + +impl BlockBody { + /// 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 { + self.withdrawals.as_ref().map(|w| crate::proofs::calculate_withdrawals_root(w)) + } +} + +impl BlockBody { + /// 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 + '_ { + 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