Skip to content

Commit

Permalink
chore(rpc): remove redundant EthBlocks::provider (#12109)
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane authored Oct 27, 2024
1 parent 768404c commit 131cc51
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
11 changes: 3 additions & 8 deletions crates/optimism/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use alloy_rpc_types::BlockId;
use op_alloy_network::Network;
use op_alloy_rpc_types::OpTransactionReceipt;
use reth_chainspec::ChainSpecProvider;
use reth_node_api::{FullNodeComponents, NodeTypes};
use reth_node_api::FullNodeComponents;
use reth_optimism_chainspec::OpChainSpec;
use reth_primitives::TransactionMeta;
use reth_provider::HeaderProvider;
use reth_rpc_eth_api::{
helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
RpcReceipt,
RpcNodeCore, RpcReceipt,
};
use reth_rpc_eth_types::EthStateCache;

Expand All @@ -22,13 +22,8 @@ where
Error = OpEthApiError,
NetworkTypes: Network<ReceiptResponse = OpTransactionReceipt>,
>,
N: FullNodeComponents<Types: NodeTypes<ChainSpec = OpChainSpec>>,
N: RpcNodeCore<Provider: ChainSpecProvider<ChainSpec = OpChainSpec> + HeaderProvider>,
{
#[inline]
fn provider(&self) -> impl HeaderProvider {
self.inner.provider()
}

async fn block_receipts(
&self,
block_id: BlockId,
Expand Down
39 changes: 17 additions & 22 deletions crates/rpc/rpc-eth-api/src/helpers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use reth_provider::{BlockIdReader, BlockReader, BlockReaderIdExt, HeaderProvider
use reth_rpc_eth_types::EthStateCache;
use reth_rpc_types_compat::block::{from_block, uncle_block_from_header};

use crate::{FromEthApiError, FullEthApiTypes, RpcBlock, RpcNodeCore, RpcReceipt};
use crate::{FromEthApiError, FullEthApiTypes, RpcBlock, RpcReceipt};

use super::{LoadPendingBlock, LoadReceipt, SpawnBlocking};

Expand All @@ -20,12 +20,7 @@ pub type BlockAndReceiptsResult<E> = Result<Option<(SealedBlock, Arc<Vec<Receipt

/// Block related functions for the [`EthApiServer`](crate::EthApiServer) trait in the
/// `eth_` namespace.
pub trait EthBlocks: LoadBlock {
/// Returns a handle for reading data from disk.
///
/// Data access in default (L1) trait method implementations.
fn provider(&self) -> impl HeaderProvider;

pub trait EthBlocks: LoadBlock<Provider: HeaderProvider> {
/// Returns the block header for the given block id.
fn rpc_block_header(
&self,
Expand All @@ -52,15 +47,15 @@ pub trait EthBlocks: LoadBlock {
async move {
let Some(block) = self.block_with_senders(block_id).await? else { return Ok(None) };
let block_hash = block.hash();
let mut total_difficulty = EthBlocks::provider(self)
let mut total_difficulty = self
.provider()
.header_td_by_number(block.number)
.map_err(Self::Error::from_eth_err)?;
if total_difficulty.is_none() {
// if we failed to find td after we successfully loaded the block, try again using
// the hash this only matters if the chain is currently transitioning the merge block and there's a reorg: <https://github.com/paradigmxyz/reth/issues/10941>
total_difficulty = EthBlocks::provider(self)
.header_td(&block.hash())
.map_err(Self::Error::from_eth_err)?;
total_difficulty =
self.provider().header_td(&block.hash()).map_err(Self::Error::from_eth_err)?;
}

let block = from_block(
Expand All @@ -85,13 +80,15 @@ pub trait EthBlocks: LoadBlock {
async move {
if block_id.is_pending() {
// Pending block can be fetched directly without need for caching
return Ok(RpcNodeCore::provider(self)
return Ok(self
.provider()
.pending_block()
.map_err(Self::Error::from_eth_err)?
.map(|block| block.body.transactions.len()))
}

let block_hash = match RpcNodeCore::provider(self)
let block_hash = match self
.provider()
.block_hash_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
{
Expand Down Expand Up @@ -132,7 +129,8 @@ pub trait EthBlocks: LoadBlock {
if block_id.is_pending() {
// First, try to get the pending block from the provider, in case we already
// received the actual pending block from the CL.
if let Some((block, receipts)) = RpcNodeCore::provider(self)
if let Some((block, receipts)) = self
.provider()
.pending_block_and_receipts()
.map_err(Self::Error::from_eth_err)?
{
Expand All @@ -145,9 +143,8 @@ pub trait EthBlocks: LoadBlock {
}
}

if let Some(block_hash) = RpcNodeCore::provider(self)
.block_hash_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
if let Some(block_hash) =
self.provider().block_hash_for_id(block_id).map_err(Self::Error::from_eth_err)?
{
return LoadReceipt::cache(self)
.get_block_and_receipts(block_hash)
Expand All @@ -167,7 +164,7 @@ pub trait EthBlocks: LoadBlock {
&self,
block_id: BlockId,
) -> Result<Option<Vec<reth_primitives::Header>>, Self::Error> {
RpcNodeCore::provider(self).ommers_by_id(block_id).map_err(Self::Error::from_eth_err)
self.provider().ommers_by_id(block_id).map_err(Self::Error::from_eth_err)
}

/// Returns uncle block at given index in given block.
Expand All @@ -182,14 +179,12 @@ pub trait EthBlocks: LoadBlock {
async move {
let uncles = if block_id.is_pending() {
// Pending block can be fetched directly without need for caching
RpcNodeCore::provider(self)
self.provider()
.pending_block()
.map_err(Self::Error::from_eth_err)?
.map(|block| block.body.ommers)
} else {
RpcNodeCore::provider(self)
.ommers_by_id(block_id)
.map_err(Self::Error::from_eth_err)?
self.provider().ommers_by_id(block_id).map_err(Self::Error::from_eth_err)?
}
.unwrap_or_default();

Expand Down
7 changes: 1 addition & 6 deletions crates/rpc/rpc/src/eth/helpers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ where
Self: LoadBlock<
Error = EthApiError,
NetworkTypes: alloy_network::Network<ReceiptResponse = AnyTransactionReceipt>,
Provider: HeaderProvider,
>,
Provider: HeaderProvider,
{
#[inline]
fn provider(&self) -> impl HeaderProvider {
self.inner.provider()
}

async fn block_receipts(
&self,
block_id: BlockId,
Expand Down

0 comments on commit 131cc51

Please sign in to comment.