Skip to content

Commit

Permalink
feat: add TryFrom for GethTrace for all inner variants (alloy-rs#933)
Browse files Browse the repository at this point in the history
* feat: add TryFrom for GethTrace for all inner variants

* Use workspace version for thiserror

* Fix clippy findings

* Simplify error

* Add functions for try_into_{tracer-variant} for GethTrace

* Fix clippy findings

* Update changelog

* Typo

* Fix clippy findings

* Remove redundant TryFrom implementation and rename try_into method to match the returning type
  • Loading branch information
Ebolon authored Jun 19, 2024
1 parent dfef950 commit cffab13
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/rpc-types-trace/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/alloy-rs/alloy/compare/v0.1.1...HEAD)

- Add `try_into_{variant}()` for all inner `GethTrace` variants ([#929](https://github.com/alloy-rs/alloy/issues/929))


## [0.1.1](https://github.com/alloy-rs/alloy/tree/v0.1.1)

### Features
Expand Down
1 change: 1 addition & 0 deletions crates/rpc-types-trace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ alloy-serde.workspace = true

serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true

[dev-dependencies]
alloy-primitives = { workspace = true, features = [
Expand Down
102 changes: 102 additions & 0 deletions crates/rpc-types-trace/src/geth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ pub mod mux;
pub mod noop;
pub mod pre_state;

/// Error when the inner tracer from [GethTrace] is mismatching to the target tracer.
#[derive(Debug, thiserror::Error)]
#[error("unexpected tracer")]
pub struct UnexpectedTracerError(pub GethTrace);

/// Result type for geth style transaction trace
pub type TraceResult = crate::common::TraceResult<GethTrace, String>;

Expand Down Expand Up @@ -123,6 +128,64 @@ pub enum GethTrace {
JS(serde_json::Value),
}

impl GethTrace {
/// Try to convert the inner tracer to [DefaultFrame]
pub fn try_into_default_frame(self) -> Result<DefaultFrame, UnexpectedTracerError> {
match self {
Self::Default(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [CallFrame]
pub fn try_into_call_frame(self) -> Result<CallFrame, UnexpectedTracerError> {
match self {
Self::CallTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [FourByteFrame]
pub fn try_into_four_byte_frame(self) -> Result<FourByteFrame, UnexpectedTracerError> {
match self {
Self::FourByteTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [PreStateFrame]
pub fn try_into_pre_state_frame(self) -> Result<PreStateFrame, UnexpectedTracerError> {
match self {
Self::PreStateTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [NoopFrame]
pub fn try_into_noop_frame(self) -> Result<NoopFrame, UnexpectedTracerError> {
match self {
Self::NoopTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [MuxFrame]
pub fn try_into_mux_frame(self) -> Result<MuxFrame, UnexpectedTracerError> {
match self {
Self::MuxTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [serde_json::Value]
pub fn try_into_json_value(self) -> Result<serde_json::Value, UnexpectedTracerError> {
match self {
Self::JS(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}
}

impl Default for GethTrace {
fn default() -> Self {
Self::Default(DefaultFrame::default())
Expand Down Expand Up @@ -659,4 +722,43 @@ mod tests {
let val = serde_json::from_str::<serde_json::Value>(s).unwrap();
similar_asserts::assert_eq!(val, de);
}

#[test]
fn test_geth_trace_into_tracer() {
let geth_trace = GethTrace::Default(DefaultFrame::default());
let inner = geth_trace.try_into_default_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::CallTracer(CallFrame::default());
let inner = geth_trace.try_into_call_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::FourByteTracer(FourByteFrame::default());
let inner = geth_trace.try_into_four_byte_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::PreStateTracer(PreStateFrame::Default(PreStateMode::default()));
let inner = geth_trace.try_into_pre_state_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::NoopTracer(NoopFrame::default());
let inner = geth_trace.try_into_noop_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::MuxTracer(MuxFrame::default());
let inner = geth_trace.try_into_mux_frame();
assert!(inner.is_ok());

let geth_trace = GethTrace::JS(serde_json::Value::Null);
let inner = geth_trace.try_into_json_value();
assert!(inner.is_ok());
}

#[test]
fn test_geth_trace_into_tracer_wrong_tracer() {
let geth_trace = GethTrace::Default(DefaultFrame::default());
let inner = geth_trace.try_into_call_frame();
assert!(inner.is_err());
assert!(matches!(inner, Err(UnexpectedTracerError(_))));
}
}

0 comments on commit cffab13

Please sign in to comment.