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: add TryFrom for GethTrace for all inner variants #933

Merged
merged 10 commits into from
Jun 19, 2024

Conversation

Ebolon
Copy link
Contributor

@Ebolon Ebolon commented Jun 18, 2024

Fixes #929

For me it feels more rust like to use {variant}::try_from(geth_trace) instead of geth_trace.try_into_{variant}(). How do you feel about that @mattsse?

Example:

use alloy_primitives::b256;
use alloy_provider::{ext::DebugApi, ProviderBuilder};
use alloy_rpc_types_trace::geth::{
    GethDebugBuiltInTracerType, GethDebugTracerConfig, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions,
    PreStateConfig, PreStateFrame, TraceResult,
};
use std::convert::{TryFrom, TryInto};

#[tokio::main]
async fn main() {
    let url = std::env::var("ETH_MAINNET_HTTP").expect("$ETH_MAINNET_HTTP must be set.");
    let node_url = url::Url::parse(url.as_str()).unwrap();
    let provider = ProviderBuilder::new().on_http(node_url);

    let tracer_opts = GethDebugTracingOptions {
        tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::PreStateTracer)),
        config: GethDefaultTracingOptions::default().disable_storage().disable_stack().disable_memory().disable_return_data(),
        timeout: None,
        tracer_config: GethDebugTracerConfig::default(),
    }
    .with_prestate_config(PreStateConfig { diff_mode: Some(true) });

    let call_result =
        provider.debug_trace_block_by_hash(b256!("1b9f245b55515d17fd5b3467bfe595670c9599b3d54837cd32cfde5e2e667830"), tracer_opts).await;

    let trace_results = match call_result {
        Ok(trace_results) => trace_results,
        Err(e) => {
            println!("Error: {:?}", e);
            return;
        }
    };

    for trace_result in trace_results {
        match trace_result {
            TraceResult::Success { result, tx_hash } => {
                let pre_state_frame = PreStateFrame::try_from(result);
                assert!(pre_state_frame.is_ok());
            }
            TraceResult::Error { .. } => {}
        }
    }
}

onbjerg

This comment was marked as resolved.

@mattsse

This comment was marked as resolved.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with the tryfrom impls, but I personally would prefer gethtrace functions instead, so you can do something like:

let trace = provider.trace(..).await?.try_into_default()?;

without providing type hints, but we can do both, tryfrom and the functions

@onbjerg

This comment was marked as resolved.

@Ebolon
Copy link
Contributor Author

Ebolon commented Jun 18, 2024

@mattsse please have a look again.

PS: If you wonder why the clippy fixes: I used the command from the CONTRIBUTING.md in differs from the CI job.

@Ebolon
Copy link
Contributor Author

Ebolon commented Jun 19, 2024

I gave the topic a fresh look and came to the conclusion that the TryFrom implementation is redundant at all. From api perspective you initial idea has a better flow and and is easier to use. I adapted the naming of the variants in the function names. My feeling is that it is much better to have the full type name in there. It is easier to read without IDE type hinting and does not maybe lead to a mixup with e.g. Default().

@mattsse feel free to have a look again

@mattsse mattsse merged commit cffab13 into alloy-rs:main Jun 19, 2024
22 checks passed
@Ebolon Ebolon deleted the add-try-from-geth-trace-to-inner branch June 19, 2024 11:56
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add more convenience functions for GethTrace
3 participants