diff --git a/crates/network-primitives/src/block.rs b/crates/network-primitives/src/block.rs index 2ce3f9e7957..75c873a9841 100644 --- a/crates/network-primitives/src/block.rs +++ b/crates/network-primitives/src/block.rs @@ -1,11 +1,11 @@ use alloy_primitives::B256; use serde::{Deserialize, Serialize}; +use crate::TransactionResponse; use alloc::{vec, vec::Vec}; +use alloy_eips::Encodable2718; use core::slice; -use crate::TransactionResponse; - /// Block Transactions depending on the boolean attribute of `eth_getBlockBy*`, /// or if used by `eth_getUncle*` #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -83,6 +83,16 @@ impl BlockTransactions { } } + /// Calculate the transaction root for the full transactions. + /// + /// Returns `None` if this is not the [`BlockTransactions::Full`] variant + pub fn calculate_transactions_root(&self) -> Option + where + T: Encodable2718, + { + self.as_transactions().map(alloy_consensus::proofs::calculate_transaction_root) + } + /// Returns true if the enum variant is used for an uncle response. #[inline] pub const fn is_uncle(&self) -> bool { diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index d27bd998ea0..743c2da6e0f 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -11,11 +11,11 @@ use alloy_primitives::{Address, BlockHash, Bloom, Bytes, Sealable, B256, B64, U2 use alloy_rlp::Encodable; use core::ops::{Deref, DerefMut}; -use alloy_eips::eip7840::BlobParams; pub use alloy_eips::{ calc_blob_gasprice, calc_excess_blob_gas, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, RpcBlockHash, }; +use alloy_eips::{eip7840::BlobParams, Encodable2718}; /// Block representation for RPC. #[derive(Clone, Debug, PartialEq, Eq)] @@ -143,6 +143,16 @@ impl Block { withdrawals: self.withdrawals, }) } + + /// Calculate the transaction root for the full transactions in this block type. + /// + /// Returns `None` if the `transactions` is not the [`BlockTransactions::Full`] variant. + pub fn calculate_transactions_root(&self) -> Option + where + T: Encodable2718, + { + self.transactions.calculate_transactions_root() + } } impl Block {