diff --git a/crates/optimism/rpc/src/eth/mod.rs b/crates/optimism/rpc/src/eth/mod.rs index fead8b490d1..9c34c723bc1 100644 --- a/crates/optimism/rpc/src/eth/mod.rs +++ b/crates/optimism/rpc/src/eth/mod.rs @@ -23,7 +23,7 @@ use reth_rpc::eth::{core::EthApiInner, DevSigner}; use reth_rpc_eth_api::{ helpers::{ pending_block::BuildPendingEnv, spec::SignersForApi, AddDevSigners, EthApiSpec, EthFees, - EthState, LoadFee, LoadState, SpawnBlocking, Trace, + EthState, LoadFee, LoadPendingBlock, LoadState, SpawnBlocking, Trace, }, EthApiTypes, FromEvmError, FullEthApiServer, RpcConvert, RpcConverter, RpcNodeCore, RpcNodeCoreExt, RpcTypes, SignableTxRequest, @@ -210,6 +210,7 @@ impl LoadState for OpEthApi where N: RpcNodeCore, Rpc: RpcConvert, + Self: LoadPendingBlock, { } @@ -217,6 +218,7 @@ impl EthState for OpEthApi where N: RpcNodeCore, Rpc: RpcConvert, + Self: LoadPendingBlock, { #[inline] fn max_proof_window(&self) -> u64 { diff --git a/crates/rpc/rpc-eth-api/src/helpers/state.rs b/crates/rpc/rpc-eth-api/src/helpers/state.rs index 9dcb7a0bb23..eab08450c81 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/state.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/state.rs @@ -11,12 +11,14 @@ use alloy_serde::JsonStorageKey; use futures::Future; use reth_errors::RethError; use reth_evm::{ConfigureEvm, EvmEnvFor}; -use reth_rpc_eth_types::{EthApiError, PendingBlockEnv, RpcInvalidTransactionError}; +use reth_rpc_convert::RpcConvert; +use reth_rpc_eth_types::{ + error::FromEvmError, EthApiError, PendingBlockEnv, RpcInvalidTransactionError, +}; use reth_storage_api::{ BlockIdReader, BlockNumReader, StateProvider, StateProviderBox, StateProviderFactory, }; use reth_transaction_pool::TransactionPool; -use std::future; /// Helper methods for `eth_` methods relating to state (accounts). pub trait EthState: LoadState + SpawnBlocking { @@ -195,7 +197,13 @@ 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 + RpcNodeCoreExt { +pub trait LoadState: + LoadPendingBlock + + EthApiTypes< + Error: FromEvmError + FromEthApiError, + RpcConvert: RpcConvert, + > + RpcNodeCoreExt +{ /// Returns the state at the given block number fn state_at_hash(&self, block_hash: B256) -> Result { self.provider().history_by_block_hash(block_hash).map_err(Self::Error::from_eth_err) @@ -208,8 +216,19 @@ pub trait LoadState: EthApiTypes + RpcNodeCoreExt { fn state_at_block_id( &self, at: BlockId, - ) -> impl Future> + Send { - future::ready(self.provider().state_by_block_id(at).map_err(Self::Error::from_eth_err)) + ) -> impl Future> + Send + where + Self: SpawnBlocking, + { + async move { + if at.is_pending() { + if let Ok(Some(state)) = self.local_pending_state().await { + return Ok(state) + } + } + + self.provider().state_by_block_id(at).map_err(Self::Error::from_eth_err) + } } /// Returns the _latest_ state @@ -223,7 +242,10 @@ pub trait LoadState: EthApiTypes + RpcNodeCoreExt { fn state_at_block_id_or_latest( &self, block_id: Option, - ) -> impl Future> + Send { + ) -> impl Future> + Send + where + Self: SpawnBlocking, + { async move { if let Some(block_id) = block_id { self.state_at_block_id(block_id).await @@ -244,7 +266,7 @@ pub trait LoadState: EthApiTypes + RpcNodeCoreExt { at: BlockId, ) -> impl Future, BlockId), Self::Error>> + Send where - Self: LoadPendingBlock + SpawnBlocking, + Self: SpawnBlocking, { async move { if at.is_pending() { diff --git a/crates/rpc/rpc/src/eth/helpers/state.rs b/crates/rpc/rpc/src/eth/helpers/state.rs index 5d767d2ede5..3d9cc763097 100644 --- a/crates/rpc/rpc/src/eth/helpers/state.rs +++ b/crates/rpc/rpc/src/eth/helpers/state.rs @@ -1,17 +1,17 @@ //! Contains RPC handler implementations specific to state. +use crate::EthApi; use reth_rpc_convert::RpcConvert; use reth_rpc_eth_api::{ - helpers::{EthState, LoadState}, + helpers::{EthState, LoadPendingBlock, LoadState}, RpcNodeCore, }; -use crate::EthApi; - impl EthState for EthApi where N: RpcNodeCore, Rpc: RpcConvert, + Self: LoadPendingBlock, { fn max_proof_window(&self) -> u64 { self.inner.eth_proof_window() @@ -22,6 +22,7 @@ impl LoadState for EthApi where N: RpcNodeCore, Rpc: RpcConvert, + Self: LoadPendingBlock, { }