diff --git a/crates/rpc/rpc-eth-api/src/helpers/block.rs b/crates/rpc/rpc-eth-api/src/helpers/block.rs index 1ae084fbe45f..d69ef02f1322 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/block.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/block.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use alloy_eips::BlockId; use alloy_primitives::Sealable; use alloy_rlp::Encodable; -use alloy_rpc_types_eth::{Block, BlockTransactions, Header, Index}; +use alloy_rpc_types_eth::{Block, BlockNumberOrTag, BlockTransactions, Header, Index}; use futures::Future; use reth_node_api::BlockBody; use reth_primitives::{SealedBlockFor, SealedBlockWithSenders}; @@ -91,21 +91,31 @@ pub trait EthBlocks: LoadBlock { .map(|block| block.body.transactions().len())) } - let block_hash = match self - .provider() - .block_hash_for_id(block_id) - .map_err(Self::Error::from_eth_err)? - { - Some(block_hash) => block_hash, - None => return Ok(None), - }; + match block_id { + BlockId::Number(BlockNumberOrTag::Latest) => Ok(self + .cache() + .latest_block_with_senders() + .await + .map_err(Self::Error::from_eth_err)? + .map(|b| b.body.transactions().len())), + _ => { + let block_hash = match self + .provider() + .block_hash_for_id(block_id) + .map_err(Self::Error::from_eth_err)? + { + Some(block_hash) => block_hash, + None => return Ok(None), + }; - Ok(self - .cache() - .get_sealed_block_with_senders(block_hash) - .await - .map_err(Self::Error::from_eth_err)? - .map(|b| b.body.transactions().len())) + Ok(self + .cache() + .get_sealed_block_with_senders(block_hash) + .await + .map_err(Self::Error::from_eth_err)? + .map(|b| b.body.transactions().len())) + } + } } } @@ -241,19 +251,29 @@ pub trait LoadBlock: LoadPendingBlock + SpawnBlocking + RpcNodeCoreExt { }; } - let block_hash = match self - .provider() - .block_hash_for_id(block_id) - .map_err(Self::Error::from_eth_err)? - { - Some(block_hash) => block_hash, - None => return Ok(None), - }; + let block = match block_id { + BlockId::Number(BlockNumberOrTag::Latest) => self + .cache() + .latest_block_with_senders() + .await + .map_err(Self::Error::from_eth_err)?, + _ => { + let block_hash = match self + .provider() + .block_hash_for_id(block_id) + .map_err(Self::Error::from_eth_err)? + { + Some(block_hash) => block_hash, + None => return Ok(None), + }; - self.cache() - .get_sealed_block_with_senders(block_hash) - .await - .map_err(Self::Error::from_eth_err) + self.cache() + .get_sealed_block_with_senders(block_hash) + .await + .map_err(Self::Error::from_eth_err)? + } + }; + Ok(block) } } } diff --git a/crates/rpc/rpc-eth-types/src/cache/mod.rs b/crates/rpc/rpc-eth-types/src/cache/mod.rs index 13689f427cdd..60ff2309da37 100644 --- a/crates/rpc/rpc-eth-types/src/cache/mod.rs +++ b/crates/rpc/rpc-eth-types/src/cache/mod.rs @@ -185,8 +185,8 @@ impl EthStateCache { /// Returns the most recent canonical block from the cache, if available. /// Used to efficiently handle latest block requests and avoid race conditions during chain - /// reorgs. - /// Returns None if no canonical chain is tracked or during reorgs. + /// reorgs. + /// Returns `None` if no canonical chain is tracked or during reorgs. pub async fn latest_block_with_senders( &self, ) -> ProviderResult>>> {