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

core/tracing: add GetCodeHash to StateDB #30784

Merged

Conversation

nebojsa94
Copy link
Contributor

This PR extends tracing.StateDB by adding a GetCodeHash function.

Motivation

  • Currently, tracing.StateDB supports 2 out of 3 basic account data getters (GetBalance and GetNonce). Adding GetCodeHash completes the set, making it 3/3.

  • This addition significantly improves the performance of tracers that need to compute code hashes. Previously, the only available option was to use crypto.Keccak256Hash(t.stateDB.GetCode(addr)), which is less efficient.

@nebojsa94 nebojsa94 requested a review from s1na as a code owner November 21, 2024 18:06
Copy link
Contributor

@jwasinger jwasinger left a comment

Choose a reason for hiding this comment

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

SGTM

@holiman
Copy link
Contributor

holiman commented Nov 22, 2024

Again, is there a practical usecase which drives this, or is it just about symmetry? Do you have a tracer which does a lot of hashing and suffers from performance bottlenecks?

@nebojsa94
Copy link
Contributor Author

Yes, there is a practical use case for GetCodeHash. We aim to track the state objects required during the execution of each transaction in a block, as well as the resulting state changes. Instead of storing the entire code in the state object for every transaction (which would significantly increase result size), we store only the codeHash for each transaction’s state object and maintain a separate map of codeHash => code.

Currently, hashing every new code encountered during block execution increases tracing time by a factor of 5 and overall block execution time by a factor of 2. This performance bottleneck is significant, and GetCodeHash provides a direct way to address it.

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

LGTM
@s1na ?

Copy link
Contributor

@s1na s1na left a comment

Choose a reason for hiding this comment

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

LGTM

@holiman holiman added this to the 1.14.13 milestone Nov 25, 2024
@MariusVanDerWijden MariusVanDerWijden merged commit d7e7b54 into ethereum:master Nov 26, 2024
3 checks passed
@nebojsa94 nebojsa94 deleted the tracing-statedb-get-codehash branch December 4, 2024 11:23
@fjl fjl modified the milestones: 1.14.13, 1.15.0 Jan 23, 2025
tokeyg pushed a commit to tokeyg/go-ethereum that referenced this pull request Feb 6, 2025
This PR extends the tracing.StateDB interface by adding a GetCodeHash function.
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.

6 participants