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 trace_get #987

Merged
merged 5 commits into from
Jun 29, 2024
Merged

feat: Add trace_get #987

merged 5 commits into from
Jun 29, 2024

Conversation

SozinM
Copy link
Contributor

@SozinM SozinM commented Jun 28, 2024

Motivation

Adds the missing trace_get method in #926

Problems

Currently trace_get returns null if queries with multiple indices (params example: "params":["0x346b152d421c095a2eab383d9c5d0309bb28c31e8c5b84fdc6a4f38e2194558d", ["0x0", "0x1"]]) paradigmxyz/reth#3852

Because of this, i choose to use param index with type u64, to remove footgun.

Solution

Usage example. Replace RETH_ENDPOINT with endpoint

use alloy_primitives::{b256};
use alloy_provider::{ext::TraceApi, ProviderBuilder};

#[tokio::main]
async fn main() {
    let url = String::from("RETH_ENDPOINT");
    let node_url = url::Url::parse(url.as_str()).unwrap();
    let provider = ProviderBuilder::new().on_http(node_url);


    let result = provider
        .trace_get(
            b256!("346b152d421c095a2eab383d9c5d0309bb28c31e8c5b84fdc6a4f38e2194558d"),
            1,
        )
        .await
        .unwrap();
    println!("{:#?}", result);
}

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@SozinM SozinM marked this pull request as ready for review June 28, 2024 20:52
@SozinM
Copy link
Contributor Author

SozinM commented Jun 28, 2024

Open to discussion about whether to use indices: Vec<u64> or index: u64 param.
The first variant is compatible with documentation (which is misleading).
The second variant helps with correct understanding but is confusing as contradict docs.

index: u64,
) -> TransportResult<LocalizedTransactionTrace> {
// We are using vec![indices] because API accepts a list, but in fact works only if list.len == 1
self.client().request("trace_get", (hash, vec![index])).await
Copy link
Member

Choose a reason for hiding this comment

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

you can use tuples for lists in JSON since they're equivalent to avoid allocating on the heap, see above in trace_transaction

index: u64,
) -> TransportResult<LocalizedTransactionTrace> {
// We are using vec![indices] because API accepts a list, but in fact works only if list.len == 1
self.client().request("trace_get", (hash, vec![index])).await
Copy link
Member

@mattsse mattsse Jun 28, 2024

Choose a reason for hiding this comment

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

this should also use the Index type of rpc-types-eth, because this should ideally be formatted as hex

https://docs.alchemy.com/reference/trace-get

async fn trace_get(
&self,
hash: TxHash,
index: u64,
Copy link
Member

Choose a reason for hiding this comment

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

since this is an index, I wonder if we should use usize here because I assume most of the time this value is derived from doing some vec.len() +- ops

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, put usize in there.
I think it would be less convenient to put Index as type because then all calls to trace_get would require a .into() call from the user. It probably would be less convenient.

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.

lgtm,
doc nit

crates/provider/src/ext/trace.rs Outdated Show resolved Hide resolved
SozinM and others added 2 commits June 29, 2024 14:20
docs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
@PanGan21 PanGan21 mentioned this pull request Jun 29, 2024
3 tasks
@SozinM
Copy link
Contributor Author

SozinM commented Jun 29, 2024

@mattsse could you approve workflows, please?

@mattsse mattsse merged commit 7b47529 into alloy-rs:main Jun 29, 2024
22 checks passed
@SozinM SozinM deleted the feature/trace-get branch June 29, 2024 12:47
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
* trace_get implementation

* Review

* Update crates/provider/src/ext/trace.rs

docs

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>

* Cargo fmt

---------

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
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.

3 participants