Skip to content

Commit 87a024e

Browse files
authored
feat(anvil): add debug_dbGet endpoint (#12375)
* feat(anvil): add `debug_dbGet` endpoint Introduces a new RPC handler `debug_db_get` that retrieves bytecode from the database using a specific key format. The key must be 33 bytes long, starting with the prefix 0x63, and the method validates the key before fetching the bytecode using the existing `debug_code_by_hash` function. * fix: typo * add `EthRequest::DebugDbGet` variant
1 parent 6062254 commit 87a024e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

crates/anvil/core/src/eth/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ pub enum EthRequest {
303303
#[serde(rename = "debug_codeByHash")]
304304
DebugCodeByHash(B256, #[serde(default)] Option<BlockId>),
305305

306+
/// reth's `debug_dbGet` endpoint
307+
#[serde(rename = "debug_dbGet")]
308+
DebugDbGet(String),
309+
306310
/// Trace transaction endpoint for parity's `trace_transaction`
307311
#[serde(rename = "trace_transaction", with = "sequence")]
308312
TraceTransaction(B256),

crates/anvil/src/eth/api.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ impl EthApi {
343343
EthRequest::DebugCodeByHash(hash, block) => {
344344
self.debug_code_by_hash(hash, block).await.to_rpc_result()
345345
}
346+
EthRequest::DebugDbGet(key) => self.debug_db_get(key).await.to_rpc_result(),
346347
EthRequest::TraceTransaction(tx) => self.trace_transaction(tx).await.to_rpc_result(),
347348
EthRequest::TraceBlock(block) => self.trace_block(block).await.to_rpc_result(),
348349
EthRequest::TraceFilter(filter) => self.trace_filter(filter).await.to_rpc_result(),
@@ -1845,6 +1846,15 @@ impl EthApi {
18451846
self.backend.debug_code_by_hash(hash, block_id).await
18461847
}
18471848

1849+
/// Returns the value associated with a key from the database
1850+
/// Only supports bytecode lookups.
1851+
///
1852+
/// Handler for RPC call: `debug_dbGet`
1853+
pub async fn debug_db_get(&self, key: String) -> Result<Option<Bytes>> {
1854+
node_info!("debug_dbGet");
1855+
self.backend.debug_db_get(key).await
1856+
}
1857+
18481858
/// Returns traces for the transaction hash via parity's tracing endpoint
18491859
///
18501860
/// Handler for RPC call: `trace_transaction`

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,42 @@ impl Backend {
28402840
Ok(None)
28412841
}
28422842

2843+
/// Returns the value associated with a key from the database
2844+
/// Currently only supports bytecode lookups.
2845+
///
2846+
/// Based on Reth implementation: <https://github.com/paradigmxyz/reth/blob/66cfa9ed1a8c4bc2424aacf6fb2c1e67a78ee9a2/crates/rpc/rpc/src/debug.rs#L1146-L1178>
2847+
///
2848+
/// Key should be: 0x63 (1-byte prefix) + 32 bytes (code_hash)
2849+
/// Total key length must be 33 bytes.
2850+
pub async fn debug_db_get(&self, key: String) -> Result<Option<Bytes>, BlockchainError> {
2851+
let key_bytes = if key.starts_with("0x") {
2852+
hex::decode(&key)
2853+
.map_err(|_| BlockchainError::Message("Invalid hex key".to_string()))?
2854+
} else {
2855+
key.into_bytes()
2856+
};
2857+
2858+
// Validate key length: must be 33 bytes (1 byte prefix + 32 bytes code hash)
2859+
if key_bytes.len() != 33 {
2860+
return Err(BlockchainError::Message(format!(
2861+
"Invalid key length: expected 33 bytes, got {}",
2862+
key_bytes.len()
2863+
)));
2864+
}
2865+
2866+
// Check for bytecode prefix (0x63 = 'c' in ASCII)
2867+
if key_bytes[0] != 0x63 {
2868+
return Err(BlockchainError::Message(
2869+
"Key prefix must be 0x63 for code hash lookups".to_string(),
2870+
));
2871+
}
2872+
2873+
let code_hash = B256::from_slice(&key_bytes[1..33]);
2874+
2875+
// Use the existing debug_code_by_hash method to retrieve the bytecode
2876+
self.debug_code_by_hash(code_hash, None).await
2877+
}
2878+
28432879
fn geth_trace(
28442880
&self,
28452881
tx: &MinedTransaction,

0 commit comments

Comments
 (0)