mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add js debug test utils (#3153)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -5664,6 +5664,7 @@ dependencies = [
|
|||||||
"reth-primitives",
|
"reth-primitives",
|
||||||
"reth-rpc-api",
|
"reth-rpc-api",
|
||||||
"reth-rpc-types",
|
"reth-rpc-types",
|
||||||
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@ futures = { workspace = true }
|
|||||||
|
|
||||||
# misc
|
# misc
|
||||||
jsonrpsee = { version = "0.18", features = ["client", "async-client"] }
|
jsonrpsee = { version = "0.18", features = ["client", "async-client"] }
|
||||||
|
serde_json.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "rt"] }
|
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "rt"] }
|
||||||
|
|||||||
8
crates/rpc/rpc-testing-util/assets/noop-tracer.js
Normal file
8
crates/rpc/rpc-testing-util/assets/noop-tracer.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
// required function that is invoked when a step fails
|
||||||
|
fault: function(log, db) { },
|
||||||
|
// required function that returns the result of the tracer: empty object
|
||||||
|
result: function(ctx, db) { return {}; },
|
||||||
|
// optional function that is invoked for every opcode
|
||||||
|
step: function(log, db) { }
|
||||||
|
}
|
||||||
79
crates/rpc/rpc-testing-util/src/debug.rs
Normal file
79
crates/rpc/rpc-testing-util/src/debug.rs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
//! Helpers for testing debug trace calls.
|
||||||
|
|
||||||
|
use reth_primitives::H256;
|
||||||
|
use reth_rpc_api::clients::DebugApiClient;
|
||||||
|
use reth_rpc_types::trace::geth::{GethDebugTracerType, GethDebugTracingOptions};
|
||||||
|
|
||||||
|
const NOOP_TRACER: &str = include_str!("../assets/noop-tracer.js");
|
||||||
|
|
||||||
|
/// An extension trait for the Trace API.
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait DebugApiExt {
|
||||||
|
/// The provider type that is used to make the requests.
|
||||||
|
type Provider;
|
||||||
|
|
||||||
|
/// Same as [DebugApiClient::debug_trace_transaction] but returns the result as json.
|
||||||
|
async fn debug_trace_transaction_json(
|
||||||
|
&self,
|
||||||
|
hash: H256,
|
||||||
|
opts: GethDebugTracingOptions,
|
||||||
|
) -> Result<serde_json::Value, jsonrpsee::core::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl<T: DebugApiClient + Sync> DebugApiExt for T {
|
||||||
|
type Provider = T;
|
||||||
|
|
||||||
|
async fn debug_trace_transaction_json(
|
||||||
|
&self,
|
||||||
|
hash: H256,
|
||||||
|
opts: GethDebugTracingOptions,
|
||||||
|
) -> Result<serde_json::Value, jsonrpsee::core::Error> {
|
||||||
|
let mut params = jsonrpsee::core::params::ArrayParams::new();
|
||||||
|
params.insert(hash).unwrap();
|
||||||
|
params.insert(opts).unwrap();
|
||||||
|
self.request("debug_traceTransaction", params).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A javascript tracer that does nothing
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub struct NoopJsTracer;
|
||||||
|
|
||||||
|
impl From<NoopJsTracer> for GethDebugTracingOptions {
|
||||||
|
fn from(_: NoopJsTracer) -> Self {
|
||||||
|
GethDebugTracingOptions {
|
||||||
|
tracer: Some(GethDebugTracerType::JsTracer(NOOP_TRACER.to_string())),
|
||||||
|
tracer_config: serde_json::Value::Object(Default::default()).into(),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<NoopJsTracer> for Option<GethDebugTracingOptions> {
|
||||||
|
fn from(_: NoopJsTracer) -> Self {
|
||||||
|
Some(NoopJsTracer.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
debug::{DebugApiExt, NoopJsTracer},
|
||||||
|
utils::parse_env_url,
|
||||||
|
};
|
||||||
|
use jsonrpsee::http_client::HttpClientBuilder;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore]
|
||||||
|
async fn can_trace_noop_sepolia() {
|
||||||
|
// random tx <https://sepolia.etherscan.io/tx/0x5525c63a805df2b83c113ebcc8c7672a3b290673c4e81335b410cd9ebc64e085>
|
||||||
|
let tx =
|
||||||
|
"0x5525c63a805df2b83c113ebcc8c7672a3b290673c4e81335b410cd9ebc64e085".parse().unwrap();
|
||||||
|
let url = parse_env_url("RETH_RPC_TEST_NODE_URL").unwrap();
|
||||||
|
let client = HttpClientBuilder::default().build(url).unwrap();
|
||||||
|
let res =
|
||||||
|
client.debug_trace_transaction_json(tx, NoopJsTracer::default().into()).await.unwrap();
|
||||||
|
assert_eq!(res, serde_json::Value::Object(Default::default()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
//! Reth RPC testing utilities.
|
//! Reth RPC testing utilities.
|
||||||
|
|
||||||
|
pub mod debug;
|
||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|||||||
Reference in New Issue
Block a user