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

exposing GetCodeHash in StateDB interface #455

Open
wants to merge 3 commits into
base: optimism
Choose a base branch
from

Conversation

daleksov
Copy link

@daleksov daleksov commented Dec 28, 2024

Expose GetCodeHash (already merged and exposed in Geth) in the tracing.StateDB Interface for Improved Tracing Performance


Description

This PR proposes exposing the GetCodeHash method in the tracing.StateDB interface. This method is already implemented internally but is not currently publicly accessible. Additionally, it's already exposed in geth https://github.com/ethereum/go-ethereum/blob/c0882429f032da58620bcaa610007939aa7e0adb/core/tracing/hooks.go#L45

Motivation

The GetCodeHash method allows direct retrieval of the code hash for a given address without recomputing it. Without this, users must resort to a workaround like:

// because GetCodeHash is not yet available, you need to calculate it manually
codeHash := gethcrypto.Keccak256Hash(tracing.StateDB.GetCode(addr)) 

This approach involves:

  1. Fetching the contract code.
  2. Computing its Keccak256 hash.

Both steps are computationally expensive, especially when performed repeatedly in tracing scenarios. This inefficiency is particularly problematic for live tracing, where synchronous tracing occurs directly on the node. High overhead in this process can block or slow down the node, leading to potential tracing and block discovery delays.

Proven Results

This method is already exposed and utilized in native Go-Ethereum (geth), where it has delivered significant performance improvements in tracing workflows. By avoiding redundant code fetching and hashing, tools relying on this method have observed a substantial reduction in tracing overhead.

Impact

Exposing GetCodeHash in the tracing.StateDB interface will:

  • Enhance tracing performance by eliminating unnecessary computation.
  • Provide a more efficient way for tracing tools to access code hashes.
  • Reduce the risk of node performance degradation during high-intensity tracing, especially in live tracing environments.

Implementation

This change makes the existing GetCodeHash method publicly accessible via the tracing.StateDB interface, ensuring it can be utilized by external tracing tools without altering its internal logic.

Testing

Given the method is already implemented and in use internally, no changes were made to its core logic. Standard interface exposure testing ensures compatibility.

Example Usage

With this change, users (especially live tracers) can directly call:

codeHash := tracing.StateDB.GetCodeHash(addr)

This removes the need to manually fetch and hash code, streamlining tracing workflows.

Exposing GetCodeHash in the tracing.StateDB interface is a minimal yet impactful change that aligns with best practices and has proven benefits, as seen in native Go-Ethereum. This improvement is expected to significantly enhance the performance and reliability of tracing tools within the Optimism ecosystem.

@daleksov daleksov requested a review from a team as a code owner December 28, 2024 10:39
@daleksov daleksov requested a review from sebastianst December 28, 2024 10:40
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.

1 participant