Skip to content

Commit

Permalink
chore(rpc): set RpcNodeCore as supertrait for LoadState (#12094)
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane authored Oct 26, 2024
1 parent 019f347 commit d5f5c0f
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 70 deletions.
19 changes: 4 additions & 15 deletions crates/optimism/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ where

impl<N> EthApiSpec for OpEthApi<N>
where
Self: Send + Sync,
N: RpcNodeCore<
Provider: ChainSpecProvider<ChainSpec: EthereumHardforks>
+ BlockNumReader
Expand Down Expand Up @@ -213,25 +212,15 @@ where

impl<N> LoadState for OpEthApi<N>
where
Self: Send + Sync + Clone,
N: FullNodeComponents<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
N: RpcNodeCore<
Provider: StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks>,
Pool: TransactionPool,
>,
{
#[inline]
fn provider(
&self,
) -> impl StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks> {
self.inner.provider()
}

#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}

#[inline]
fn pool(&self) -> impl TransactionPool {
self.inner.pool()
}
}

impl<N> EthState for OpEthApi<N>
Expand Down
8 changes: 4 additions & 4 deletions crates/rpc/rpc-eth-api/src/helpers/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ pub trait Call: LoadState + SpawnBlocking {
DB: Database,
EthApiError: From<DB::Error>,
{
let mut evm = self.evm_config().evm_with_env(db, env);
let mut evm = Call::evm_config(self).evm_with_env(db, env);
let res = evm.transact().map_err(Self::Error::from_evm_err)?;
let (_, env) = evm.into_db_and_env_with_handler_cfg();
Ok((res, env))
Expand All @@ -504,7 +504,7 @@ pub trait Call: LoadState + SpawnBlocking {
DB: Database,
EthApiError: From<DB::Error>,
{
let mut evm = self.evm_config().evm_with_env_and_inspector(db, env, inspector);
let mut evm = Call::evm_config(self).evm_with_env_and_inspector(db, env, inspector);
let res = evm.transact().map_err(Self::Error::from_evm_err)?;
let (_, env) = evm.into_db_and_env_with_handler_cfg();
Ok((res, env))
Expand Down Expand Up @@ -669,15 +669,15 @@ pub trait Call: LoadState + SpawnBlocking {
{
let env = EnvWithHandlerCfg::new_with_cfg_env(cfg, block_env, Default::default());

let mut evm = self.evm_config().evm_with_env(db, env);
let mut evm = Call::evm_config(self).evm_with_env(db, env);
let mut index = 0;
for (sender, tx) in transactions {
if tx.hash() == target_tx_hash {
// reached the target transaction
break
}

self.evm_config().fill_tx_env(evm.tx_mut(), tx, *sender);
Call::evm_config(self).fill_tx_env(evm.tx_mut(), tx, *sender);
evm.transact_commit().map_err(Self::Error::from_evm_err)?;
index += 1;
}
Expand Down
11 changes: 5 additions & 6 deletions crates/rpc/rpc-eth-api/src/helpers/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ use crate::{helpers::EthSigner, RpcNodeCore};
#[auto_impl::auto_impl(&, Arc)]
pub trait EthApiSpec:
RpcNodeCore<
Provider: ChainSpecProvider<ChainSpec: EthereumHardforks>
+ BlockNumReader
+ StageCheckpointReader,
Network: NetworkInfo,
> + Send
+ Sync
Provider: ChainSpecProvider<ChainSpec: EthereumHardforks>
+ BlockNumReader
+ StageCheckpointReader,
Network: NetworkInfo,
>
{
/// Returns the block node is started on.
fn starting_block(&self) -> U256;
Expand Down
34 changes: 15 additions & 19 deletions crates/rpc/rpc-eth-api/src/helpers/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use reth_rpc_types_compat::proof::from_primitive_account_proof;
use reth_transaction_pool::TransactionPool;
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId};

use crate::{EthApiTypes, FromEthApiError};
use crate::{EthApiTypes, FromEthApiError, RpcNodeCore};

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

Expand Down Expand Up @@ -105,7 +105,8 @@ pub trait EthState: LoadState + SpawnBlocking {
let block_id = block_id.unwrap_or_default();

// Check whether the distance to the block exceeds the maximum configured window.
let block_number = LoadState::provider(self)
let block_number = self
.provider()
.block_number_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(block_id))?;
Expand Down Expand Up @@ -138,9 +139,9 @@ pub trait EthState: LoadState + SpawnBlocking {
let Some(account) = account else { return Ok(None) };

// Check whether the distance to the block exceeds the maximum configured proof window.
let chain_info =
LoadState::provider(&this).chain_info().map_err(Self::Error::from_eth_err)?;
let block_number = LoadState::provider(&this)
let chain_info = this.provider().chain_info().map_err(Self::Error::from_eth_err)?;
let block_number = this
.provider()
.block_number_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
.ok_or(EthApiError::HeaderNotFound(block_id))?;
Expand All @@ -167,24 +168,19 @@ pub trait EthState: LoadState + SpawnBlocking {
/// Loads state from database.
///
/// Behaviour shared by several `eth_` RPC methods, not exclusive to `eth_` state RPC methods.
pub trait LoadState: EthApiTypes {
/// Returns a handle for reading state from database.
///
/// Data access in default trait method implementations.
fn provider(
&self,
) -> impl StateProviderFactory + ChainSpecProvider<ChainSpec: EthChainSpec + EthereumHardforks>;

pub trait LoadState:
EthApiTypes
+ RpcNodeCore<
Provider: StateProviderFactory
+ ChainSpecProvider<ChainSpec: EthChainSpec + EthereumHardforks>,
Pool: TransactionPool,
>
{
/// Returns a handle for reading data from memory.
///
/// Data access in default (L1) trait method implementations.
fn cache(&self) -> &EthStateCache;

/// Returns a handle for reading data from transaction pool.
///
/// Data access in default trait method implementations.
fn pool(&self) -> impl TransactionPool;

/// Returns the state at the given block number
fn state_at_hash(&self, block_hash: B256) -> Result<StateProviderBox, Self::Error> {
self.provider().history_by_block_hash(block_hash).map_err(Self::Error::from_eth_err)
Expand Down Expand Up @@ -266,7 +262,7 @@ pub trait LoadState: EthApiTypes {
let (cfg, mut block_env, _) = self.evm_env_at(header.parent_hash.into()).await?;

let after_merge = cfg.handler_cfg.spec_id >= SpecId::MERGE;
self.evm_config().fill_block_env(&mut block_env, header, after_merge);
LoadPendingBlock::evm_config(self).fill_block_env(&mut block_env, header, after_merge);

Ok((cfg, block_env))
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rpc/rpc-eth-api/src/helpers/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::sync::Arc;

use crate::FromEvmError;
use crate::{FromEvmError, RpcNodeCore};
use alloy_primitives::B256;
use alloy_rpc_types::{BlockId, TransactionInfo};
use futures::Future;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub trait Trace: LoadState {

I: GetInspector<DB>,
{
let mut evm = self.evm_config().evm_with_env_and_inspector(db, env, inspector);
let mut evm = Trace::evm_config(self).evm_with_env_and_inspector(db, env, inspector);
let res = evm.transact().map_err(Self::Error::from_evm_err)?;
let (db, env) = evm.into_db_and_env_with_handler_cfg();
Ok((res, env, db))
Expand Down Expand Up @@ -202,7 +202,7 @@ pub trait Trace: LoadState {
// apply relevant system calls
let mut system_caller = SystemCaller::new(
Trace::evm_config(&this).clone(),
LoadState::provider(&this).chain_spec(),
RpcNodeCore::provider(&this).chain_spec(),
);
system_caller
.pre_block_beacon_root_contract_call(
Expand Down Expand Up @@ -345,7 +345,7 @@ pub trait Trace: LoadState {
// apply relevant system calls
let mut system_caller = SystemCaller::new(
Trace::evm_config(&this).clone(),
LoadState::provider(&this).chain_spec(),
RpcNodeCore::provider(&this).chain_spec(),
);
system_caller
.pre_block_beacon_root_contract_call(
Expand Down
6 changes: 4 additions & 2 deletions crates/rpc/rpc-eth-api/src/helpers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use reth_rpc_types_compat::transaction::{from_recovered, from_recovered_with_blo
use reth_transaction_pool::{PoolTransaction, TransactionOrigin, TransactionPool};
use std::sync::Arc;

use crate::{FromEthApiError, FullEthApiTypes, IntoEthApiError, RpcReceipt, RpcTransaction};
use crate::{
FromEthApiError, FullEthApiTypes, IntoEthApiError, RpcNodeCore, RpcReceipt, RpcTransaction,
};

use super::{
Call, EthApiSpec, EthSigner, LoadBlock, LoadPendingBlock, LoadReceipt, LoadState, SpawnBlocking,
Expand Down Expand Up @@ -235,7 +237,7 @@ pub trait EthTransactions: LoadTransaction {
// Check the pool first
if include_pending {
if let Some(tx) =
LoadState::pool(self).get_transaction_by_sender_and_nonce(sender, nonce)
RpcNodeCore::pool(self).get_transaction_by_sender_and_nonce(sender, nonce)
{
let transaction = tx.transaction.clone().into_consensus();
return Ok(Some(from_recovered(transaction.into(), self.tx_resp_builder())));
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-api/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reth_node_api::FullNodeComponents;
/// `N: RpcNodeCore` instead, allows access to all the associated types on [`FullNodeComponents`]
/// that are used in RPC, but with more flexibility since they have no trait bounds (asides auto
/// traits).
pub trait RpcNodeCore: Clone {
pub trait RpcNodeCore: Clone + Send + Sync {
/// The provider type used to interact with the node.
type Provider: Send + Sync + Clone + Unpin;
/// The transaction pool of the node.
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use reth_provider::{
use reth_revm::database::StateProviderDatabase;
use reth_rpc_api::DebugApiServer;
use reth_rpc_eth_api::{
helpers::{Call, EthApiSpec, EthTransactions, LoadState, TraceExt},
EthApiTypes, FromEthApiError,
helpers::{Call, EthApiSpec, EthTransactions, TraceExt},
EthApiTypes, FromEthApiError, RpcNodeCore,
};
use reth_rpc_eth_types::{EthApiError, StateCacheDb};
use reth_rpc_server_types::{result::internal_rpc_err, ToRpcResult};
Expand Down Expand Up @@ -264,7 +264,7 @@ where
// apply relevant system calls
let mut system_caller = SystemCaller::new(
Call::evm_config(this.eth_api()).clone(),
LoadState::provider(this.eth_api()).chain_spec(),
RpcNodeCore::provider(this.eth_api()).chain_spec(),
);

system_caller
Expand Down
24 changes: 8 additions & 16 deletions crates/rpc/rpc/src/eth/helpers/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use reth_chainspec::EthereumHardforks;
use reth_provider::{ChainSpecProvider, StateProviderFactory};
use reth_transaction_pool::TransactionPool;

use reth_rpc_eth_api::helpers::{EthState, LoadState, SpawnBlocking};
use reth_rpc_eth_api::{
helpers::{EthState, LoadState, SpawnBlocking},
RpcNodeCore,
};
use reth_rpc_eth_types::EthStateCache;

use crate::EthApi;
Expand All @@ -20,26 +23,15 @@ where

impl<Provider, Pool, Network, EvmConfig> LoadState for EthApi<Provider, Pool, Network, EvmConfig>
where
Self: Send + Sync,
Provider: StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks>,
Pool: TransactionPool,
Self: RpcNodeCore<
Provider: StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks>,
Pool: TransactionPool,
>,
{
#[inline]
fn provider(
&self,
) -> impl StateProviderFactory + ChainSpecProvider<ChainSpec: EthereumHardforks> {
self.inner.provider()
}

#[inline]
fn cache(&self) -> &EthStateCache {
self.inner.cache()
}

#[inline]
fn pool(&self) -> impl TransactionPool {
self.inner.pool()
}
}

#[cfg(test)]
Expand Down

0 comments on commit d5f5c0f

Please sign in to comment.