From dd997e0705056ff426f9612b192e00f0c414a8ee Mon Sep 17 00:00:00 2001 From: Ishika Choudhury <117741714+Rimeeeeee@users.noreply.github.com> Date: Sat, 26 Apr 2025 11:52:02 +0530 Subject: [PATCH] feat: Introduce Block::into_ethereum_block to Block trait (#15940) Co-authored-by: Matthias Seitz Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> --- crates/primitives-traits/src/block/mod.rs | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/crates/primitives-traits/src/block/mod.rs b/crates/primitives-traits/src/block/mod.rs index fa6bc2c3ae..f3ac7f2bc7 100644 --- a/crates/primitives-traits/src/block/mod.rs +++ b/crates/primitives-traits/src/block/mod.rs @@ -177,6 +177,27 @@ pub trait Block: }; Ok(RecoveredBlock::new_unhashed(self, signers)) } + + /// A Convenience function to convert this type into the regular ethereum block that + /// consists of: + /// + /// - Header + /// + /// And the ethereum block body [`alloy_consensus::BlockBody`], see also + /// [`BlockBody::into_ethereum_body`]. + /// - Transactions + /// - Withdrawals + /// - Ommers + /// + /// Note: This conversion can be incomplete. It is not expected that this `Block` is the same as + /// [`alloy_consensus::Block`] only that it can be converted into it which is useful for + /// the `eth_` RPC namespace (e.g. RPC block). + fn into_ethereum_block( + self, + ) -> alloy_consensus::Block<::Transaction, Self::Header> { + let (header, body) = self.split(); + alloy_consensus::Block::new(header, body.into_ethereum_body()) + } } impl Block for alloy_consensus::Block @@ -206,6 +227,10 @@ where fn rlp_length(header: &Self::Header, body: &Self::Body) -> usize { Self::rlp_length_for(header, body) } + + fn into_ethereum_block(self) -> Self { + self + } } /// An extension trait for [`Block`]s that allows for mutable access to the block's internals.