Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add calc tx root fn for rpc types #1950

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading