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 eth_getRawTransaction #6830

Merged
merged 1 commit into from
Feb 27, 2024
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
6 changes: 6 additions & 0 deletions crates/rpc/rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ pub trait EthApi {
index: Index,
) -> RpcResult<Option<RichBlock>>;

/// Returns the EIP-2718 encoded transaction if it exists.
///
/// If this is a EIP-4844 transaction that is in the pool it will include the sidecar.
#[method(name = "getRawTransactionByHash")]
async fn raw_transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Bytes>>;

/// Returns the information about a transaction requested by transaction hash.
#[method(name = "getTransactionByHash")]
async fn transaction_by_hash(&self, hash: B256) -> RpcResult<Option<Transaction>>;
Expand Down
6 changes: 6 additions & 0 deletions crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ where
Ok(EthApi::ommer_by_block_and_index(self, number, index).await?)
}

/// 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::raw_transaction_by_hash(self, hash).await?)
}

/// 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");
Expand Down
26 changes: 26 additions & 0 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
},
EthApi, EthApiSpec,
};
use alloy_rlp::Encodable;
use async_trait::async_trait;
use reth_network_api::NetworkInfo;
use reth_node_api::ConfigureEvmEnv;
Expand Down Expand Up @@ -146,6 +147,15 @@ pub trait EthTransactions: Send + Sync {
block: BlockId,
) -> EthResult<Option<Vec<TransactionSigned>>>;

/// Returns the EIP-2718 encoded transaction by hash.
///
/// If this is a pooled EIP-4844 transaction, the blob sidecar is included.
///
/// Checks the pool and state.
///
/// Returns `Ok(None)` if no matching transaction was found.
async fn raw_transaction_by_hash(&self, hash: B256) -> EthResult<Option<Bytes>>;

/// Returns the transaction by hash.
///
/// Checks the pool and state.
Expand Down Expand Up @@ -428,6 +438,22 @@ where
self.block_by_id(block).await.map(|block| block.map(|block| block.body))
}

async fn raw_transaction_by_hash(&self, hash: B256) -> EthResult<Option<Bytes>> {
// Note: this is mostly used to fetch pooled transactions so we check the pool first
if let Some(tx) = self.pool().get_pooled_transaction_element(hash).map(|tx| {
let mut buf = Vec::new();
tx.encode(&mut buf);
buf
}) {
return Ok(Some(tx.into()));
}

self.on_blocking_task(|this| async move {
Ok(this.provider().transaction_by_hash(hash)?.map(|tx| tx.envelope_encoded()))
})
.await
}

async fn transaction_by_hash(&self, hash: B256) -> EthResult<Option<TransactionSource>> {
// Try to find the transaction on disk
let mut resp = self
Expand Down
Loading