From 28897d7c1cfb024f1b711c555279d3567ce8be88 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 26 Jun 2023 19:20:28 +0200 Subject: [PATCH] perf: fetch receipts and transactions concurrently (#3406) --- crates/rpc/rpc/src/eth/api/fees.rs | 49 ++++++++++++++---------------- crates/rpc/rpc/src/eth/cache.rs | 13 ++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/crates/rpc/rpc/src/eth/api/fees.rs b/crates/rpc/rpc/src/eth/api/fees.rs index 630c0c2305..5122fb0627 100644 --- a/crates/rpc/rpc/src/eth/api/fees.rs +++ b/crates/rpc/rpc/src/eth/api/fees.rs @@ -131,36 +131,31 @@ where percentiles: &[f64], header: &SealedHeader, ) -> Result, EthApiError> { - let Some(receipts) = - self.cache().get_receipts(header.hash).await? else { - // If there are no receipts, then we do not have all info on the block - return Err(EthApiError::InvalidBlockRange) - }; - let Some(mut transactions): Option> = self + let (transactions, receipts) = self .cache() - .get_block_transactions(header.hash).await? - .map(|txs|txs - .into_iter() - .zip(receipts.into_iter()) - .scan(0, |previous_gas, (tx, receipt)| { - // Convert the cumulative gas used in the receipts - // to the gas usage by the transaction - // - // While we will sum up the gas again later, it is worth - // noting that the order of the transactions will be different, - // so the sum will also be different for each receipt. - let gas_used = receipt.cumulative_gas_used - *previous_gas; - *previous_gas = receipt.cumulative_gas_used; + .get_transactions_and_receipts(header.hash) + .await? + .ok_or(EthApiError::InvalidBlockRange)?; - Some(TxGasAndReward { - gas_used, - reward: tx.effective_gas_tip(header.base_fee_per_gas).unwrap_or_default(), - }) + let mut transactions = transactions + .into_iter() + .zip(receipts.into_iter()) + .scan(0, |previous_gas, (tx, receipt)| { + // Convert the cumulative gas used in the receipts + // to the gas usage by the transaction + // + // While we will sum up the gas again later, it is worth + // noting that the order of the transactions will be different, + // so the sum will also be different for each receipt. + let gas_used = receipt.cumulative_gas_used - *previous_gas; + *previous_gas = receipt.cumulative_gas_used; + + Some(TxGasAndReward { + gas_used, + reward: tx.effective_gas_tip(header.base_fee_per_gas).unwrap_or_default(), }) - .collect()) else { - // If there are no transactions, then we do not have all info on the block - return Err(EthApiError::InvalidBlockRange) - }; + }) + .collect::>(); // Sort the transactions by their rewards in ascending order transactions.sort_by_key(|tx| tx.reward); diff --git a/crates/rpc/rpc/src/eth/cache.rs b/crates/rpc/rpc/src/eth/cache.rs index 8a580f36e2..3adf1b7892 100644 --- a/crates/rpc/rpc/src/eth/cache.rs +++ b/crates/rpc/rpc/src/eth/cache.rs @@ -179,6 +179,19 @@ impl EthStateCache { rx.await.map_err(|_| ProviderError::CacheServiceUnavailable)? } + /// Fetches both transactions and receipts for the given block hash. + pub(crate) async fn get_transactions_and_receipts( + &self, + block_hash: H256, + ) -> Result, Vec)>> { + let transactions = self.get_block_transactions(block_hash); + let receipts = self.get_receipts(block_hash); + + let (transactions, receipts) = futures::try_join!(transactions, receipts)?; + + Ok(transactions.zip(receipts)) + } + /// Requests the [Receipt] for the block hash /// /// Returns `None` if the block was not found.