Skip to content

Commit

Permalink
feat: add calc tx root fn for rpc types (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 26, 2025
1 parent c475390 commit ae7c342
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions crates/network-primitives/src/block.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -83,6 +83,16 @@ impl<T> BlockTransactions<T> {
}
}

/// 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<B256>
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 {
Expand Down
12 changes: 11 additions & 1 deletion crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -143,6 +143,16 @@ impl<T, H> Block<T, H> {
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<B256>
where
T: Encodable2718,
{
self.transactions.calculate_transactions_root()
}
}

impl<T: TransactionResponse, H> Block<T, H> {
Expand Down

0 comments on commit ae7c342

Please sign in to comment.