Skip to content

Commit

Permalink
feat: add eth_getRawTransaction (paradigmxyz#6830)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 27, 2024
1 parent d93d8ea commit 7bee4a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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

0 comments on commit 7bee4a0

Please sign in to comment.