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: resolve multiple function/event selectors in one openchain.xyz request #6719

Closed
wants to merge 2 commits into from

Conversation

cdump
Copy link
Contributor

@cdump cdump commented Jan 6, 2024

Motivation

Currently, the number of HTTP requests to openchain.xyz is equal to the number of selectors to resolve, which is not optimal.
On the current master, the following cast run takes ~11 seconds:

# run twice to ensure that the blockchain state is cached, and almost all time is spent on openchain.xyz requests
$ rm ~/.foundry/cache/signatures
$ cast run -q 0xa4fd571d66e89df8ed0ff4cf2eaea0ef56b75470715b89253cb833598e08ffec

I've added println!("get {url}"); to selectors.rs:58 and got:

get https://api.openchain.xyz/signature-database/v1/lookup?function=0x7527b51c
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x128acb08
get https://api.openchain.xyz/signature-database/v1/lookup?event=0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67
get https://api.openchain.xyz/signature-database/v1/lookup?function=0xa9059cbb
get https://api.openchain.xyz/signature-database/v1/lookup?event=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x70a08231
get https://api.openchain.xyz/signature-database/v1/lookup?function=0xfa461e33
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x022c0d9f
get https://api.openchain.xyz/signature-database/v1/lookup?event=0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1
get https://api.openchain.xyz/signature-database/v1/lookup?event=0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x66e27b0c
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x095ea7b3
get https://api.openchain.xyz/signature-database/v1/lookup?event=0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925
get https://api.openchain.xyz/signature-database/v1/lookup?function=0xe5d7bde6
get https://api.openchain.xyz/signature-database/v1/lookup?event=0xb9ed0243fdf00f0545c63a0af8850c090d86bb46682baec4bf3c496814fe4f02
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x2cc2878d
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x23b872dd
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x52bbbe29
get https://api.openchain.xyz/signature-database/v1/lookup?event=0x2170c741c41531aec20e7c107c24eecfdd15e69c9bb0a8dd37b1840b9e0b207b
get https://api.openchain.xyz/signature-database/v1/lookup?function=0x9d2c110c

Solution

openchain.xyz's api supports multiple selectors in one request - let's use that feature.

After the proposed changes, cast run ... took 2.8 seconds - x4 faster than before

get https://api.openchain.xyz/signature-database/v1/lookup?function=0x9d2c110c,0xa9059cbb,0x2cc2878d,0xfa461e33,0x128acb08,0x52bbbe29,0x66e27b0c,0x7527b51c,0x70a08231,0x022c0d9f,0x095ea7b3,0xe5d7bde6,0x23b872dd
get https://api.openchain.xyz/signature-database/v1/lookup?event=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef,0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67,0x2170c741c415
31aec20e7c107c24eecfdd15e69c9bb0a8dd37b1840b9e0b207b,0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925,0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822,0xb9ed0243fdf00f0545c63a0a
f8850c090d86bb46682baec4bf3c496814fe4f02,0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1

}

/// Identifies `Function` from its cache or `https://api.openchain.xyz`
pub async fn identify_function(&mut self, identifier: &[u8]) -> Option<Function> {
self.ensure_not_offline()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to use results from loaded disk cache, network requests will be ignored in identify() function

}

#[derive(Deserialize)]
struct ApiResult {
event: HashMap<String, Vec<Decoded>>,
function: HashMap<String, Vec<Decoded>>,
event: HashMap<String, Option<Vec<Decoded>>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

api returns null for unknown selectors, we don't want to fail whole request with probably success responses

crates/evm/traces/src/decoder/mod.rs Outdated Show resolved Hide resolved
crates/evm/traces/src/decoder/mod.rs Outdated Show resolved Hide resolved
#[derive(Deserialize)]
struct Decoded {
name: String,
filtered: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

filter=true by default (source), so results are already filtered on API side and this field is not in response.

@DaniPopes
Copy link
Member

cc @Evalir @onbjerg

@cdump
Copy link
Contributor Author

cdump commented Jan 11, 2024

@onbjerg, are there any more 'big changes' planned? I've noticed a merge conflict, and while I can resolve it, it won't be a quick 1-minute task due to the changes in d46bcb3, so it's better to do it only once

@Evalir
Copy link
Member

Evalir commented Jan 11, 2024

hey @cdump — def no more big changes planned for this section of code, sorry about that! feel free to go ahead and rebase

@cdump
Copy link
Contributor Author

cdump commented Jan 12, 2024

Rebased & ready for review.
The failed clippy check looks like a false positive. The .as_ref() in this code (not changed in this PR) is required.

Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

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

lgtm, sorry for the wait

Copy link
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

lgtm, needs clippy

@cdump
Copy link
Contributor Author

cdump commented Jan 19, 2024

@Evalir, what do you mean by 'needs clippy'? It seems that other MR no longer fail on clippy nightly. Unfortunately, I don't have the rights to restart the clippy check in this MR, should I force-push just to restart pipeline?

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.

5 participants