Skip to content

Commit b4124dd

Browse files
authored
feat: make RPC cache generic over primitives (#13146)
1 parent 804dc99 commit b4124dd

File tree

31 files changed

+354
-274
lines changed

31 files changed

+354
-274
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/optimism/evm/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ reth-chainspec.workspace = true
1616
reth-ethereum-forks.workspace = true
1717
reth-evm.workspace = true
1818
reth-primitives.workspace = true
19+
reth-primitives-traits.workspace = true
1920
reth-revm.workspace = true
2021
reth-execution-errors.workspace = true
2122
reth-execution-types.workspace = true
@@ -63,6 +64,7 @@ std = [
6364
"alloy-genesis/std",
6465
"alloy-primitives/std",
6566
"revm-primitives/std",
67+
"reth-primitives-traits/std",
6668
"revm/std",
6769
"reth-optimism-primitives/std",
6870
"reth-ethereum-forks/std",

crates/optimism/evm/src/l1.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use reth_chainspec::ChainSpec;
88
use reth_execution_errors::BlockExecutionError;
99
use reth_optimism_chainspec::OpChainSpec;
1010
use reth_optimism_forks::OpHardfork;
11-
use reth_primitives::BlockBody;
11+
use reth_primitives_traits::BlockBody;
1212
use revm::{
1313
primitives::{Bytecode, HashMap, SpecId},
1414
DatabaseCommit, L1BlockInfo,
@@ -32,9 +32,9 @@ const L1_BLOCK_ECOTONE_SELECTOR: [u8; 4] = hex!("440a5e20");
3232
/// transaction in the L2 block.
3333
///
3434
/// Returns an error if the L1 info transaction is not found, if the block is empty.
35-
pub fn extract_l1_info(body: &BlockBody) -> Result<L1BlockInfo, OpBlockExecutionError> {
35+
pub fn extract_l1_info<B: BlockBody>(body: &B) -> Result<L1BlockInfo, OpBlockExecutionError> {
3636
let l1_info_tx_data = body
37-
.transactions
37+
.transactions()
3838
.first()
3939
.ok_or_else(|| OpBlockExecutionError::L1BlockInfoError {
4040
message: "could not find l1 block info tx in the L2 block".to_string(),

crates/optimism/rpc/src/eth/block.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ use reth_primitives::TransactionMeta;
99
use reth_provider::HeaderProvider;
1010
use reth_rpc_eth_api::{
1111
helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
12-
RpcNodeCore, RpcReceipt,
12+
RpcReceipt,
1313
};
1414

15-
use crate::{OpEthApi, OpEthApiError, OpReceiptBuilder};
15+
use crate::{eth::OpNodeCore, OpEthApi, OpEthApiError, OpReceiptBuilder};
1616

1717
impl<N> EthBlocks for OpEthApi<N>
1818
where
1919
Self: LoadBlock<
2020
Error = OpEthApiError,
2121
NetworkTypes: Network<ReceiptResponse = OpTransactionReceipt>,
2222
>,
23-
N: RpcNodeCore<Provider: ChainSpecProvider<ChainSpec = OpChainSpec> + HeaderProvider>,
23+
N: OpNodeCore<Provider: ChainSpecProvider<ChainSpec = OpChainSpec> + HeaderProvider>,
2424
{
2525
async fn block_receipts(
2626
&self,
@@ -77,6 +77,6 @@ where
7777
impl<N> LoadBlock for OpEthApi<N>
7878
where
7979
Self: LoadPendingBlock + SpawnBlocking,
80-
N: RpcNodeCore,
80+
N: OpNodeCore,
8181
{
8282
}

crates/optimism/rpc/src/eth/call.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1+
use super::OpNodeCore;
12
use crate::{OpEthApi, OpEthApiError};
23
use alloy_consensus::Header;
34
use alloy_primitives::{Bytes, TxKind, U256};
45
use alloy_rpc_types_eth::transaction::TransactionRequest;
56
use reth_evm::ConfigureEvm;
67
use reth_rpc_eth_api::{
78
helpers::{estimate::EstimateCall, Call, EthCall, LoadPendingBlock, LoadState, SpawnBlocking},
8-
FromEthApiError, IntoEthApiError, RpcNodeCore,
9+
FromEthApiError, IntoEthApiError,
910
};
1011
use reth_rpc_eth_types::{revm_utils::CallFees, RpcInvalidTransactionError};
1112
use revm::primitives::{BlockEnv, OptimismFields, TxEnv};
1213

1314
impl<N> EthCall for OpEthApi<N>
1415
where
1516
Self: EstimateCall + LoadPendingBlock,
16-
N: RpcNodeCore,
17+
N: OpNodeCore,
1718
{
1819
}
1920

2021
impl<N> EstimateCall for OpEthApi<N>
2122
where
2223
Self: Call,
2324
Self::Error: From<OpEthApiError>,
24-
N: RpcNodeCore,
25+
N: OpNodeCore,
2526
{
2627
}
2728

2829
impl<N> Call for OpEthApi<N>
2930
where
3031
Self: LoadState<Evm: ConfigureEvm<Header = Header>> + SpawnBlocking,
3132
Self::Error: From<OpEthApiError>,
32-
N: RpcNodeCore,
33+
N: OpNodeCore,
3334
{
3435
#[inline]
3536
fn call_gas_limit(&self) -> u64 {

crates/optimism/rpc/src/eth/mod.rs

+29-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod call;
88
mod pending_block;
99

1010
pub use receipt::{OpReceiptBuilder, OpReceiptFieldsBuilder};
11+
use reth_node_api::NodePrimitives;
1112
use reth_optimism_primitives::OpPrimitives;
1213

1314
use std::{fmt, sync::Arc};
@@ -21,7 +22,8 @@ use reth_network_api::NetworkInfo;
2122
use reth_node_builder::EthApiBuilderCtx;
2223
use reth_provider::{
2324
BlockNumReader, BlockReader, BlockReaderIdExt, CanonStateSubscriptions, ChainSpecProvider,
24-
EvmEnvProvider, StageCheckpointReader, StateProviderFactory,
25+
EvmEnvProvider, NodePrimitivesProvider, ProviderBlock, ProviderReceipt, StageCheckpointReader,
26+
StateProviderFactory,
2527
};
2628
use reth_rpc::eth::{core::EthApiInner, DevSigner};
2729
use reth_rpc_eth_api::{
@@ -48,6 +50,10 @@ pub type EthApiNodeBackend<N> = EthApiInner<
4850
<N as RpcNodeCore>::Evm,
4951
>;
5052

53+
/// A helper trait with requirements for [`RpcNodeCore`] to be used in [`OpEthApi`].
54+
pub trait OpNodeCore: RpcNodeCore<Provider: BlockReader> {}
55+
impl<T> OpNodeCore for T where T: RpcNodeCore<Provider: BlockReader> {}
56+
5157
/// OP-Reth `Eth` API implementation.
5258
///
5359
/// This type provides the functionality for handling `eth_` related requests.
@@ -59,14 +65,14 @@ pub type EthApiNodeBackend<N> = EthApiInner<
5965
/// This type implements the [`FullEthApi`](reth_rpc_eth_api::helpers::FullEthApi) by implemented
6066
/// all the `Eth` helper traits and prerequisite traits.
6167
#[derive(Clone)]
62-
pub struct OpEthApi<N: RpcNodeCore> {
68+
pub struct OpEthApi<N: OpNodeCore> {
6369
/// Gateway to node's core components.
6470
inner: Arc<OpEthApiInner<N>>,
6571
}
6672

6773
impl<N> OpEthApi<N>
6874
where
69-
N: RpcNodeCore<
75+
N: OpNodeCore<
7076
Provider: BlockReaderIdExt
7177
+ ChainSpecProvider
7278
+ CanonStateSubscriptions<Primitives = OpPrimitives>
@@ -83,7 +89,7 @@ where
8389
impl<N> EthApiTypes for OpEthApi<N>
8490
where
8591
Self: Send + Sync,
86-
N: RpcNodeCore,
92+
N: OpNodeCore,
8793
{
8894
type Error = OpEthApiError;
8995
type NetworkTypes = Optimism;
@@ -96,7 +102,7 @@ where
96102

97103
impl<N> RpcNodeCore for OpEthApi<N>
98104
where
99-
N: RpcNodeCore,
105+
N: OpNodeCore,
100106
{
101107
type Provider = N::Provider;
102108
type Pool = N::Pool;
@@ -132,17 +138,17 @@ where
132138

133139
impl<N> RpcNodeCoreExt for OpEthApi<N>
134140
where
135-
N: RpcNodeCore,
141+
N: OpNodeCore,
136142
{
137143
#[inline]
138-
fn cache(&self) -> &EthStateCache {
144+
fn cache(&self) -> &EthStateCache<ProviderBlock<N::Provider>, ProviderReceipt<N::Provider>> {
139145
self.inner.eth_api.cache()
140146
}
141147
}
142148

143149
impl<N> EthApiSpec for OpEthApi<N>
144150
where
145-
N: RpcNodeCore<
151+
N: OpNodeCore<
146152
Provider: ChainSpecProvider<ChainSpec: EthereumHardforks>
147153
+ BlockNumReader
148154
+ StageCheckpointReader,
@@ -163,7 +169,7 @@ where
163169
impl<N> SpawnBlocking for OpEthApi<N>
164170
where
165171
Self: Send + Sync + Clone + 'static,
166-
N: RpcNodeCore,
172+
N: OpNodeCore,
167173
{
168174
#[inline]
169175
fn io_task_spawner(&self) -> impl TaskSpawner {
@@ -184,7 +190,7 @@ where
184190
impl<N> LoadFee for OpEthApi<N>
185191
where
186192
Self: LoadBlock<Provider = N::Provider>,
187-
N: RpcNodeCore<
193+
N: OpNodeCore<
188194
Provider: BlockReaderIdExt
189195
+ EvmEnvProvider
190196
+ ChainSpecProvider<ChainSpec: EthChainSpec + EthereumHardforks>
@@ -203,7 +209,7 @@ where
203209
}
204210

205211
impl<N> LoadState for OpEthApi<N> where
206-
N: RpcNodeCore<
212+
N: OpNodeCore<
207213
Provider: StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks>,
208214
Pool: TransactionPool,
209215
>
@@ -213,7 +219,7 @@ impl<N> LoadState for OpEthApi<N> where
213219
impl<N> EthState for OpEthApi<N>
214220
where
215221
Self: LoadState + SpawnBlocking,
216-
N: RpcNodeCore,
222+
N: OpNodeCore,
217223
{
218224
#[inline]
219225
fn max_proof_window(&self) -> u64 {
@@ -224,35 +230,35 @@ where
224230
impl<N> EthFees for OpEthApi<N>
225231
where
226232
Self: LoadFee,
227-
N: RpcNodeCore,
233+
N: OpNodeCore,
228234
{
229235
}
230236

231237
impl<N> Trace for OpEthApi<N>
232238
where
233239
Self: RpcNodeCore<Provider: BlockReader> + LoadState<Evm: ConfigureEvm<Header = Header>>,
234-
N: RpcNodeCore,
240+
N: OpNodeCore,
235241
{
236242
}
237243

238244
impl<N> AddDevSigners for OpEthApi<N>
239245
where
240-
N: RpcNodeCore,
246+
N: OpNodeCore,
241247
{
242248
fn with_dev_accounts(&self) {
243249
*self.inner.eth_api.signers().write() = DevSigner::random_signers(20)
244250
}
245251
}
246252

247-
impl<N: RpcNodeCore> fmt::Debug for OpEthApi<N> {
253+
impl<N: OpNodeCore> fmt::Debug for OpEthApi<N> {
248254
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249255
f.debug_struct("OpEthApi").finish_non_exhaustive()
250256
}
251257
}
252258

253259
/// Container type `OpEthApi`
254260
#[allow(missing_debug_implementations)]
255-
struct OpEthApiInner<N: RpcNodeCore> {
261+
struct OpEthApiInner<N: OpNodeCore> {
256262
/// Gateway to node's core components.
257263
eth_api: EthApiNodeBackend<N>,
258264
/// Sequencer client, configured to forward submitted transactions to sequencer of given OP
@@ -285,10 +291,12 @@ impl OpEthApiBuilder {
285291
/// Builds an instance of [`OpEthApi`]
286292
pub fn build<N>(self, ctx: &EthApiBuilderCtx<N>) -> OpEthApi<N>
287293
where
288-
N: RpcNodeCore<
289-
Provider: BlockReaderIdExt
290-
+ ChainSpecProvider
291-
+ CanonStateSubscriptions<Primitives = OpPrimitives>
294+
N: OpNodeCore<
295+
Provider: BlockReaderIdExt<
296+
Block = <<N::Provider as NodePrimitivesProvider>::Primitives as NodePrimitives>::Block,
297+
Receipt = <<N::Provider as NodePrimitivesProvider>::Primitives as NodePrimitives>::Receipt,
298+
> + ChainSpecProvider
299+
+ CanonStateSubscriptions
292300
+ Clone
293301
+ 'static,
294302
>,

crates/optimism/rpc/src/eth/transaction.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ use reth_primitives::{RecoveredTx, TransactionSigned};
1010
use reth_provider::{BlockReaderIdExt, ReceiptProvider, TransactionsProvider};
1111
use reth_rpc_eth_api::{
1212
helpers::{EthSigner, EthTransactions, LoadTransaction, SpawnBlocking},
13-
FromEthApiError, FullEthApiTypes, RpcNodeCore, TransactionCompat,
13+
FromEthApiError, FullEthApiTypes, RpcNodeCore, RpcNodeCoreExt, TransactionCompat,
1414
};
1515
use reth_rpc_eth_types::utils::recover_raw_transaction;
1616
use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool};
1717

18-
use crate::{OpEthApi, OpEthApiError, SequencerClient};
18+
use crate::{eth::OpNodeCore, OpEthApi, OpEthApiError, SequencerClient};
1919

2020
impl<N> EthTransactions for OpEthApi<N>
2121
where
2222
Self: LoadTransaction<Provider: BlockReaderIdExt>,
23-
N: RpcNodeCore,
23+
N: OpNodeCore,
2424
{
2525
fn signers(&self) -> &parking_lot::RwLock<Vec<Box<dyn EthSigner>>> {
2626
self.inner.eth_api.signers()
@@ -56,15 +56,15 @@ where
5656

5757
impl<N> LoadTransaction for OpEthApi<N>
5858
where
59-
Self: SpawnBlocking + FullEthApiTypes,
60-
N: RpcNodeCore<Provider: TransactionsProvider, Pool: TransactionPool>,
59+
Self: SpawnBlocking + FullEthApiTypes + RpcNodeCoreExt,
60+
N: OpNodeCore<Provider: TransactionsProvider, Pool: TransactionPool>,
6161
Self::Pool: TransactionPool,
6262
{
6363
}
6464

6565
impl<N> OpEthApi<N>
6666
where
67-
N: RpcNodeCore,
67+
N: OpNodeCore,
6868
{
6969
/// Returns the [`SequencerClient`] if one is set.
7070
pub fn raw_tx_forwarder(&self) -> Option<SequencerClient> {

crates/rpc/rpc-builder/src/eth.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pub type DynEthApiBuilder<Provider, Pool, EvmConfig, Network, Tasks, Events, Eth
1515

1616
/// Handlers for core, filter and pubsub `eth` namespace APIs.
1717
#[derive(Debug, Clone)]
18-
pub struct EthHandlers<Provider, Pool, Network, Events, EthApi: EthApiTypes> {
18+
pub struct EthHandlers<Provider: BlockReader, Pool, Network, Events, EthApi: EthApiTypes> {
1919
/// Main `eth_` request handler
2020
pub api: EthApi,
2121
/// The async caching layer used by the eth handlers
22-
pub cache: EthStateCache,
22+
pub cache: EthStateCache<Provider::Block, Provider::Receipt>,
2323
/// Polling based filter handler available on all transports
2424
pub filter: EthFilter<Provider, Pool, EthApi>,
2525
/// Handler for subscriptions only available for transports that support it (ws, ipc)

crates/rpc/rpc-builder/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ impl RpcModuleConfigBuilder {
925925
/// A Helper type the holds instances of the configured modules.
926926
#[derive(Debug, Clone)]
927927
pub struct RpcRegistryInner<
928-
Provider,
928+
Provider: BlockReader,
929929
Pool,
930930
Network,
931931
Tasks,
@@ -1029,6 +1029,7 @@ where
10291029
impl<Provider, Pool, Network, Tasks, Events, EthApi, BlockExecutor, Consensus>
10301030
RpcRegistryInner<Provider, Pool, Network, Tasks, Events, EthApi, BlockExecutor, Consensus>
10311031
where
1032+
Provider: BlockReader,
10321033
EthApi: EthApiTypes,
10331034
{
10341035
/// Returns a reference to the installed [`EthApi`](reth_rpc::eth::EthApi).
@@ -1045,7 +1046,7 @@ where
10451046
///
10461047
/// This will spawn exactly one [`EthStateCache`] service if this is the first time the cache is
10471048
/// requested.
1048-
pub const fn eth_cache(&self) -> &EthStateCache {
1049+
pub const fn eth_cache(&self) -> &EthStateCache<Provider::Block, Provider::Receipt> {
10491050
&self.eth.cache
10501051
}
10511052

@@ -1089,7 +1090,7 @@ impl<Provider, Pool, Network, Tasks, Events, EthApi, BlockExecutor, Consensus>
10891090
where
10901091
Network: NetworkInfo + Clone + 'static,
10911092
EthApi: EthApiTypes,
1092-
Provider: ChainSpecProvider<ChainSpec: EthereumHardforks>,
1093+
Provider: BlockReader + ChainSpecProvider<ChainSpec: EthereumHardforks>,
10931094
BlockExecutor: BlockExecutorProvider,
10941095
{
10951096
/// Instantiates `AdminApi`

0 commit comments

Comments
 (0)