Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc-testing) : add raw transaction tracing to traceApiExt #5221

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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