Skip to content

Commit 37150ff

Browse files
fix(rpc): check canonical chain first in tx lookups (#259)
1 parent c5e105d commit 37150ff

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

crates/rpc/src/eth/rpc.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,22 @@ where
174174
tx_hash = %tx_hash
175175
);
176176

177+
// Check canonical chain first to avoid race condition where flashblocks
178+
// state hasn't been cleared yet after canonical block commit
179+
if let Some(canonical_receipt) =
180+
EthTransactions::transaction_receipt(&self.eth_api, tx_hash).await?
181+
{
182+
return Ok(Some(canonical_receipt));
183+
}
184+
185+
// Fall back to flashblocks for pending transactions
177186
let pending_blocks = self.flashblocks_state.get_pending_blocks();
178187
if let Some(fb_receipt) = pending_blocks.get_transaction_receipt(tx_hash) {
179188
self.metrics.get_transaction_receipt.increment(1);
180189
return Ok(Some(fb_receipt));
181190
}
182191

183-
EthTransactions::transaction_receipt(&self.eth_api, tx_hash).await.map_err(Into::into)
192+
Ok(None)
184193
}
185194

186195
async fn get_balance(
@@ -241,17 +250,24 @@ where
241250
tx_hash = %tx_hash
242251
);
243252

244-
let pending_blocks = self.flashblocks_state.get_pending_blocks();
253+
// Check canonical chain first to avoid race condition where flashblocks
254+
// state hasn't been cleared yet after canonical block commit
255+
if let Some(canonical_tx) = EthTransactions::transaction_by_hash(&self.eth_api, tx_hash)
256+
.await?
257+
.map(|tx| tx.into_transaction(self.eth_api.tx_resp_builder()))
258+
.transpose()?
259+
{
260+
return Ok(Some(canonical_tx));
261+
}
245262

263+
// Fall back to flashblocks for pending transactions
264+
let pending_blocks = self.flashblocks_state.get_pending_blocks();
246265
if let Some(fb_transaction) = pending_blocks.get_transaction_by_hash(tx_hash) {
247-
self.metrics.get_transaction_receipt.increment(1);
266+
self.metrics.get_transaction_by_hash.increment(1);
248267
return Ok(Some(fb_transaction));
249268
}
250269

251-
Ok(EthTransactions::transaction_by_hash(&self.eth_api, tx_hash)
252-
.await?
253-
.map(|tx| tx.into_transaction(self.eth_api.tx_resp_builder()))
254-
.transpose()?)
270+
Ok(None)
255271
}
256272

257273
async fn send_raw_transaction_sync(

crates/rpc/src/metrics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub(crate) struct Metrics {
1414
#[metric(describe = "Count of times flashblocks get_transaction_receipt is called")]
1515
pub get_transaction_receipt: Counter,
1616

17+
#[metric(describe = "Count of times flashblocks get_transaction_by_hash is called")]
18+
pub get_transaction_by_hash: Counter,
19+
1720
#[metric(describe = "Count of times flashblocks get_balance is called")]
1821
pub get_balance: Counter,
1922

0 commit comments

Comments
 (0)