chore(rpc): remove redundant EthBlocks::provider (#12109)

This commit is contained in:
Emilia Hane
2024-10-27 17:21:34 +08:00
committed by GitHub
parent 768404c59e
commit 131cc5175e
3 changed files with 21 additions and 36 deletions

View File

@ -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};
@ -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,
@ -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(
@ -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)?
{
@ -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)?
{
@ -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)
@ -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.
@ -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();

View File

@ -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,