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: raw_tx_by_hash #6827

Merged
merged 6 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 21 additions & 0 deletions crates/rpc/rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ pub trait EthApi {
#[method(name = "getTransactionByHash")]
async fn transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Transaction>>;

/// Returns the information about a raw transaction requested by transaction hash.
#[method(name = "getRawTransactionByHash")]
async fn raw_transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Bytes>>;

/// Returns information about a transaction by block hash and transaction index position.
#[method(name = "getTransactionByBlockHashAndIndex")]
async fn transaction_by_block_hash_and_index(
Expand All @@ -104,6 +108,15 @@ pub trait EthApi {
index: Index,
) -> RpcResult<Option<Transaction>>;

// /// Returns information about a raw transaction by block number and transaction index
// /// position.
// #[method(name = "getRawTransactionByBlockNumberAndIndex")]
// async fn raw_transaction_by_block_number_and_index(
// &self,
// number: BlockNumberOrTag,
// index: Index,
// ) -> RpcResult<Option<Bytes>>;

/// Returns information about a transaction by block number and transaction index position.
#[method(name = "getTransactionByBlockNumberAndIndex")]
async fn transaction_by_block_number_and_index(
Expand All @@ -112,6 +125,14 @@ pub trait EthApi {
index: Index,
) -> RpcResult<Option<Transaction>>;

// /// Returns information about a raw transaction by block hash and transaction index position.
// #[method(name = "getRawTransactionByBlockHashAndIndex")]
// async fn raw_transaction_by_block_hash_and_index(
// &self,
// hash: B256,
// index: Index,
// ) -> RpcResult<Option<Bytes>>;

/// Returns the receipt of a transaction by transaction hash.
#[method(name = "getTransactionReceipt")]
async fn transaction_receipt(&self, hash: B256) -> RpcResult<Option<TransactionReceipt>>;
Expand Down
11 changes: 10 additions & 1 deletion crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
api::{EthApi, EthTransactions},
error::EthApiError,
revm_utils::EvmOverrides,
TransactionSource,
},
result::{internal_rpc_err, ToRpcResult},
};
Expand Down Expand Up @@ -156,7 +157,15 @@ where
/// Handler for: `eth_getTransactionByHash`
async fn transaction_by_hash(&self, hash: B256) -> Result<Option<reth_rpc_types::Transaction>> {
trace!(target: "rpc::eth", ?hash, "Serving eth_getTransactionByHash");
Ok(EthTransactions::transaction_by_hash(self, hash).await?.map(Into::into))
Ok(EthTransactions::transaction_by_hash::<TransactionSource>(self, hash)
.await?
.map(Into::into))
}

/// Handler for: `eth_getRawTransactionByHash`
async fn raw_transaction_by_hash(&self, hash: B256) -> Result<Option<Bytes>> {
trace!(target: "rpc::eth", ?hash, "Serving eth_getRawTransactionByHash");
Ok(EthTransactions::transaction_by_hash::<Bytes>(self, hash).await?.map(Into::into))
}

/// Handler for: `eth_getTransactionByBlockHashAndIndex`
Expand Down
52 changes: 42 additions & 10 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use reth_node_api::ConfigureEvmEnv;
use reth_primitives::{
eip4844::calc_blob_gasprice,
revm::env::{fill_block_env_with_coinbase, tx_env_with_recovered},
Address, BlockId, BlockNumberOrTag, Bytes, FromRecoveredPooledTransaction, Header,
IntoRecoveredTransaction, Receipt, SealedBlock, SealedBlockWithSenders,
Address, BlockId, BlockNumberOrTag, Bytes, FromRecoveredPooledTransaction, Header, Receipt,
SealedBlock, SealedBlockWithSenders,
TransactionKind::{Call, Create},
TransactionMeta, TransactionSigned, TransactionSignedEcRecovered, B256, U128, U256, U64,
};
Expand Down Expand Up @@ -146,12 +146,18 @@ pub trait EthTransactions: Send + Sync {
block: BlockId,
) -> EthResult<Option<Vec<TransactionSigned>>>;

/// Returns the transaction by hash.
/// Returns the transaction by hash, converted to a specified type.
///
/// Checks the pool and state.
/// Checks the pool and state for a transaction matching the given hash. Utilizes the generic
/// type `T`, which must implement `IntoRpcTransaction`, to allow flexible return types.
///
/// Returns `Ok(None)` if no matching transaction was found.
async fn transaction_by_hash(&self, hash: B256) -> EthResult<Option<TransactionSource>>;
///
/// Consumers: `eth_getTransactionByHash` and `eth_getRawTransactionByHash`
async fn transaction_by_hash<T: IntoRpcTransaction + Sync + Send + 'static>(
&self,
hash: B256,
) -> EthResult<Option<T>>;

/// Returns the transaction by including its corresponding [BlockId]
///
Expand Down Expand Up @@ -428,7 +434,10 @@ where
self.block_by_id(block).await.map(|block| block.map(|block| block.body))
}

async fn transaction_by_hash(&self, hash: B256) -> EthResult<Option<TransactionSource>> {
async fn transaction_by_hash<T: IntoRpcTransaction + Sync + Send + 'static>(
&self,
hash: B256,
) -> EthResult<Option<T>> {
// Try to find the transaction on disk
let mut resp = self
.on_blocking_task(|this| async move {
Expand All @@ -449,18 +458,21 @@ where
block_number: meta.block_number,
base_fee: meta.base_fee,
};
Ok(Some(tx))
Ok(Some(T::into_rpc_transaction(tx)))
}
}
})
.await?;

if resp.is_none() {
// tx not found on disk, check pool
if let Some(tx) =
self.pool().get(&hash).map(|tx| tx.transaction.to_recovered_transaction())
if let Some(Some(tx)) = self
.pool()
.get_pooled_transaction_element(hash)
.map(|tx| tx.into_transaction().into_ecrecovered())
{
resp = Some(TransactionSource::Pool(tx));
let tx = TransactionSource::Pool(tx);
resp = Some(T::into_rpc_transaction(tx));
}
}

Expand Down Expand Up @@ -1355,6 +1367,26 @@ impl From<TransactionSource> for Transaction {
}
}

/// Converts a `TransactionSource` into a type suitable for RPC transaction responses.
///
/// Used by
/// `eth_getTransactionByHash` or the raw bytes form used by `eth_getRawTransactionByHash`.
pub trait IntoRpcTransaction {
fn into_rpc_transaction(source: TransactionSource) -> Self;
}

impl IntoRpcTransaction for TransactionSource {
fn into_rpc_transaction(source: TransactionSource) -> Self {
source
}
}

impl IntoRpcTransaction for Bytes {
fn into_rpc_transaction(source: TransactionSource) -> Self {
source.into_recovered().envelope_encoded()
}
}
loocapro marked this conversation as resolved.
Show resolved Hide resolved

/// Helper function to construct a transaction receipt
///
/// Note: This requires _all_ block receipts because we need to calculate the gas used by the
Expand Down
Loading