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(forge, cast): add cast --with_local_artifacts/forge selectors cache to trace with local artifacts #7359

Merged
merged 28 commits into from
Nov 22, 2024

Conversation

byteshijinn
Copy link
Contributor

@byteshijinn byteshijinn commented Mar 10, 2024

Motivation

Closes #8949
Closes #8581
Supply a fast way to add all project contracts functions and events to signatures makes them decode correctly.

Solution

This PR introduces such a possibility using a cli argument --generate-local-signatures for cast run.

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.

supportive, though we can solve this a bit differently

lmk if anything's unclear

crates/cast/bin/cmd/run.rs Outdated Show resolved Hide resolved
crates/cast/bin/cmd/run.rs Outdated Show resolved Hide resolved
crates/cast/bin/cmd/run.rs Outdated Show resolved Hide resolved
@byteshijinn byteshijinn force-pushed the master branch 2 times, most recently from 2a08d1c to 4f0ee53 Compare March 17, 2024 14:18
@byteshijinn
Copy link
Contributor Author

I updated my code. please check again @mattsse

@byteshijinn byteshijinn requested a review from mattsse March 23, 2024 03:35
@byteshijinn byteshijinn changed the title add RunArgs generate_local_signatures to enable trace with local contracts functions and events feat(forge, cast): add RunArgs generate_local_signatures to enable trace with local contracts functions and events Apr 20, 2024
@byteshijinn byteshijinn requested a review from Evalir as a code owner June 30, 2024 12:00
mattsse
mattsse previously requested changes Jul 1, 2024
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.

sorry this one slipped through,
some nits.

generally supportive, wdyt @DaniPopes @klkvr

crates/cli/src/utils/cmd.rs Outdated Show resolved Hide resolved
crates/cast/bin/cmd/run.rs Outdated Show resolved Hide resolved
crates/cast/bin/cmd/run.rs Outdated Show resolved Hide resolved
crates/cli/src/utils/cmd.rs Outdated Show resolved Hide resolved
@mattsse
Copy link
Member

mattsse commented Jul 9, 2024

how to proceed here @klkvr ?

| NamedChain::ArbitrumTestnet
| NamedChain::ArbitrumGoerli
| NamedChain::ArbitrumSepolia
| NamedChain::Moonbeam
Copy link
Member

@klkvr klkvr Jul 9, 2024

Choose a reason for hiding this comment

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

please remove all the formatting changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry for my mistakes, I will fix it

Comment on lines 428 to 435
let cache_contents = std::fs::read_to_string(path.clone()).unwrap();
match serde_json::from_str::<CachedSignatures>(&cache_contents) {
Ok(existed_signatures) => cached_signatures = existed_signatures,
Err(e) => {
println!("parse cached local signatures file error: {}", e);
}
}
dbg!(&cached_signatures);
Copy link
Member

Choose a reason for hiding this comment

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

this is now duplicated with SignatureIdentifier::new, can we move this logic to CachedSignatures::load(path)

@klkvr
Copy link
Member

klkvr commented Jul 9, 2024

So this can be broken into 2 separate usecases

  1. Have a way to flush selectors from local project contracts into global signature cache (forge build --cache_local_signatures). Current impl lgtm, however, wondering if it makes more sense to be forge selectors cache instead?

  2. Use local ABIs when decoding call trace from cast run. For this usecase I'd prefer a different implementation which would obtain Project, compile it, construct known_contracts and include them in both CallTraceDecoder and TraceIdentifiers similarly to how it's done in forge test:

    let mut identifier = TraceIdentifiers::new().with_local(&known_contracts);

    The motivation is that if we have an entire Project, then we can not only use artifacts for identification of selectors, but also to resolve return values of functions, and identify contracts by their bytecodes. ref tracing: Improve decoding of functions output #6531 which had similar motivation

@byteshijinn byteshijinn force-pushed the master branch 2 times, most recently from 825579e to c575b2d Compare July 10, 2024 16:59
@byteshijinn
Copy link
Contributor Author

