feat: add transactions_iter helper (#13910)

This commit is contained in:
Matthias Seitz
2025-01-22 00:37:51 +01:00
committed by GitHub
parent 6d2648dc1b
commit 901240877a
7 changed files with 23 additions and 22 deletions

View File

@ -553,8 +553,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
.block_ref()
.recovered_block()
.body()
.transactions()
.iter()
.transactions_iter()
.find(|tx| tx.trie_hash() == hash)
{
return Some(tx.clone())
@ -577,8 +576,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
.block_ref()
.recovered_block()
.body()
.transactions()
.iter()
.transactions_iter()
.enumerate()
.find(|(_, tx)| tx.trie_hash() == tx_hash)
{
@ -747,8 +745,7 @@ impl<N: NodePrimitives> BlockState<N> {
.block_ref()
.recovered_block()
.body()
.transactions()
.iter()
.transactions_iter()
.find(|tx| tx.trie_hash() == hash)
.cloned()
})
@ -767,8 +764,7 @@ impl<N: NodePrimitives> BlockState<N> {
.block_ref()
.recovered_block()
.body()
.transactions()
.iter()
.transactions_iter()
.enumerate()
.find(|(_, tx)| tx.trie_hash() == tx_hash)
.map(|(index, tx)| {

View File

@ -248,7 +248,7 @@ impl<N: NodePrimitives> Chain<N> {
self.blocks().iter().zip(self.execution_outcome.receipts().iter())
{
let mut tx_receipts = Vec::with_capacity(receipts.len());
for (tx, receipt) in block.body().transactions().iter().zip(receipts.iter()) {
for (tx, receipt) in block.body().transactions_iter().zip(receipts.iter()) {
tx_receipts.push((
tx.trie_hash(),
receipt.as_ref().expect("receipts have not been pruned").clone(),
@ -431,7 +431,7 @@ impl<B: Block<Body: BlockBody<Transaction: SignedTransaction>>> ChainBlocks<'_,
/// Returns an iterator over all transactions in the chain.
#[inline]
pub fn transactions(&self) -> impl Iterator<Item = &<B::Body as BlockBody>::Transaction> + '_ {
self.blocks.values().flat_map(|block| block.body().transactions().iter())
self.blocks.values().flat_map(|block| block.body().transactions_iter())
}
/// Returns an iterator over all transactions and their senders.
@ -457,7 +457,7 @@ impl<B: Block<Body: BlockBody<Transaction: SignedTransaction>>> ChainBlocks<'_,
pub fn transaction_hashes(&self) -> impl Iterator<Item = TxHash> + '_ {
self.blocks
.values()
.flat_map(|block| block.body().transactions().iter().map(|tx| tx.trie_hash()))
.flat_map(|block| block.body().transactions_iter().map(|tx| tx.trie_hash()))
}
}

View File

@ -33,18 +33,23 @@ pub trait BlockBody:
+ MaybeSerde
+ 'static
{
/// Ordered list of signed transactions as committed in block.
/// Ordered list of signed transactions as committed in the block.
type Transaction: SignedTransaction;
/// Ommer header type.
type OmmerHeader: BlockHeader;
/// Returns reference to transactions in block.
/// Returns reference to transactions in the block.
fn transactions(&self) -> &[Self::Transaction];
/// Returns an iterator over the transactions in the block.
fn transactions_iter(&self) -> impl Iterator<Item = &Self::Transaction> {
self.transactions().iter()
}
/// Returns an iterator over all transaction hashes in the block body.
fn transaction_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
self.transactions().iter().map(|tx| tx.tx_hash())
self.transactions_iter().map(|tx| tx.tx_hash())
}
/// Returns the number of the transactions in the block.
@ -57,7 +62,7 @@ pub trait BlockBody:
/// Returns `true` if the block body contains a transaction of the given type.
fn contains_transaction_type(&self, tx_type: u8) -> bool {
self.transactions().iter().any(|tx| tx.is_type(tx_type))
self.transactions_iter().any(|tx| tx.is_type(tx_type))
}
/// Calculate the transaction root for the block body.
@ -89,12 +94,12 @@ pub trait BlockBody:
/// Calculates the total blob gas used by _all_ EIP-4844 transactions in the block.
fn blob_gas_used(&self) -> u64 {
self.transactions().iter().filter_map(|tx| tx.blob_gas_used()).sum()
self.transactions_iter().filter_map(|tx| tx.blob_gas_used()).sum()
}
/// Returns an iterator over all blob versioned hashes in the block body.
fn blob_versioned_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
self.transactions().iter().filter_map(|tx| tx.blob_versioned_hashes()).flatten()
self.transactions_iter().filter_map(|tx| tx.blob_versioned_hashes()).flatten()
}
/// Returns an iterator over the encoded 2718 transactions.
@ -104,7 +109,7 @@ pub trait BlockBody:
/// See also [`Encodable2718`].
#[doc(alias = "raw_transactions_iter")]
fn encoded_2718_transactions_iter(&self) -> impl Iterator<Item = Vec<u8>> + '_ {
self.transactions().iter().map(|tx| tx.encoded_2718())
self.transactions_iter().map(|tx| tx.encoded_2718())
}
/// Returns a vector of encoded 2718 transactions.

View File

@ -226,7 +226,7 @@ where
let parent_hash = block.parent_hash();
// sort the functions by ascending effective tip first
let sorted_transactions = block.body().transactions().iter().sorted_by_cached_key(|tx| {
let sorted_transactions = block.body().transactions_iter().sorted_by_cached_key(|tx| {
if let Some(base_fee) = base_fee_per_gas {
(*tx).effective_tip_per_gas(base_fee)
} else {

View File

@ -108,7 +108,7 @@ pub fn convert_block_to_payload_field_v2<T: SignedTransaction>(
pub fn convert_to_payload_body_v1(
value: impl reth_primitives_traits::Block,
) -> ExecutionPayloadBodyV1 {
let transactions = value.body().transactions().iter().map(|tx| tx.encoded_2718().into());
let transactions = value.body().transactions_iter().map(|tx| tx.encoded_2718().into());
ExecutionPayloadBodyV1 {
transactions: transactions.collect(),
withdrawals: value.body().withdrawals().cloned().map(Withdrawals::into_inner),

View File

@ -1065,7 +1065,7 @@ impl<N: ProviderNodeTypes> ReceiptProvider for ConsistentProvider<N> {
);
if let Some(tx_index) =
block.body().transactions().iter().position(|tx| tx.trie_hash() == hash)
block.body().transactions_iter().position(|tx| tx.trie_hash() == hash)
{
// safe to use tx_index for receipts due to 1:1 correspondence
return Ok(receipts.get(tx_index).cloned());

View File

@ -2836,7 +2836,7 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
let tx_count = block.body().transaction_count() as u64;
// Ensures we have all the senders for the block's transactions.
for (transaction, sender) in block.body().transactions().iter().zip(block.senders_iter()) {
for (transaction, sender) in block.body().transactions_iter().zip(block.senders_iter()) {
let hash = transaction.tx_hash();
if self.prune_modes.sender_recovery.as_ref().is_none_or(|m| !m.is_full()) {