Skip to content

Commit

Permalink
feat(rpc-testing) : add raw transaction tracing to traceApiExt (#5221)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoTheBestToGetTheBest authored Oct 30, 2023
1 parent 3b7d093 commit ef80b4d
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion crates/rpc/rpc-testing-util/src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//! Helpers for testing trace calls.
use futures::{Stream, StreamExt};
use jsonrpsee::core::Error as RpcError;
use reth_primitives::{BlockId, TxHash};

use reth_primitives::{BlockId, Bytes, TxHash};
use reth_rpc_api::clients::TraceApiClient;
use reth_rpc_types::trace::parity::{LocalizedTransactionTrace, TraceResults, TraceType};
use std::{
collections::HashSet,
pin::Pin,
task::{Context, Poll},
};
/// A type alias that represents the result of a raw transaction trace stream.
type RawTransactionTraceResult<'a> =
Pin<Box<dyn Stream<Item = Result<(TraceResults, Bytes), (RpcError, Bytes)>> + 'a>>;
/// A result type for the `trace_block` method that also captures the requested block.
pub type TraceBlockResult = Result<(Vec<LocalizedTransactionTrace>, BlockId), (RpcError, BlockId)>;
/// Type alias representing the result of replaying a transaction.
Expand Down Expand Up @@ -47,6 +51,34 @@ pub trait TraceApiExt {
) -> ReplayTransactionStream<'_>
where
I: IntoIterator<Item = TxHash>;

/// Returns a new stream that traces the provided raw transaction data.
fn trace_raw_transaction_stream(
&self,
data: Bytes,
trace_types: HashSet<TraceType>,
block_id: Option<BlockId>,
) -> RawTransactionTraceStream<'_>;
}
/// A stream that traces the provided raw transaction data.
#[must_use = "streams do nothing unless polled"]
pub struct RawTransactionTraceStream<'a> {
stream: RawTransactionTraceResult<'a>,
}

impl<'a> Stream for RawTransactionTraceStream<'a> {
type Item = Result<(TraceResults, Bytes), (RpcError, Bytes)>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.stream.as_mut().poll_next(cx)
}
}

impl<'a> std::fmt::Debug for RawTransactionTraceStream<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RawTransactionTraceStream").finish()
}
}

/// A stream that replays the transactions for the requested hashes.
Expand Down Expand Up @@ -125,6 +157,20 @@ impl<T: TraceApiClient + Sync> TraceApiExt for T {
.buffered(10);
ReplayTransactionStream { stream: Box::pin(stream) }
}
fn trace_raw_transaction_stream(
&self,
data: Bytes,
trace_types: HashSet<TraceType>,
block_id: Option<BlockId>,
) -> RawTransactionTraceStream<'_> {
let stream = futures::stream::once(async move {
match self.trace_raw_transaction(data.clone(), trace_types, block_id).await {
Ok(result) => Ok((result, data)),
Err(err) => Err((err, data)),
}
});
RawTransactionTraceStream { stream: Box::pin(stream) }
}
}

/// A stream that yields the traces for the requested blocks.
Expand Down

0 comments on commit ef80b4d

Please sign in to comment.