Skip to content
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
39 changes: 29 additions & 10 deletions crates/rpc/rpc-eth-api/src/helpers/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait LoadReceipt:
{
/// Helper method for `eth_getBlockReceipts` and `eth_getTransactionReceipt`.
///
/// If `all_receipts` is `Some`, skips the cache lookup for receipts entirely.
/// If a value is `Some`, skips the corresponding cache lookup entirely.
fn build_transaction_receipt(
&self,
tx: Recovered<ProviderTx<Self::Provider>>,
Expand All @@ -32,15 +32,34 @@ pub trait LoadReceipt:
) -> impl Future<Output = Result<RpcReceipt<Self::NetworkTypes>, Self::Error>> + Send {
async move {
let hash = meta.block_hash;
// Use pre-fetched receipts if available, otherwise fetch from cache.
let all_receipts = match all_receipts {
Some(receipts) => receipts,
None => self
.cache()
.get_receipts(hash)
.await
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(hash.into()))?,
let (block, all_receipts) = match (block, all_receipts) {
(Some(block), Some(all_receipts)) => (Some(block), all_receipts),
(Some(block), None) => {
let all_receipts = self
.cache()
.get_receipts(hash)
.await
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(hash.into()))?;
(Some(block), all_receipts)
}
(None, Some(all_receipts)) => {
let block = self
.cache()
.get_maybe_block(hash)
.await
.map_err(Self::Error::from_eth_err)?;
(block, all_receipts)
}
(None, None) => {
let (all_receipts, block) = self
.cache()
.get_receipts_and_maybe_block(hash)
.await
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(hash.into()))?;
(block, all_receipts)
}
};

let (gas_used, next_log_index) =
Expand Down
10 changes: 10 additions & 0 deletions crates/rpc/rpc-eth-types/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ impl<N: NodePrimitives> EthStateCache<N> {
rx.await.map_err(|_| CacheServiceUnavailable)?
}

/// Requests the block for the given block hash if it is cached.
pub async fn get_maybe_block(
&self,
block_hash: B256,
) -> ProviderResult<Option<Arc<RecoveredBlock<N::Block>>>> {
let (response_tx, rx) = oneshot::channel();
let _ = self.to_service.send(CacheAction::GetCachedBlock { block_hash, response_tx });
rx.await.map_err(|_| CacheServiceUnavailable.into())
}

/// Requests the receipts for the block hash
///
/// Returns `None` if the block was not found.
Expand Down
Loading