mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add transactions_iter helper (#13910)
This commit is contained in:
@ -553,8 +553,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
|
|||||||
.block_ref()
|
.block_ref()
|
||||||
.recovered_block()
|
.recovered_block()
|
||||||
.body()
|
.body()
|
||||||
.transactions()
|
.transactions_iter()
|
||||||
.iter()
|
|
||||||
.find(|tx| tx.trie_hash() == hash)
|
.find(|tx| tx.trie_hash() == hash)
|
||||||
{
|
{
|
||||||
return Some(tx.clone())
|
return Some(tx.clone())
|
||||||
@ -577,8 +576,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
|
|||||||
.block_ref()
|
.block_ref()
|
||||||
.recovered_block()
|
.recovered_block()
|
||||||
.body()
|
.body()
|
||||||
.transactions()
|
.transactions_iter()
|
||||||
.iter()
|
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, tx)| tx.trie_hash() == tx_hash)
|
.find(|(_, tx)| tx.trie_hash() == tx_hash)
|
||||||
{
|
{
|
||||||
@ -747,8 +745,7 @@ impl<N: NodePrimitives> BlockState<N> {
|
|||||||
.block_ref()
|
.block_ref()
|
||||||
.recovered_block()
|
.recovered_block()
|
||||||
.body()
|
.body()
|
||||||
.transactions()
|
.transactions_iter()
|
||||||
.iter()
|
|
||||||
.find(|tx| tx.trie_hash() == hash)
|
.find(|tx| tx.trie_hash() == hash)
|
||||||
.cloned()
|
.cloned()
|
||||||
})
|
})
|
||||||
@ -767,8 +764,7 @@ impl<N: NodePrimitives> BlockState<N> {
|
|||||||
.block_ref()
|
.block_ref()
|
||||||
.recovered_block()
|
.recovered_block()
|
||||||
.body()
|
.body()
|
||||||
.transactions()
|
.transactions_iter()
|
||||||
.iter()
|
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.find(|(_, tx)| tx.trie_hash() == tx_hash)
|
.find(|(_, tx)| tx.trie_hash() == tx_hash)
|
||||||
.map(|(index, tx)| {
|
.map(|(index, tx)| {
|
||||||
|
|||||||
@ -248,7 +248,7 @@ impl<N: NodePrimitives> Chain<N> {
|
|||||||
self.blocks().iter().zip(self.execution_outcome.receipts().iter())
|
self.blocks().iter().zip(self.execution_outcome.receipts().iter())
|
||||||
{
|
{
|
||||||
let mut tx_receipts = Vec::with_capacity(receipts.len());
|
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_receipts.push((
|
||||||
tx.trie_hash(),
|
tx.trie_hash(),
|
||||||
receipt.as_ref().expect("receipts have not been pruned").clone(),
|
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.
|
/// Returns an iterator over all transactions in the chain.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn transactions(&self) -> impl Iterator<Item = &<B::Body as BlockBody>::Transaction> + '_ {
|
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.
|
/// 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> + '_ {
|
pub fn transaction_hashes(&self) -> impl Iterator<Item = TxHash> + '_ {
|
||||||
self.blocks
|
self.blocks
|
||||||
.values()
|
.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()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,18 +33,23 @@ pub trait BlockBody:
|
|||||||
+ MaybeSerde
|
+ MaybeSerde
|
||||||
+ 'static
|
+ 'static
|
||||||
{
|
{
|
||||||
/// Ordered list of signed transactions as committed in block.
|
/// Ordered list of signed transactions as committed in the block.
|
||||||
type Transaction: SignedTransaction;
|
type Transaction: SignedTransaction;
|
||||||
|
|
||||||
/// Ommer header type.
|
/// Ommer header type.
|
||||||
type OmmerHeader: BlockHeader;
|
type OmmerHeader: BlockHeader;
|
||||||
|
|
||||||
/// Returns reference to transactions in block.
|
/// Returns reference to transactions in the block.
|
||||||
fn transactions(&self) -> &[Self::Transaction];
|
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.
|
/// Returns an iterator over all transaction hashes in the block body.
|
||||||
fn transaction_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
|
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.
|
/// 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.
|
/// Returns `true` if the block body contains a transaction of the given type.
|
||||||
fn contains_transaction_type(&self, tx_type: u8) -> bool {
|
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.
|
/// 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.
|
/// Calculates the total blob gas used by _all_ EIP-4844 transactions in the block.
|
||||||
fn blob_gas_used(&self) -> u64 {
|
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.
|
/// Returns an iterator over all blob versioned hashes in the block body.
|
||||||
fn blob_versioned_hashes_iter(&self) -> impl Iterator<Item = &B256> + '_ {
|
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.
|
/// Returns an iterator over the encoded 2718 transactions.
|
||||||
@ -104,7 +109,7 @@ pub trait BlockBody:
|
|||||||
/// See also [`Encodable2718`].
|
/// See also [`Encodable2718`].
|
||||||
#[doc(alias = "raw_transactions_iter")]
|
#[doc(alias = "raw_transactions_iter")]
|
||||||
fn encoded_2718_transactions_iter(&self) -> impl Iterator<Item = Vec<u8>> + '_ {
|
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.
|
/// Returns a vector of encoded 2718 transactions.
|
||||||
|
|||||||
@ -226,7 +226,7 @@ where
|
|||||||
let parent_hash = block.parent_hash();
|
let parent_hash = block.parent_hash();
|
||||||
|
|
||||||
// sort the functions by ascending effective tip first
|
// 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 {
|
if let Some(base_fee) = base_fee_per_gas {
|
||||||
(*tx).effective_tip_per_gas(base_fee)
|
(*tx).effective_tip_per_gas(base_fee)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -108,7 +108,7 @@ pub fn convert_block_to_payload_field_v2<T: SignedTransaction>(
|
|||||||
pub fn convert_to_payload_body_v1(
|
pub fn convert_to_payload_body_v1(
|
||||||
value: impl reth_primitives_traits::Block,
|
value: impl reth_primitives_traits::Block,
|
||||||
) -> ExecutionPayloadBodyV1 {
|
) -> 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 {
|
ExecutionPayloadBodyV1 {
|
||||||
transactions: transactions.collect(),
|
transactions: transactions.collect(),
|
||||||
withdrawals: value.body().withdrawals().cloned().map(Withdrawals::into_inner),
|
withdrawals: value.body().withdrawals().cloned().map(Withdrawals::into_inner),
|
||||||
|
|||||||
@ -1065,7 +1065,7 @@ impl<N: ProviderNodeTypes> ReceiptProvider for ConsistentProvider<N> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if let Some(tx_index) =
|
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
|
// safe to use tx_index for receipts due to 1:1 correspondence
|
||||||
return Ok(receipts.get(tx_index).cloned());
|
return Ok(receipts.get(tx_index).cloned());
|
||||||
|
|||||||
@ -2836,7 +2836,7 @@ impl<TX: DbTxMut + DbTx + 'static, N: NodeTypesForProvider + 'static> BlockWrite
|
|||||||
let tx_count = block.body().transaction_count() as u64;
|
let tx_count = block.body().transaction_count() as u64;
|
||||||
|
|
||||||
// Ensures we have all the senders for the block's transactions.
|
// 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();
|
let hash = transaction.tx_hash();
|
||||||
|
|
||||||
if self.prune_modes.sender_recovery.as_ref().is_none_or(|m| !m.is_full()) {
|
if self.prune_modes.sender_recovery.as_ref().is_none_or(|m| !m.is_full()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user