From fe6fd742b43374919eecf8809888dcbbdfc8d613 Mon Sep 17 00:00:00 2001 From: RexCloud Date: Mon, 3 Jun 2024 20:27:53 +0500 Subject: [PATCH] chore: move trace to extension trait --- crates/provider/src/ext/mod.rs | 3 + crates/provider/src/ext/trace.rs | 117 ++++++++++++++++++++++++++ crates/provider/src/lib.rs | 3 +- crates/provider/src/provider/mod.rs | 2 +- crates/provider/src/provider/trait.rs | 62 -------------- 5 files changed, 122 insertions(+), 65 deletions(-) create mode 100644 crates/provider/src/ext/trace.rs diff --git a/crates/provider/src/ext/mod.rs b/crates/provider/src/ext/mod.rs index 924fb8e0dda..a9f24a63fc0 100644 --- a/crates/provider/src/ext/mod.rs +++ b/crates/provider/src/ext/mod.rs @@ -11,5 +11,8 @@ pub use engine::EngineApi; mod debug; pub use debug::DebugApi; +mod trace; +pub use trace::{TraceApi, TraceCallList}; + mod txpool; pub use txpool::TxPoolApi; diff --git a/crates/provider/src/ext/trace.rs b/crates/provider/src/ext/trace.rs new file mode 100644 index 00000000000..b7d8f7d6349 --- /dev/null +++ b/crates/provider/src/ext/trace.rs @@ -0,0 +1,117 @@ +//! This module extends the Ethereum JSON-RPC provider with the Trace namespace's RPC methods. +use crate::{Provider, RpcWithBlock}; +use alloy_eips::BlockNumberOrTag; +use alloy_network::Network; +use alloy_primitives::TxHash; +use alloy_rpc_types_trace::parity::{LocalizedTransactionTrace, TraceResults, TraceType}; +use alloy_transport::{Transport, TransportResult}; + +/// List of trace calls for use with [`TraceApi::trace_call_many`] +pub type TraceCallList<'a, N> = &'a [(::TransactionRequest, Vec)]; + +/// Trace namespace rpc interface that gives access to several non-standard RPC methods. +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] +pub trait TraceApi: Send + Sync +where + N: Network, + T: Transport + Clone, +{ + /// Executes the given transaction and returns a number of possible traces. + /// + /// # Note + /// + /// Not all nodes support this call. + fn trace_call<'a, 'b>( + &self, + request: &'a N::TransactionRequest, + trace_type: &'b [TraceType], + ) -> RpcWithBlock; + + /// Traces multiple transactions on top of the same block, i.e. transaction `n` will be executed + /// on top of the given block with all `n - 1` transaction applied first. + /// + /// Allows tracing dependent transactions. + /// + /// # Note + /// + /// Not all nodes support this call. + fn trace_call_many<'a>( + &self, + request: TraceCallList<'a, N>, + ) -> RpcWithBlock, TraceResults>; + + /// Parity trace transaction. + async fn trace_transaction( + &self, + hash: TxHash, + ) -> TransportResult>; + + /// Trace all transactions in the given block. + /// + /// # Note + /// + /// Not all nodes support this call. + async fn trace_block( + &self, + block: BlockNumberOrTag, + ) -> TransportResult>; +} + +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] +impl TraceApi for P +where + N: Network, + T: Transport + Clone, + P: Provider, +{ + fn trace_call<'a, 'b>( + &self, + request: &'a ::TransactionRequest, + trace_type: &'b [TraceType], + ) -> RpcWithBlock::TransactionRequest, &'b [TraceType]), TraceResults> + { + RpcWithBlock::new(self.weak_client(), "trace_call", (request, trace_type)) + } + + fn trace_call_many<'a>( + &self, + request: TraceCallList<'a, N>, + ) -> RpcWithBlock, TraceResults> { + RpcWithBlock::new(self.weak_client(), "trace_callMany", request) + } + + async fn trace_transaction( + &self, + hash: TxHash, + ) -> TransportResult> { + self.client().request("trace_transaction", (hash,)).await + } + + async fn trace_block( + &self, + block: BlockNumberOrTag, + ) -> TransportResult> { + self.client().request("trace_block", (block,)).await + } +} + +#[cfg(test)] +mod test { + use crate::ProviderBuilder; + + use super::*; + + fn init_tracing() { + let _ = tracing_subscriber::fmt::try_init(); + } + + #[tokio::test] + async fn test_trace_block() { + init_tracing(); + let provider = ProviderBuilder::new().on_anvil(); + let traces = provider.trace_block(BlockNumberOrTag::Latest).await.unwrap(); + assert_eq!(traces.len(), 0); + } +} diff --git a/crates/provider/src/lib.rs b/crates/provider/src/lib.rs index b484b4fe362..682999ca153 100644 --- a/crates/provider/src/lib.rs +++ b/crates/provider/src/lib.rs @@ -40,8 +40,7 @@ pub use heart::{PendingTransaction, PendingTransactionBuilder, PendingTransactio mod provider; pub use provider::{ - EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, TraceCallList, - WalletProvider, + EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, WalletProvider, }; pub mod utils; diff --git a/crates/provider/src/provider/mod.rs b/crates/provider/src/provider/mod.rs index 1d431f82912..9ff0dd67e9c 100644 --- a/crates/provider/src/provider/mod.rs +++ b/crates/provider/src/provider/mod.rs @@ -8,7 +8,7 @@ mod sendable; pub use sendable::SendableTx; mod r#trait; -pub use r#trait::{FilterPollerBuilder, Provider, TraceCallList}; +pub use r#trait::{FilterPollerBuilder, Provider}; mod wallet; pub use wallet::WalletProvider; diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index cb0678d9433..401f5262b66 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -17,7 +17,6 @@ use alloy_rpc_types::{ AccessListWithGasUsed, Block, BlockId, BlockNumberOrTag, EIP1186AccountProofResponse, FeeHistory, Filter, FilterChanges, Log, SyncStatus, }; -use alloy_rpc_types_trace::parity::{LocalizedTransactionTrace, TraceResults, TraceType}; use alloy_transport::{BoxTransport, Transport, TransportErrorKind, TransportResult}; use serde_json::value::RawValue; use std::borrow::Cow; @@ -27,9 +26,6 @@ use std::borrow::Cow; /// See [`PollerBuilder`] for more details. pub type FilterPollerBuilder = PollerBuilder>; -/// List of trace calls for use with [`Provider::trace_call_many`] -pub type TraceCallList<'a, N> = &'a [(::TransactionRequest, Vec)]; - // todo: adjust docs // todo: reorder /// Provider is parameterized with a network and a transport. The default @@ -789,56 +785,6 @@ pub trait Provider: RpcWithBlock::new(self.weak_client(), "eth_createAccessList", request) } - /// Executes the given transaction and returns a number of possible traces. - /// - /// # Note - /// - /// Not all nodes support this call. - fn trace_call<'a, 'b>( - &self, - request: &'a N::TransactionRequest, - trace_type: &'b [TraceType], - ) -> RpcWithBlock { - RpcWithBlock::new(self.weak_client(), "trace_call", (request, trace_type)) - } - - /// Traces multiple transactions on top of the same block, i.e. transaction `n` will be executed - /// on top of the given block with all `n - 1` transaction applied first. - /// - /// Allows tracing dependent transactions. - /// - /// # Note - /// - /// Not all nodes support this call. - fn trace_call_many<'a>( - &self, - request: TraceCallList<'a, N>, - ) -> RpcWithBlock, TraceResults> { - RpcWithBlock::new(self.weak_client(), "trace_callMany", request) - } - - // todo: move to extension trait - /// Parity trace transaction. - async fn trace_transaction( - &self, - hash: TxHash, - ) -> TransportResult> { - self.client().request("trace_transaction", (hash,)).await - } - - // todo: move to extension trait - /// Trace all transactions in the given block. - /// - /// # Note - /// - /// Not all nodes support this call. - async fn trace_block( - &self, - block: BlockNumberOrTag, - ) -> TransportResult> { - self.client().request("trace_block", (block,)).await - } - /* ------------------------------------------ anvil ----------------------------------------- */ /// Set the bytecode of a given account. @@ -1307,14 +1253,6 @@ mod tests { assert!(receipts.is_some()); } - #[tokio::test] - async fn gets_block_traces() { - init_tracing(); - let provider = ProviderBuilder::new().on_anvil(); - let traces = provider.trace_block(BlockNumberOrTag::Latest).await.unwrap(); - assert_eq!(traces.len(), 0); - } - #[tokio::test] async fn sends_raw_transaction() { init_tracing();