Skip to content

cast: Improve debugger when tracing on-chain transactions/calls #10596

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ngotchac
Copy link
Contributor

@ngotchac ngotchac commented May 22, 2025

Motivation

The current debugger provided in cast is great, but it currently cannot show the source file of local contracts in cast run (for example).

It seems that it can only match contracts to source code, whether from Etherscan (but this requires both an API key, and a verified contract), or locally via --with-local-artifacts. However, w/ --with-local-artifacts, it only matches the contracts for which the deployment is within the same transaction, to get both the deployment bytecode, as well as the runtime one.

Solution

A simple solution to this problem (first commit), is to actually fetch the bytecode of all addresses involved in a trace, to pass it along to the local identifier. This is done for both cast run (which I guess already has all bytecodes available, since it re-executes the tx), as well as cast call, for which I'm unsure if it's a good default, or should it be behind a flag?

Another issue arose, for contracts using libraries. For those, the bytecode isn't convertible to bytes, as it has placeholders. A simple solution is to replace all those placeholders w/ the 0x00.00 address, which should still yield some good matching score. It's also used in the sourcemap generation, in which the actual value of the library's address isn't needed anyway.

Reproduction

Clone the morpho-blue repo: https://github.com/morpho-org/morpho-blue and

cast run --with-local-artifacts -r "<NODE_URL>" --debug 0x32fbce9a55b5b13d65fc5a359d40d742812d409df6c4a7483db3a18f0b8e3d22

and go through the calls until the first call to 0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb

The tx https://etherscan.io/tx/0x32fbce9a55b5b13d65fc5a359d40d742812d409df6c4a7483db3a18f0b8e3d22 calls the Morpho Blue contract in the 5th call, which shows no matching contract on master, but properly shows the contract source code on this branch.

Same with

cast call --with-local-artifacts --trace --debug -r "<NODE_URL>" 0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb "feeRecipient()"

PR Checklist

I haven't added any tests yet, as I wanted to hear from maintainers first on the PR's logic/structure.

  • Added Tests
  • Added Documentation
  • Breaking changes

ngotchac added 2 commits May 22, 2025 23:52
Without fetching the bytecodes from the current chain, matching the
contracts with `--with-local-artifacts` option only works if the
matching contracts have been deployed in the trace. This is very
limiting when trying to `--debug` an on-chain transaction.

By fetching the contracts' bytecodes, we can increase the matching of
address to source file, by thus providing the runtime bytecode.
If a contract contains some libraries, and thus has an "unlinked"
bytecode object, it will never be matched against a deployed instance,
and the source map will never be set.

This fixes this issue by striping from the unlinked bytecode all
placeholders, replacing them with the `0x00..00` address. It doesn't
change anything regarding source-maps, but could change the matching of
the runtime bytecode. The changes are usually minimal in this case, though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

1 participant