From 91182f653593f2a81eddd424802c9728f4bd2a8e Mon Sep 17 00:00:00 2001 From: John Chase <68833933+joohhnnn@users.noreply.github.com> Date: Tue, 3 Mar 2026 12:17:55 +0800 Subject: [PATCH] feat(rpc): implement debug_traceBadBlock (#22719) --- crates/rpc/rpc-api/src/debug.rs | 2 +- crates/rpc/rpc/src/debug.rs | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/rpc/rpc-api/src/debug.rs b/crates/rpc/rpc-api/src/debug.rs index 0a9c0c266f..2ac12e7446 100644 --- a/crates/rpc/rpc-api/src/debug.rs +++ b/crates/rpc/rpc-api/src/debug.rs @@ -404,7 +404,7 @@ pub trait DebugApi { &self, block_hash: B256, opts: Option, - ) -> RpcResult<()>; + ) -> RpcResult>; /// Sets the logging verbosity ceiling. Log messages with level up to and including the given /// level will be printed. diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index f25f82f69b..bb3a6b4e4b 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -1070,10 +1070,25 @@ where async fn debug_trace_bad_block( &self, - _block_hash: B256, - _opts: Option, - ) -> RpcResult<()> { - Ok(()) + block_hash: B256, + opts: Option, + ) -> RpcResult> { + let _permit = self.acquire_trace_permit().await; + let block = self + .inner + .bad_block_store + .get(block_hash) + .ok_or_else(|| internal_rpc_err("bad block not found in cache"))?; + + let evm_env = self + .eth_api() + .evm_config() + .evm_env(block.header()) + .map_err(RethError::other) + .to_rpc_result()?; + + let opts = opts.map(|o| o.tracing_options).unwrap_or_default(); + self.trace_block(block, evm_env, opts).await.map_err(Into::into) } async fn debug_verbosity(&self, _level: usize) -> RpcResult<()> { @@ -1153,6 +1168,12 @@ impl BadBlockStore { let guard = self.inner.read(); guard.iter().rev().cloned().collect() } + + /// Returns the bad block with the given hash, if cached. + fn get(&self, hash: B256) -> Option>> { + let guard = self.inner.read(); + guard.iter().find(|b| b.hash() == hash).cloned() + } } impl Default for BadBlockStore {