diff --git a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs index caeb77c139..01b3f6c1b7 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/transaction.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/transaction.rs @@ -629,15 +629,9 @@ pub trait LoadTransaction: SpawnBlocking + FullEthApiTypes + RpcNodeCoreExt { async move { // First, try the RPC cache if let Some(cached) = self.cache().get_transaction_by_hash(hash).await && - let Some(tx) = cached.recovered_transaction() + let Some(source) = cached.to_transaction_source() { - return Ok(Some(TransactionSource::Block { - transaction: tx.cloned(), - index: cached.tx_index as u64, - block_hash: cached.block.hash(), - block_number: cached.block.number(), - base_fee: cached.block.base_fee_per_gas(), - })); + return Ok(Some(source)); } // Cache miss - try to find the transaction on disk diff --git a/crates/rpc/rpc-eth-types/src/block.rs b/crates/rpc/rpc-eth-types/src/block.rs index 6316effa36..f1db846f55 100644 --- a/crates/rpc/rpc-eth-types/src/block.rs +++ b/crates/rpc/rpc-eth-types/src/block.rs @@ -2,7 +2,7 @@ use std::sync::Arc; -use alloy_consensus::{transaction::TxHashRef, TxReceipt}; +use alloy_consensus::{transaction::TxHashRef, BlockHeader, TxReceipt}; use alloy_primitives::TxHash; use reth_primitives_traits::{ Block, BlockBody, BlockTy, IndexedTx, NodePrimitives, ReceiptTy, Recovered, RecoveredBlock, @@ -10,7 +10,7 @@ use reth_primitives_traits::{ }; use reth_rpc_convert::{transaction::ConvertReceiptInput, RpcConvert, RpcTypes}; -use crate::utils::calculate_gas_used_and_next_log_index; +use crate::{utils::calculate_gas_used_and_next_log_index, TransactionSource}; /// Cached data for a transaction lookup. #[derive(Debug, Clone)] @@ -38,6 +38,22 @@ impl CachedTransaction { self.block.recovered_transaction(self.tx_index) } + /// Converts this cached transaction into a [`TransactionSource::Block`]. + /// + /// Returns `None` if the transaction index is out of bounds. + pub fn to_transaction_source( + &self, + ) -> Option::Transaction>> { + let tx = self.recovered_transaction()?; + Some(TransactionSource::Block { + transaction: tx.cloned(), + index: self.tx_index as u64, + block_hash: self.block.hash(), + block_number: self.block.number(), + base_fee: self.block.base_fee_per_gas(), + }) + } + /// Converts this cached transaction into an RPC receipt using the given converter. /// /// Returns `None` if receipts are not available or the transaction index is out of bounds.