So this can be broken into 2 separate usecases

  1. Have a way to flush selectors from local project contracts into global signature cache (forge build --cache_local_signatures). Current impl lgtm, however, wondering if it makes more sense to be forge selectors cache instead?

  2. Use local ABIs when decoding call trace from cast run. For this usecase I'd prefer a different implementation which would obtain Project, compile it, construct known_contracts and include them in both CallTraceDecoder and TraceIdentifiers similarly to how it's done in forge test:

    let mut identifier = TraceIdentifiers::new().with_local(&known_contracts);

    The motivation is that if we have an entire Project, then we can not only use artifacts for identification of selectors, but also to resolve return values of functions, and identify contracts by their bytecodes. ref tracing: Improve decoding of functions output #6531 which had similar motivation

For case 1, I believe having helper options in forge build and cast run would make this feature more noticeable. I think adding a command to clear the cache would be useful because SignatureIdentifier merges matched signatures into the cache. My previous tests showed that if a local signature is matched, even if the parameters do not match, it still displays the matched function name.

For case 2, I have considered the solution you mentioned. However, the changes involved are quite extensive and a bit difficult for me, which is why I chose the current method. It allows me to get it working quickly.

@grandizzy
Copy link
Collaborator

grandizzy commented Nov 21, 2024

thanks @byteshijinn ! I made following changes and updated the PR:

  • merged with latest master
  • flush selectors from local project contracts into global signature cache through forge selectors cache instead forge build
  • use local ABIs when decoding call trace from cast run and cast send (--with_local_artifacts or --la)
  • cast test

tested locally as:

  • start anvil
  • deploy contract
  • send tx (custom errors, events)
  • run cast run tx - no traces shown. run cast run tx --la showing decoded traces (contracts identified)
  • run forge selectors cache, then run cast run tx --la showing decoded traces (contracts not identified though)

@klkvr mind to review? thank you!

@grandizzy grandizzy changed the title feat(forge, cast): add RunArgs generate_local_signatures to enable trace with local contracts functions and events feat(forge, cast): add cast --with_local_artifacts/forge selectors cache to trace with local artifacts Nov 21, 2024
@grandizzy grandizzy requested a review from mattsse November 22, 2024 07:58
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

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

nice, a couple nits

crates/cli/src/utils/cmd.rs Outdated Show resolved Hide resolved
crates/cli/src/utils/cmd.rs Outdated Show resolved Hide resolved
- compile without quiet, fix test
- merge local sources with etherscan
@grandizzy grandizzy requested a review from klkvr November 22, 2024 12:44
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

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

last nit

crates/evm/traces/src/debug/sources.rs Outdated Show resolved Hide resolved
@grandizzy grandizzy requested a review from klkvr November 22, 2024 15:52
@klkvr klkvr enabled auto-merge (squash) November 22, 2024 15:54
@grandizzy grandizzy dismissed mattsse’s stale review November 22, 2024 15:58

changes accomodated

@klkvr klkvr merged commit 398ef4a into foundry-rs:master Nov 22, 2024
21 checks passed
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 2024
… cache` to trace with local artifacts (foundry-rs#7359)

* add RunArgs generate_local_signatures to enable trace with local contracts functions and events

* make generate_local_signatures as a helper function

* rename generate_local_signatures to cache_local_signatures
merge project signatures with exists cached local signatures instead of
just override them

* extract duplicate method for CachedSignatures

* fix cache load path

* fix for lint

* fix fot lint

* remove unnecessary `let` binding

* fix for format check

* fix for clippy check

* fix for clippy check

* Move cache in forge selectors, use local artifacts for cast run and send traces

* Add test

* Review changes:
- compile without quiet, fix test
- merge local sources with etherscan

* Update crates/evm/traces/src/debug/sources.rs

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>

---------

Co-authored-by: grandizzy <grandizzy.the.egg@gmail.com>
Co-authored-by: grandizzy <38490174+grandizzy@users.noreply.github.com>
Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
@grandizzy grandizzy added the T-feature Type: feature label Dec 18, 2024
@byteshijinn byteshijinn deleted the local_signature branch January 11, 2025 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tracing Area: tracing C-cast Command: cast C-forge Command: forge Cmd-forge-build Command: forge build T-feature Type: feature
Projects
Archived in project
5 participants