From 1cb439207f498bf0620bde1b869c0834b47d0c2a Mon Sep 17 00:00:00 2001 From: Amir Pasha Motamed Date: Thu, 28 Nov 2024 22:15:05 +0000 Subject: [PATCH] Replace the pending block with the latest block in all RPC functions --- crates/rpc/rpc-eth-api/src/core.rs | 95 ++++++++++++++++++++++++++++++ crates/rpc/rpc/src/debug.rs | 35 +++++++++++ crates/rpc/rpc/src/engine.rs | 20 +++++++ crates/rpc/rpc/src/otterscan.rs | 5 ++ crates/rpc/rpc/src/reth.rs | 7 +++ crates/rpc/rpc/src/trace.rs | 32 ++++++++++ 6 files changed, 194 insertions(+) diff --git a/crates/rpc/rpc-eth-api/src/core.rs b/crates/rpc/rpc-eth-api/src/core.rs index 20edf96d810d..fcad61a6f62a 100644 --- a/crates/rpc/rpc-eth-api/src/core.rs +++ b/crates/rpc/rpc-eth-api/src/core.rs @@ -425,6 +425,11 @@ where number: BlockNumberOrTag, full: bool, ) -> RpcResult>> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, ?full, "Serving eth_getBlockByNumber"); Ok(EthBlocks::rpc_block(self, number.into(), full).await?) } @@ -440,6 +445,11 @@ where &self, number: BlockNumberOrTag, ) -> RpcResult> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, "Serving eth_getBlockTransactionCountByNumber"); Ok(EthBlocks::block_transaction_count(self, number.into()).await?.map(U256::from)) } @@ -455,6 +465,11 @@ where &self, number: BlockNumberOrTag, ) -> RpcResult> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, "Serving eth_getUncleCountByBlockNumber"); Ok(EthBlocks::ommers(self, number.into())?.map(|ommers| U256::from(ommers.len()))) } @@ -464,6 +479,11 @@ where &self, block_id: BlockId, ) -> RpcResult>>> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; trace!(target: "rpc::eth", ?block_id, "Serving eth_getBlockReceipts"); Ok(EthBlocks::block_receipts(self, block_id).await?) } @@ -484,6 +504,11 @@ where number: BlockNumberOrTag, index: Index, ) -> RpcResult>> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getUncleByBlockNumberAndIndex"); Ok(EthBlocks::ommer_by_block_and_index(self, number.into(), index).await?) } @@ -533,6 +558,11 @@ where number: BlockNumberOrTag, index: Index, ) -> RpcResult> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getRawTransactionByBlockNumberAndIndex"); Ok(EthTransactions::raw_transaction_by_block_and_tx_index( self, @@ -548,6 +578,11 @@ where number: BlockNumberOrTag, index: Index, ) -> RpcResult>> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; trace!(target: "rpc::eth", ?number, ?index, "Serving eth_getTransactionByBlockNumberAndIndex"); Ok(EthTransactions::transaction_by_block_and_tx_index(self, number.into(), index.into()) .await?) @@ -575,6 +610,11 @@ where /// Handler for: `eth_getBalance` async fn balance(&self, address: Address, block_number: Option) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getBalance"); Ok(EthState::balance(self, address, block_number).await?) } @@ -586,6 +626,11 @@ where index: JsonStorageKey, block_number: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getStorageAt"); Ok(EthState::storage_at(self, address, index, block_number).await?) } @@ -596,18 +641,33 @@ where address: Address, block_number: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getTransactionCount"); Ok(EthState::transaction_count(self, address, block_number).await?) } /// Handler for: `eth_getCode` async fn get_code(&self, address: Address, block_number: Option) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?address, ?block_number, "Serving eth_getCode"); Ok(EthState::get_code(self, address, block_number).await?) } /// Handler for: `eth_getHeaderByNumber` async fn header_by_number(&self, block_number: BlockNumberOrTag) -> RpcResult> { + #[cfg(feature = "telos")] + let block_number: BlockNumberOrTag = match block_number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => block_number, + }; trace!(target: "rpc::eth", ?block_number, "Serving eth_getHeaderByNumber"); Ok(EthBlocks::rpc_block_header(self, block_number.into()).await?) } @@ -624,6 +684,11 @@ where payload: SimulatePayload, block_number: Option, ) -> RpcResult>>> { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?block_number, "Serving eth_simulateV1"); Ok(EthCall::simulate_v1(self, payload, block_number).await?) } @@ -636,6 +701,11 @@ where state_overrides: Option, block_overrides: Option>, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?request, ?block_number, ?state_overrides, ?block_overrides, "Serving eth_call"); Ok(EthCall::call( self, @@ -663,6 +733,11 @@ where request: TransactionRequest, block_number: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_createAccessList"); Ok(EthCall::create_access_list_at(self, request, block_number).await?) } @@ -674,6 +749,11 @@ where block_number: Option, state_override: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_estimateGas"); Ok(EthCall::estimate_gas_at( self, @@ -696,6 +776,11 @@ where address: Address, block: BlockId, ) -> RpcResult> { + #[cfg(feature = "telos")] + let block = match block { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block, + }; trace!(target: "rpc::eth", "Serving eth_getAccount"); Ok(EthState::get_account(self, address, block).await?) } @@ -727,6 +812,11 @@ where newest_block: BlockNumberOrTag, reward_percentiles: Option>, ) -> RpcResult { + #[cfg(feature = "telos")] + let newest_block = match newest_block { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => newest_block, + }; trace!(target: "rpc::eth", ?block_count, ?newest_block, ?reward_percentiles, "Serving eth_feeHistory"); Ok(EthFees::fee_history(self, block_count.to(), newest_block, reward_percentiles).await?) } @@ -797,6 +887,11 @@ where keys: Vec, block_number: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; trace!(target: "rpc::eth", ?address, ?keys, ?block_number, "Serving eth_getProof"); Ok(EthState::get_proof(self, address, keys, block_number)?.await?) } diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 5474d020a00f..ac5f70cf2b48 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -879,6 +879,11 @@ where { /// Handler for `debug_getRawHeader` async fn raw_header(&self, block_id: BlockId) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let header = match block_id { BlockId::Hash(hash) => self.inner.provider.header(&hash.into()).to_rpc_result()?, BlockId::Number(number_or_tag) => { @@ -902,6 +907,11 @@ where /// Handler for `debug_getRawBlock` async fn raw_block(&self, block_id: BlockId) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let block = self .inner .provider @@ -925,6 +935,11 @@ where /// Handler for `debug_getRawTransactions` /// Returns the bytes of the transaction for the given hash. async fn raw_transactions(&self, block_id: BlockId) -> RpcResult> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let block = self .inner .provider @@ -936,6 +951,11 @@ where /// Handler for `debug_getRawReceipts` async fn raw_receipts(&self, block_id: BlockId) -> RpcResult> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; Ok(self .inner .provider @@ -991,6 +1011,11 @@ where block: BlockNumberOrTag, opts: Option, ) -> RpcResult> { + #[cfg(feature = "telos")] + let block = match block { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => block, + }; let _permit = self.acquire_trace_permit().await; Self::debug_trace_block(self, block.into(), opts.unwrap_or_default()) .await @@ -1015,6 +1040,11 @@ where block: BlockNumberOrTag, include_preimages: bool, ) -> RpcResult { + #[cfg(feature = "telos")] + let block = match block { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => block, + }; let _permit = self.acquire_trace_permit().await; Self::debug_execution_witness(self, block, include_preimages).await.map_err(Into::into) } @@ -1026,6 +1056,11 @@ where block_id: Option, opts: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Self::debug_trace_call(self, request, block_id, opts.unwrap_or_default()) .await diff --git a/crates/rpc/rpc/src/engine.rs b/crates/rpc/rpc/src/engine.rs index 928e2050a5c2..c2eb13cd594e 100644 --- a/crates/rpc/rpc/src/engine.rs +++ b/crates/rpc/rpc/src/engine.rs @@ -73,6 +73,11 @@ where state_overrides: Option, block_overrides: Option>, ) -> Result { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; self.eth .call(request, block_id, state_overrides, block_overrides) .instrument(engine_span!()) @@ -81,6 +86,11 @@ where /// Handler for: `eth_getCode` async fn get_code(&self, address: Address, block_id: Option) -> Result { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; self.eth.get_code(address, block_id).instrument(engine_span!()).await } @@ -99,6 +109,11 @@ where number: BlockNumberOrTag, full: bool, ) -> Result>> { + #[cfg(feature = "telos")] + let number = match number { + BlockNumberOrTag::Pending => BlockNumberOrTag::Latest, + _ => number, + }; self.eth.block_by_number(number, full).instrument(engine_span!()).await } @@ -119,6 +134,11 @@ where keys: Vec, block_number: Option, ) -> Result { + #[cfg(feature = "telos")] + let block_number = match block_number { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_number, + }; self.eth.get_proof(address, keys, block_number).instrument(engine_span!()).await } } diff --git a/crates/rpc/rpc/src/otterscan.rs b/crates/rpc/rpc/src/otterscan.rs index 31db343a104f..d5d5fe4189b8 100644 --- a/crates/rpc/rpc/src/otterscan.rs +++ b/crates/rpc/rpc/src/otterscan.rs @@ -78,6 +78,11 @@ where /// Handler for `ots_hasCode` async fn has_code(&self, address: Address, block_id: Option) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; EthApiServer::get_code(&self.eth, address, block_id).await.map(|code| !code.is_empty()) } diff --git a/crates/rpc/rpc/src/reth.rs b/crates/rpc/rpc/src/reth.rs index 6d5897df1315..f98ddbe60836 100644 --- a/crates/rpc/rpc/src/reth.rs +++ b/crates/rpc/rpc/src/reth.rs @@ -5,6 +5,8 @@ use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_errors::RethResult; use reth_primitives::BlockId; +#[cfg(feature = "telos")] +use reth_primitives::BlockNumberOrTag; use reth_provider::{BlockReaderIdExt, ChangeSetReader, StateProviderFactory}; use reth_rpc_api::RethApiServer; use reth_rpc_eth_types::{EthApiError, EthResult}; @@ -95,6 +97,11 @@ where &self, block_id: BlockId, ) -> RpcResult> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; Ok(Self::balance_changes_in_block(self, block_id).await?) } } diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index 925ea0872862..1e9211e1929e 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -20,6 +20,8 @@ use reth_consensus_common::calc::{ }; use reth_evm::ConfigureEvmEnv; use reth_primitives::{BlockId, Header}; +#[cfg(feature = "telos")] +use reth_primitives::BlockNumberOrTag; use reth_provider::{BlockReader, ChainSpecProvider, EvmEnvProvider, StateProviderFactory}; use reth_revm::database::StateProviderDatabase; use reth_rpc_api::TraceApiServer; @@ -592,6 +594,11 @@ where state_overrides: Option, block_overrides: Option>, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; let request = TraceCallRequest { call, trace_types, block_id, state_overrides, block_overrides }; @@ -604,6 +611,11 @@ where calls: Vec<(TransactionRequest, HashSet)>, block_id: Option, ) -> RpcResult> { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Ok(Self::trace_call_many(self, calls, block_id).await.map_err(Into::into)?) } @@ -615,6 +627,11 @@ where trace_types: HashSet, block_id: Option, ) -> RpcResult { + #[cfg(feature = "telos")] + let block_id = match block_id { + Some(BlockId::Number(BlockNumberOrTag::Pending)) => Some(BlockId::Number(BlockNumberOrTag::Latest)), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Ok(Self::trace_raw_transaction(self, data, trace_types, block_id) .await @@ -627,6 +644,11 @@ where block_id: BlockId, trace_types: HashSet, ) -> RpcResult>> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Ok(Self::replay_block_transactions(self, block_id, trace_types) .await @@ -648,6 +670,11 @@ where &self, block_id: BlockId, ) -> RpcResult>> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Ok(Self::trace_block(self, block_id).await.map_err(Into::into)?) } @@ -695,6 +722,11 @@ where /// Handler for `trace_blockOpcodeGas` async fn trace_block_opcode_gas(&self, block_id: BlockId) -> RpcResult> { + #[cfg(feature = "telos")] + let block_id = match block_id { + BlockId::Number(BlockNumberOrTag::Pending) => BlockId::Number(BlockNumberOrTag::Latest), + _ => block_id, + }; let _permit = self.acquire_trace_permit().await; Ok(Self::trace_block_opcode_gas(self, block_id).await.map_err(Into::into)?) }