From 344537a3a0349298545812a9340ac4b1e7e477cb Mon Sep 17 00:00:00 2001 From: kustrun <9192608+kustrun@users.noreply.github.com> Date: Mon, 17 Feb 2025 11:13:03 +0100 Subject: [PATCH] feat(rpc): Implement debug_codeByHash. (#14524) --- crates/rpc/rpc-api/src/debug.rs | 5 +++++ crates/rpc/rpc/src/debug.rs | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc-api/src/debug.rs b/crates/rpc/rpc-api/src/debug.rs index 4b9dc8246..427bcfc75 100644 --- a/crates/rpc/rpc-api/src/debug.rs +++ b/crates/rpc/rpc-api/src/debug.rs @@ -195,6 +195,11 @@ pub trait DebugApi { #[method(name = "chaindbProperty")] async fn debug_chaindb_property(&self, property: String) -> RpcResult<()>; + /// Returns the code associated with a given hash at the specified block ID. + /// If no block ID is provided, it defaults to the latest block. + #[method(name = "codeByHash")] + async fn debug_code_by_hash(&self, hash: B256, block_id: Option) -> RpcResult; + /// Turns on CPU profiling for the given duration and writes profile data to disk. #[method(name = "cpuProfile")] async fn debug_cpu_profile(&self, file: String, seconds: u64) -> RpcResult<()>; diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index b52dca756..b92166efd 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -25,7 +25,8 @@ use reth_primitives::{NodePrimitives, ReceiptWithBloom, RecoveredBlock}; use reth_primitives_traits::{Block as _, BlockBody, SignedTransaction}; use reth_provider::{ BlockIdReader, BlockReaderIdExt, ChainSpecProvider, HeaderProvider, ProviderBlock, - ReceiptProviderIdExt, StateProofProvider, TransactionVariant, + ReceiptProviderIdExt, StateProofProvider, StateProvider, StateProviderFactory, + TransactionVariant, }; use reth_revm::{database::StateProviderDatabase, witness::ExecutionWitnessRecord}; use reth_rpc_api::DebugApiServer; @@ -660,6 +661,23 @@ where .await } + /// Returns the code associated with a given hash at the specified block ID. + /// If no block ID is provided, it defaults to the latest block. + pub async fn debug_code_by_hash( + &self, + hash: B256, + block_id: Option, + ) -> Result { + Ok(self + .provider() + .state_by_block_id(block_id.unwrap_or_default()) + .map_err(Eth::Error::from_eth_err)? + .bytecode_by_hash(&hash) + .map_err(Eth::Error::from_eth_err)? + .unwrap_or_default() + .original_bytes()) + } + /// Executes the configured transaction with the environment on the given database. /// /// It optionally takes fused inspector ([`TracingInspector::fused`]) to avoid re-creating the @@ -1044,6 +1062,10 @@ where Ok(()) } + async fn debug_code_by_hash(&self, hash: B256, block_id: Option) -> RpcResult { + Self::debug_code_by_hash(self, hash, block_id).await.map_err(Into::into) + } + async fn debug_cpu_profile(&self, _file: String, _seconds: u64) -> RpcResult<()> { Ok(()) }