Skip to content

Commit 7030e92

Browse files
committed
support get transaction by hash
1 parent c23179d commit 7030e92

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

crates/flashblocks-rpc/src/flashblocks.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ fn get_and_set_txs_and_receipts(
344344
// check if exists, if not update
345345
let existing_tx = cache.get::<OpTransactionSigned>(&transaction.tx_hash().to_string());
346346
if existing_tx.is_none() {
347+
println!(
348+
"setting transaction in cache: {:?}",
349+
transaction.tx_hash().to_string()
350+
);
347351
if let Err(e) = cache.set(&transaction.tx_hash().to_string(), &transaction, Some(10)) {
348352
error!("Failed to set transaction in cache: {}", e);
349353
continue;
@@ -369,6 +373,15 @@ fn get_and_set_txs_and_receipts(
369373
) {
370374
error!("Failed to set transaction count in cache: {}", e);
371375
}
376+
377+
// also keep track of sender of each transaction
378+
if let Err(e) = cache.set(
379+
&format!("tx_sender:{}", transaction.tx_hash()),
380+
&from,
381+
Some(10),
382+
) {
383+
error!("Failed to set transaction sender in cache: {}", e);
384+
}
372385
}
373386
}
374387

crates/flashblocks-rpc/src/rpc.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ use jsonrpsee::{
1515
use op_alloy_consensus::OpTxEnvelope;
1616
use op_alloy_network::Optimism;
1717
use op_alloy_rpc_types::Transaction;
18+
use reth::providers::TransactionsProvider;
19+
use reth::rpc::server_types::eth::TransactionSource;
1820
use reth::{api::BlockBody, providers::HeaderProvider};
1921
use reth_optimism_chainspec::OpChainSpec;
2022
use reth_optimism_primitives::{OpBlock, OpReceipt, OpTransactionSigned};
2123
use reth_optimism_rpc::OpReceiptBuilder;
2224
use reth_rpc_eth_api::helpers::EthTransactions;
23-
use reth_rpc_eth_api::RpcReceipt;
2425
use reth_rpc_eth_api::{helpers::FullEthApi, RpcBlock};
2526
use reth_rpc_eth_api::{
2627
helpers::{EthBlocks, EthState},
2728
RpcNodeCore,
2829
};
30+
use reth_rpc_eth_api::{RpcReceipt, RpcTransaction};
2931
use tracing::{debug, info};
3032

3133
#[cfg_attr(not(test), rpc(server, namespace = "eth"))]
@@ -54,6 +56,12 @@ pub trait EthApiOverride {
5456
address: Address,
5557
block_number: Option<BlockId>,
5658
) -> RpcResult<U256>;
59+
60+
#[method(name = "getTransactionByHash")]
61+
async fn transaction_by_hash(
62+
&self,
63+
tx_hash: TxHash,
64+
) -> RpcResult<Option<RpcTransaction<Optimism>>>;
5765
}
5866

5967
#[derive(Debug)]
@@ -89,7 +97,7 @@ impl<E> EthApiExt<E> {
8997
let signed_tx_ec_recovered = Recovered::new_unchecked(tx.clone(), sender);
9098
let tx_info = TransactionInfo {
9199
hash: Some(tx.tx_hash()),
92-
block_hash: None,
100+
block_hash: Some(block.header.hash_slow()),
93101
block_number: Some(block.number),
94102
index: Some(idx as u64),
95103
base_fee: None,
@@ -226,12 +234,14 @@ where
226234
Eth: FullEthApi<NetworkTypes = Optimism> + Send + Sync + 'static,
227235
Eth: RpcNodeCore,
228236
<Eth as RpcNodeCore>::Provider: HeaderProvider<Header = alloy_consensus::Header>,
237+
<Eth as RpcNodeCore>::Provider: TransactionsProvider<Transaction = OpTransactionSigned>,
229238
{
230239
async fn block_by_number(
231240
&self,
232241
number: BlockNumberOrTag,
233242
_full: bool,
234243
) -> RpcResult<Option<RpcBlock<Optimism>>> {
244+
debug!("block_by_number: {:?}", number);
235245
match number {
236246
BlockNumberOrTag::Pending => {
237247
debug!("pending block by number, delegating to flashblocks");
@@ -255,6 +265,7 @@ where
255265
&self,
256266
tx_hash: TxHash,
257267
) -> RpcResult<Option<RpcReceipt<Optimism>>> {
268+
debug!("get_transaction_receipt: {:?}", tx_hash);
258269
let receipt = EthTransactions::transaction_receipt(&self.eth_api, tx_hash).await;
259270

260271
// check if receipt is none
@@ -285,6 +296,7 @@ where
285296
address: Address,
286297
block_number: Option<BlockId>,
287298
) -> RpcResult<U256> {
299+
debug!("get_balance: {:?}", address);
288300
let block_id = block_number.unwrap_or_default();
289301
if block_id.is_pending() {
290302
self.metrics.get_balance.increment(1);
@@ -304,6 +316,7 @@ where
304316
address: Address,
305317
block_number: Option<BlockId>,
306318
) -> RpcResult<U256> {
319+
debug!("get_transaction_count: {:?}", address);
307320
let block_id = block_number.unwrap_or_default();
308321
if block_id.is_pending() {
309322
self.metrics.get_transaction_count.increment(1);
@@ -341,4 +354,61 @@ where
341354
.await
342355
.map_err(Into::into)
343356
}
357+
358+
async fn transaction_by_hash(
359+
&self,
360+
tx_hash: TxHash,
361+
) -> RpcResult<Option<RpcTransaction<Optimism>>> {
362+
debug!("transaction_by_hash: {:?}", tx_hash);
363+
let tx = EthTransactions::transaction_by_hash(&self.eth_api, tx_hash)
364+
.await
365+
.map_err(Into::into)?;
366+
367+
// Process the result without using map() and transpose()
368+
if let Some(tx_source) = tx {
369+
match tx_source {
370+
TransactionSource::Pool(tx) => {
371+
// Convert the pool transaction
372+
let tx_info = TransactionInfo::default();
373+
Ok(Some(self.transform_tx(tx, tx_info)))
374+
}
375+
TransactionSource::Block {
376+
transaction,
377+
index,
378+
block_hash,
379+
block_number,
380+
base_fee,
381+
} => {
382+
// Convert the block transaction
383+
let tx_info = TransactionInfo {
384+
hash: Some(tx_hash),
385+
index: Some(index),
386+
block_hash: Some(block_hash),
387+
block_number: Some(block_number),
388+
base_fee,
389+
};
390+
Ok(Some(self.transform_tx(transaction, tx_info)))
391+
}
392+
}
393+
} else {
394+
// Handle cache lookup for transactions not found in the main lookup
395+
if let Some(tx) = self.cache.get::<OpTransactionSigned>(&tx_hash.to_string()) {
396+
let sender = self
397+
.cache
398+
.get::<Address>(&format!("tx_sender:{}", tx_hash))
399+
.unwrap();
400+
let tx_info = TransactionInfo {
401+
hash: Some(tx.tx_hash()),
402+
index: None,
403+
block_hash: None,
404+
block_number: None,
405+
base_fee: None,
406+
};
407+
let tx = Recovered::new_unchecked(tx, sender);
408+
Ok(Some(self.transform_tx(tx, tx_info)))
409+
} else {
410+
Ok(None)
411+
}
412+
}
413+
}
344414
}

0 commit comments

Comments
 (0)