From 7da8323ac2e60b2316a7d50f3967cdd3742798b0 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 3 Apr 2023 15:58:06 +0200 Subject: [PATCH] chore(rpc): move trace_at function to trait (#2089) --- crates/rpc/rpc/src/eth/api/transactions.rs | 38 +++++++++++++++++++++- crates/rpc/rpc/src/trace.rs | 31 +++--------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/crates/rpc/rpc/src/eth/api/transactions.rs b/crates/rpc/rpc/src/eth/api/transactions.rs index adac545fb1..b50922eaa3 100644 --- a/crates/rpc/rpc/src/eth/api/transactions.rs +++ b/crates/rpc/rpc/src/eth/api/transactions.rs @@ -17,7 +17,10 @@ use reth_primitives::{ TxLegacy, H256, U128, U256, U64, }; use reth_provider::{BlockProvider, EvmEnvProvider, StateProviderBox, StateProviderFactory}; -use reth_revm::database::{State, SubState}; +use reth_revm::{ + database::{State, SubState}, + tracing::{TracingInspector, TracingInspectorConfig}, +}; use reth_rpc_types::{ state::StateOverride, CallRequest, Index, Log, Transaction, TransactionInfo, TransactionReceipt, TransactionRequest, TypedTransactionRequest, @@ -105,6 +108,19 @@ pub trait EthTransactions: Send + Sync { ) -> EthResult<(ResultAndState, Env)> where I: for<'r> Inspector>>> + Send; + + /// Executes the transaction at the given [BlockId] with a tracer configured by the config. + /// The callback is then called with the [TracingInspector] and the [ResultAndState] after the + /// configured [Env] was inspected. + fn trace_at( + &self, + env: Env, + config: TracingInspectorConfig, + at: BlockId, + f: F, + ) -> EthResult + where + F: FnOnce(TracingInspector, ResultAndState) -> EthResult; } #[async_trait] @@ -340,6 +356,26 @@ where { self.with_call_at(request, at, state_overrides, |db, env| inspect(db, env, inspector)).await } + + fn trace_at( + &self, + env: Env, + config: TracingInspectorConfig, + at: BlockId, + f: F, + ) -> EthResult + where + F: FnOnce(TracingInspector, ResultAndState) -> EthResult, + { + self.with_state_at(at, |state| { + let db = SubState::new(State::new(state)); + + let mut inspector = TracingInspector::new(config); + let (res, _) = inspect(db, env, &mut inspector)?; + + f(inspector, res) + }) + } } // === impl EthApi === diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index a0023f95bd..659c84cd61 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -1,7 +1,6 @@ use crate::{ eth::{ - cache::EthStateCache, error::EthResult, revm_utils::inspect, - utils::recover_raw_transaction, EthTransactions, + cache::EthStateCache, error::EthResult, utils::recover_raw_transaction, EthTransactions, }, result::internal_rpc_err, TracingCallGuard, @@ -11,7 +10,6 @@ use jsonrpsee::core::RpcResult as Result; use reth_primitives::{BlockId, BlockNumberOrTag, Bytes, H256}; use reth_provider::{BlockProvider, EvmEnvProvider, StateProviderFactory}; use reth_revm::{ - database::{State, SubState}, env::tx_env_with_recovered, tracing::{TracingInspector, TracingInspectorConfig}, }; @@ -20,7 +18,7 @@ use reth_rpc_types::{ trace::{filter::TraceFilter, parity::*}, CallRequest, Index, }; -use revm::primitives::{Env, ResultAndState}; +use revm::primitives::Env; use std::collections::HashSet; use tokio::sync::{AcquireError, OwnedSemaphorePermit}; @@ -68,27 +66,6 @@ where Client: BlockProvider + StateProviderFactory + EvmEnvProvider + 'static, Eth: EthTransactions + 'static, { - /// Executes the transaction at the given [BlockId] with a tracer configured by the config. - fn trace_at( - &self, - env: Env, - config: TracingInspectorConfig, - at: BlockId, - f: F, - ) -> EthResult - where - F: FnOnce(TracingInspector, ResultAndState) -> EthResult, - { - self.eth_api.with_state_at(at, |state| { - let db = SubState::new(State::new(state)); - - let mut inspector = TracingInspector::new(config); - let (res, _) = inspect(db, env, &mut inspector)?; - - f(inspector, res) - }) - } - /// Executes the given call and returns a number of possible traces for it. pub async fn trace_call( &self, @@ -127,7 +104,7 @@ where let config = tracing_config(&trace_types); - self.trace_at(env, config, at, |inspector, res| { + self.eth_api.trace_at(env, config, at, |inspector, res| { let trace_res = inspector.into_parity_builder().into_trace_results(res.result, &trace_types); Ok(trace_res) @@ -171,7 +148,7 @@ where let env = Env { cfg, block, tx }; // execute the trace - self.trace_at(env, TracingInspectorConfig::default_parity(), at, |inspector, _| { + self.eth_api.trace_at(env, TracingInspectorConfig::default_parity(), at, |inspector, _| { let traces = inspector.into_parity_builder().into_localized_transaction_traces(tx_info); Ok(Some(traces)) })