Skip to content

Commit

Permalink
chore(rpc): define trait RpcNodeCoreExt and replace `LoadBlock::cac…
Browse files Browse the repository at this point in the history
…he` (#12141)
  • Loading branch information
emhane authored Oct 28, 2024
1 parent 1276277 commit b36b021
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
8 changes: 1 addition & 7 deletions crates/optimism/rpc/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +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;
use reth_optimism_chainspec::OpChainSpec;
use reth_primitives::TransactionMeta;
use reth_provider::HeaderProvider;
use reth_rpc_eth_api::{
helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
RpcNodeCore, RpcReceipt,
};
use reth_rpc_eth_types::EthStateCache;

use crate::{OpEthApi, OpEthApiError, OpReceiptBuilder};

Expand Down Expand Up @@ -80,10 +78,6 @@ where
impl<N> LoadBlock for OpEthApi<N>
where
Self: LoadPendingBlock + SpawnBlocking,
N: FullNodeComponents,
N: RpcNodeCore,
{
#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}
}
13 changes: 11 additions & 2 deletions crates/optimism/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use reth_rpc_eth_api::{
AddDevSigners, EthApiSpec, EthFees, EthSigner, EthState, LoadBlock, LoadFee, LoadState,
SpawnBlocking, Trace,
},
EthApiTypes, RpcNodeCore,
EthApiTypes, RpcNodeCore, RpcNodeCoreExt,
};
use reth_rpc_eth_types::{EthStateCache, FeeHistoryCache, GasPriceOracle};
use reth_tasks::{
Expand Down Expand Up @@ -116,7 +116,6 @@ where

impl<N> RpcNodeCore for OpEthApi<N>
where
Self: Clone,
N: RpcNodeCore,
{
type Provider = N::Provider;
Expand All @@ -141,6 +140,16 @@ where
}
}

impl<N> RpcNodeCoreExt for OpEthApi<N>
where
N: RpcNodeCore,
{
#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}
}

impl<N> EthApiSpec for OpEthApi<N>
where
N: RpcNodeCore<
Expand Down
10 changes: 2 additions & 8 deletions crates/rpc/rpc-eth-api/src/helpers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use alloy_rpc_types::{Header, Index};
use futures::Future;
use reth_primitives::{BlockId, Receipt, SealedBlock, SealedBlockWithSenders};
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, RpcReceipt};
use crate::{node::RpcNodeCoreExt, FromEthApiError, FullEthApiTypes, RpcBlock, RpcReceipt};

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

Expand Down Expand Up @@ -196,12 +195,7 @@ pub trait EthBlocks: LoadBlock {
/// Loads a block from database.
///
/// Behaviour shared by several `eth_` RPC methods, not exclusive to `eth_` blocks RPC methods.
pub trait LoadBlock: LoadPendingBlock + SpawnBlocking {
/// Returns a handle for reading data from memory.
///
/// Data access in default (L1) trait method implementations.
fn cache(&self) -> &EthStateCache;

pub trait LoadBlock: LoadPendingBlock + SpawnBlocking + RpcNodeCoreExt {
/// Returns the block object for the given block id.
fn block_with_senders(
&self,
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use bundle::{EthBundleApiServer, EthCallBundleApiServer};
pub use core::{EthApiServer, FullEthApiServer};
pub use filter::EthFilterApiServer;
pub use helpers::error::{AsEthApiError, FromEthApiError, FromEvmError, IntoEthApiError};
pub use node::RpcNodeCore;
pub use node::{RpcNodeCore, RpcNodeCoreExt};
pub use pubsub::EthPubSubApiServer;
pub use types::{EthApiTypes, FullEthApiTypes, RpcBlock, RpcReceipt, RpcTransaction};

Expand Down
8 changes: 8 additions & 0 deletions crates/rpc/rpc-eth-api/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Helper trait for interfacing with [`FullNodeComponents`].

use reth_node_api::FullNodeComponents;
use reth_rpc_eth_types::EthStateCache;

/// Helper trait to relax trait bounds on [`FullNodeComponents`].
///
Expand Down Expand Up @@ -56,3 +57,10 @@ where
FullNodeComponents::provider(self)
}
}

/// Additional components, asides the core node components, needed to run `eth_` namespace API
/// server.
pub trait RpcNodeCoreExt: RpcNodeCore {
/// Returns handle to RPC cache service.
fn cache(&self) -> &EthStateCache;
}
12 changes: 12 additions & 0 deletions crates/rpc/rpc/src/eth/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use reth_primitives::BlockNumberOrTag;
use reth_provider::{BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider};
use reth_rpc_eth_api::{
helpers::{EthSigner, SpawnBlocking},
node::RpcNodeCoreExt,
EthApiTypes, RpcNodeCore,
};
use reth_rpc_eth_types::{
Expand Down Expand Up @@ -169,6 +170,17 @@ where
}
}

impl<Provider, Pool, Network, EvmConfig> RpcNodeCoreExt
for EthApi<Provider, Pool, Network, EvmConfig>
where
Self: RpcNodeCore,
{
#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}
}

impl<Provider, Pool, Network, EvmConfig> std::fmt::Debug
for EthApi<Provider, Pool, Network, EvmConfig>
{
Expand Down
6 changes: 1 addition & 5 deletions crates/rpc/rpc/src/eth/helpers/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reth_rpc_eth_api::{
helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
RpcReceipt,
};
use reth_rpc_eth_types::{EthApiError, EthStateCache, ReceiptBuilder};
use reth_rpc_eth_types::{EthApiError, ReceiptBuilder};

use crate::EthApi;

Expand Down Expand Up @@ -68,8 +68,4 @@ where
Self: LoadPendingBlock + SpawnBlocking,
Provider: BlockReaderIdExt,
{
#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}
}

0 comments on commit b36b021

Please sign in to comment.