Skip to content

Commit

Permalink
Reorganize tracing objects (#1157)
Browse files Browse the repository at this point in the history
* Reorganize tracing objects

* reorg

* simplify logic

* fix clippy

* fix
  • Loading branch information
tcoratger authored Jun 6, 2024
1 parent d91c262 commit 5ac6dcd
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 128 deletions.
31 changes: 21 additions & 10 deletions src/eth_rpc/servers/debug_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugApiServer for DebugRpc<P>
opts: Option<GethDebugTracingOptions>,
) -> Result<Vec<TraceResult>> {
let provider = Arc::new(&self.eth_provider);
let tracer = TracerBuilder::new(provider).await?.with_block_id(BlockId::Number(block_number)).await?.build()?;
let tracer = TracerBuilder::new(provider)
.await?
.with_block_id(BlockId::Number(block_number))
.await?
.with_tracing_options(opts.unwrap_or_default().into())
.build()?;

Ok(tracer.debug_block(opts.unwrap_or_default())?)
Ok(tracer.debug_block()?)
}

/// Returns the Geth debug trace for the given block hash.
Expand All @@ -174,11 +179,14 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugApiServer for DebugRpc<P>
block_hash: B256,
opts: Option<GethDebugTracingOptions>,
) -> Result<Vec<TraceResult>> {
let provider = Arc::new(&self.eth_provider);
let tracer =
TracerBuilder::new(provider).await?.with_block_id(BlockId::Hash(block_hash.into())).await?.build()?;
let tracer = TracerBuilder::new(Arc::new(&self.eth_provider))
.await?
.with_block_id(BlockId::Hash(block_hash.into()))
.await?
.with_tracing_options(opts.unwrap_or_default().into())
.build()?;

Ok(tracer.debug_block(opts.unwrap_or_default())?)
Ok(tracer.debug_block()?)
}

/// Returns the Geth debug trace for the given transaction hash.
Expand All @@ -188,10 +196,13 @@ impl<P: EthereumProvider + Send + Sync + 'static> DebugApiServer for DebugRpc<P>
transaction_hash: B256,
opts: Option<GethDebugTracingOptions>,
) -> Result<GethTrace> {
let provider = Arc::new(&self.eth_provider);
let tracer = TracerBuilder::new(provider).await?.with_transaction_hash(transaction_hash).await?.build()?;
let tracer = TracerBuilder::new(Arc::new(&self.eth_provider))
.await?
.with_transaction_hash(transaction_hash)
.await?
.with_tracing_options(opts.unwrap_or_default().into())
.build()?;

let trace = tracer.debug_transaction(transaction_hash, opts.unwrap_or_default())?;
Ok(trace)
Ok(tracer.debug_transaction(transaction_hash)?)
}
}
10 changes: 7 additions & 3 deletions src/eth_rpc/servers/trace_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ impl<P: EthereumProvider + Send + Sync + 'static> TraceApiServer for TraceRpc<P>
#[allow(clippy::blocks_in_conditions)]
#[tracing::instrument(skip(self), err, fields(block_id = ?block_id))]
async fn trace_block(&self, block_id: BlockId) -> Result<Option<Vec<LocalizedTransactionTrace>>> {
let provider = Arc::new(&self.eth_provider);
let tracer = TracerBuilder::new(provider).await?.with_block_id(block_id).await?.build()?;
let tracer = TracerBuilder::new(Arc::new(&self.eth_provider))
.await?
.with_block_id(block_id)
.await?
.with_tracing_options(TracingInspectorConfig::default_parity().into())
.build()?;

Ok(tracer.trace_block(TracingInspectorConfig::default_parity())?)
Ok(tracer.trace_block()?)
}
}
59 changes: 52 additions & 7 deletions src/tracing/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use reth_primitives::{B256, U256};
use reth_revm::primitives::{BlockEnv, CfgEnv, Env, EnvWithHandlerCfg, HandlerCfg, SpecId};
use reth_rpc_types::{Block, BlockHashOrNumber, BlockId, BlockTransactions, Header};

use crate::eth_provider::{
error::{EthApiError, TransactionError},
provider::EthereumProvider,
};
use reth_primitives::{B256, U256};
use reth_revm::primitives::{BlockEnv, CfgEnv, Env, EnvWithHandlerCfg, HandlerCfg, SpecId};
use reth_rpc_types::trace::geth::GethDebugTracingOptions;
use reth_rpc_types::{Block, BlockHashOrNumber, BlockId, BlockTransactions, Header};
use revm_inspectors::tracing::TracingInspectorConfig;

use super::{database::EthDatabaseSnapshot, Tracer, TracerResult};

Expand All @@ -14,11 +15,39 @@ pub struct Floating;
#[derive(Debug)]
pub struct Pinned;

/// Representing different tracing options for transactions.
#[derive(Clone, Debug)]
pub enum TracingOptions {
/// Geth debug tracing options.
Geth(GethDebugTracingOptions),
/// Parity tracing options.
Parity(TracingInspectorConfig),
}

impl Default for TracingOptions {
fn default() -> Self {
GethDebugTracingOptions::default().into()
}
}

impl From<GethDebugTracingOptions> for TracingOptions {
fn from(options: GethDebugTracingOptions) -> Self {
Self::Geth(options)
}
}

impl From<TracingInspectorConfig> for TracingOptions {
fn from(config: TracingInspectorConfig) -> Self {
Self::Parity(config)
}
}

#[derive(Debug)]
pub struct TracerBuilder<P: EthereumProvider + Send + Sync, Status = Floating> {
eth_provider: P,
env: Env,
block: Block,
tracing_options: TracingOptions,
_phantom: std::marker::PhantomData<Status>,
}

Expand All @@ -34,7 +63,13 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Floating> {

let env = Env { cfg, ..Default::default() };

Ok(Self { eth_provider, env, block: Default::default(), _phantom: std::marker::PhantomData })
Ok(Self {
eth_provider,
env,
block: Default::default(),
tracing_options: Default::default(),
_phantom: std::marker::PhantomData,
})
}

/// Sets the block to trace
Expand All @@ -45,6 +80,7 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Floating> {
eth_provider: self.eth_provider.clone(),
env: self.env.clone(),
block,
tracing_options: self.tracing_options.clone(),
_phantom: std::marker::PhantomData,
})
}
Expand Down Expand Up @@ -92,6 +128,13 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Floating> {
}

impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Pinned> {
/// Sets the tracing options
#[must_use]
pub fn with_tracing_options(mut self, tracing_options: TracingOptions) -> Self {
self.tracing_options = tracing_options;
self
}

/// Builds the tracer.
pub fn build(self) -> TracerResult<Tracer<P>> {
let transactions = match &self.block.transactions {
Expand All @@ -101,9 +144,11 @@ impl<P: EthereumProvider + Send + Sync + Clone> TracerBuilder<P, Pinned> {

let env = self.init_env_with_handler_config();
// DB should use the state of the parent block
let db = EthDatabaseSnapshot::new(self.eth_provider, BlockId::Hash(self.block.header.parent_hash.into()));
let db = EthDatabaseSnapshot::new(self.eth_provider, self.block.header.parent_hash.into());

let tracing_options = self.tracing_options;

Ok(Tracer { transactions, env, db })
Ok(Tracer { transactions, env, db, tracing_options })
}

/// Init an `EnvWithHandlerCfg`.
Expand Down
Loading

0 comments on commit 5ac6dcd

Please sign in to comment.