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(evm): compile etherscan sources and use them on the debugger #1413

Closed
wants to merge 2 commits into from

Conversation

joshieDo
Copy link
Collaborator

@joshieDo joshieDo commented Apr 25, 2022

Motivation

WIP

Debugging with sources coming from etherscan is nice.

Solution

  1. When identifying addresses for traces, collect addresses & sources.
  2. Compile each contract address [and downloads the solc version required)
  3. Provide them to the TUI debugger. Here I changed the TUI initialization. @brockelmore wdyt ?

Missing:

  • etherscan multi-file support
  • cache
  • cleanup

It's working now with some limitations:

  • flattened sources only
  • no vyper
  • no v0.4.*
  • --via-ir isn't provided by etherscan (i think...).

Working example:

cargo run --bin cast -- run 0xe6288cf5bb88281801302d558e434b8fced0dbaa8edc007ef2f5b0ffec694f88 --rpc-url https://api.avax.network/ext/bc/C/rpc --debug

If there are many contracts, be ready to wait a while...

@joshieDo joshieDo changed the title evm(debugger): compile etherscan sources and use them feat(evm): compile etherscan sources and use themon the debugger Apr 25, 2022
@joshieDo joshieDo changed the title feat(evm): compile etherscan sources and use themon the debugger feat(evm): compile etherscan sources and use them on the debugger Apr 25, 2022
@onbjerg
Copy link
Collaborator

onbjerg commented Apr 26, 2022

I think there was talk of source maps being added to the Etherscan API (it's already present in the UI) - is this something we can get confirmed somewhere, and if so, are we willing to wait for that considering that this is sort of a workaround for that?

@mattsse
Copy link
Member

mattsse commented Apr 26, 2022

it's not yet included in the getsourcecode api https://api.etherscan.io/api?module=contract&action=getsourcecode

@onbjerg
Copy link
Collaborator

onbjerg commented Apr 26, 2022

it's not yet included in the getsourcecode api https://api.etherscan.io/api?module=contract&action=getsourcecode

Yeah, I meant that there was talks that it would be included soon-ish. I just don't know when, but if we can get a timeline and its not too far out we should maybe hold off and wait for that?

@joshieDo
Copy link
Collaborator Author

joshieDo commented Apr 26, 2022

It will still take some time for etherscan to add it from my understanding. And when it does arrive, some of these changes will need to happen anyway.(eg. passing sourcemaps per external contracts to the debugger UI).

But even then, I feel like it could be added because:

  • it's not clear all contracts will have a sourcemap. For example. USDT does not have a sourcemap in the UI.
  • other chains' "etherscans" might not add this to their API or be slow to add it.

@onbjerg
Copy link
Collaborator

onbjerg commented Apr 26, 2022

Alright, I was just saying that if it would be added soon there probably wouldn't be much of a point adding something we are going to delete, but if you feel strongly that it will take a long time/be incomplete then you're free to go ahead :)

Comment on lines +173 to +174
decoder: CallTraceDecoder,
known_contracts: BTreeMap<ArtifactId, ContractBytecodeSome>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably merge this at some earlier point instead of passing both of these to the function since they serve the same purpose

Copy link
Contributor

@omkarb omkarb Aug 29, 2022

Choose a reason for hiding this comment

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

Wanted to clarify this real quick, did you mean merge this contract map into one object or merge it with the sources map?

result: RunResult,
decoder: CallTraceDecoder,
known_contracts: BTreeMap<ArtifactId, ContractBytecodeSome>,
sources: BTreeMap<ArtifactId, String>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here

&self,
) -> eyre::Result<(BTreeMap<ArtifactId, String>, BTreeMap<ArtifactId, ContractBytecodeSome>)>
{
if self.compiles {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason you opted for a bool here instead of just ommitting the call to get_compiled_contracts_with_sources when we don't want to compile?

Copy link
Collaborator Author

@joshieDo joshieDo Apr 26, 2022

Choose a reason for hiding this comment

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

older code, part of the planned clean-up

Comment on lines 106 to 108
for (addr, _) in addresses {
fetcher.push(*addr);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can probably filter out addresses here if it is already present in self.contracts

@@ -1011,6 +1018,67 @@ pub fn init_tracing_subscriber() {
.ok();
}

pub async fn compile_contract_source(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should be in the CLI crate instead since we already have a compile module there? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

@onbjerg thinking about this a bit. Problem with having this in the cli crate is that we introduce a cyclical dependency between the evm crate and the cli crate (evm references itself)

One level higher, maybe the cli crate isn't the right place for the compile module at all, it could be in common instead?

wyt?

Copy link
Member

@gakonst gakonst Aug 26, 2022

Choose a reason for hiding this comment

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

I think OK to pull it all in common

Comment on lines +111 to +112
self.contracts
.insert(label.clone(), (source_code, optimization, runs, version));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should do this keyed by address instead - if we have multiple addresses with the same label (which IIRC is just the contract name for this identifier) then we might overwrite info for two contracts with the same name, but with different implementations. E.g. if you interact with two tokens and both of them show up as "Token", then only one of them would be decoded... I think

@onbjerg onbjerg added the T-feature Type: feature label Apr 27, 2022
@gakonst gakonst force-pushed the master branch 4 times, most recently from e4cdc0a to 8112635 Compare May 24, 2022 07:21
@gakonst
Copy link
Member

gakonst commented Jul 30, 2022

@joshieDo are we interested in picking this up again?

@mds1
Copy link
Collaborator

mds1 commented Aug 3, 2022

IMO yes, this would be a really great feature. Having a module for etherscan compilation would also be useful for things like #2494 (comment)

@joshieDo
Copy link
Collaborator Author

joshieDo commented Aug 10, 2022

i'd love to have this feature, but with all the refactors/conflicts (also the incoming TUI one), it's probably better to start from scratch. Maybe use this one as a potential reference.

@gakonst
Copy link
Member

gakonst commented Aug 11, 2022

Agree that this should be started from scratch. Will see if I can find someone to take it over the finish line using this as a reference.

@rkrasiuk rkrasiuk added the A-debugger Area: debugger label Aug 23, 2022
@omkarb
Copy link
Contributor

omkarb commented Aug 23, 2022

Sorry for the delay on this team, I'll have a new draft PR building on the work that JoshieDo has done in the next couple days.

@gakonst
Copy link
Member

gakonst commented Aug 23, 2022

Thanks boss

@gakonst
Copy link
Member

gakonst commented Aug 30, 2022

superseded by #3006

@gakonst gakonst closed this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debugger Area: debugger T-feature Type: feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants