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

🗞️ Add _options gas profiler #1099

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

🗞️ Add _options gas profiler #1099

wants to merge 22 commits into from

Conversation

St0rmBr3w
Copy link
Contributor

@St0rmBr3w St0rmBr3w commented Dec 3, 2024

In this PR:

This PR introduces a Gas Profiler for estimating the gas usage of LayerZero's lzReceive and lzCompose functions. It simplifies running Foundry scripts by adding pnpm commands that allow dynamic argument passing, making gas profiling more accessible and efficient for developers.

Some limitations are that Foundry does not accurately return L1 data fees on most L2s as referenced in foundry-rs/foundry/issues/6253, so the gas units returned are only part of the profile necessary to determine enforcedOptions, but would be curious if you have any ideas on making it better / more performant.

Key Features

  1. Commands for Gas Profiling:

    • gas:lzReceive: Profiles the lzReceive function.
    • gas:lzCompose: Profiles the lzCompose function.
  2. Dynamic Arguments:

    • Pass required parameters directly to the commands for custom configurations.
  3. Detailed Output:

    • Provides gas usage metrics including average, median, minimum, and maximum values across multiple runs.

Usage Examples

lzReceive Gas Profiling

pnpm gas:lzReceive \
  <rpcUrl> \
  <endpointAddress> \
  <srcEid> \
  <sender> \
  <dstEid> \
  <receiver> \
  <message> \
  <msgValue> \
  <numOfRuns>

lzCompose Gas Profiling

pnpm gas:lzCompose \
  <rpcUrl> \
  <endpointAddress> \
  <srcEid> \
  <sender> \
  <dstEid> \
  <receiver> \
  <composer> \
  <composeMsg> \
  <msgValue> \
  <numOfRuns>

Benefits

  • Simplifies repetitive gas profiling tasks.
  • Ensures consistent metrics across multiple test runs.
  • Allows for customization based on specific configurations and use cases.

This addition enhances developer tooling for LayerZero's protocol, providing actionable insights into gas optimization for enforcedOptions and extraOptions configuration.

Limitations

1. Overestimation of Gas:

EDIT: This was solved with the addition ofvm.lastCallGas().

  1. Custom Safety Checks:

    • The implementation assumes the generic safety checks are in place (e.g., ensuring the caller is the endpoint and the call originates from the correct OApp).
    • If the receiver contract includes additional custom checks, these must be manually overridden in the script for accurate results.
  2. Snapshot and Fork Dependency:

    • Results are dependent on the snapshot or fork state at the time of execution.
    • The gas profiling does not produce accurate results across multiple calls with different nonces. While calls execute successfully, the gas amount returned is often significantly lower, suggesting some state inconsistencies during resets.

    EDIT: This again is solved with the addition of vm.lastCallGas().

Recommendations

  • Use private RPCs to avoid timeouts when running multiple test cases.
  • Adjust test parameters (numOfRuns, msgValue, etc.) to match your specific use case for more reliable results.
  • Validate custom receiver contract logic independently to ensure compatibility with this profiler.

DanL0

This comment was marked as outdated.

@St0rmBr3w St0rmBr3w requested a review from DanL0 December 4, 2024 19:52
@St0rmBr3w St0rmBr3w changed the title feat: added gas profiler feat: add _options gas profiler Dec 7, 2024
@St0rmBr3w St0rmBr3w changed the title feat: add _options gas profiler 🗞️ Add _options gas profiler Dec 7, 2024
@St0rmBr3w
Copy link
Contributor Author

Based on some feedback and back and forth with off-chain team, I think that the scope of this can be reduced slightly.

Fork testing might not even be needed if we strictly just need to predict the EVM L2 execution cost. Since L1 data fee is not something we're in a place to abstract right now, the _options param can essentially only predict the L2 execution cost (special exception for different compilers like zkSolc).

In the future, we may have teams try to estimate the L1 data fee, but for today L2 execution cost maximum's is likely the best possible state.

@ryandgoulding
Copy link
Contributor

@St0rmBr3w is this ready for review or do you want to make some more updates based on your above comment? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants