Skip to content

Conversation

@ben-chain
Copy link
Collaborator

Description

This PR implements a proxy contract which sits between the EM and SM. The proxy serves two purposes:

  1. Burning any leftover gas from the calls to the SM. This makes the OVM's gas behavior more deterministic, so that different SM implementations do not change downstream results of the GAS opcode in code contracts.
  2. Records all gas consumed by the proxied calls to the SM in storage. This allows the EM to "refund" the overhead of the PSM, so that the gas we meter in the OVM is based on L2 execution costs and not the L1 costs.

Questions

  • How should we configure the large set of constants in the Gas Sanitizer?

Metadata

Fixes

  • Fixes YAS 422

Contributing Agreement

Copy link

@willmeister willmeister left a comment

Choose a reason for hiding this comment

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

Left a few comments, but LGTM 👍

Might also suggest @karlfloersch to take a look, as this is right at the core of our stuff.


// Internal Logic
function addToOVMRefund(uint _refund) internal {
OVMRefund += _refund;

Choose a reason for hiding this comment

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

Do we need to require(MAX_UINT - OVMRefund >= _refund, "overflow");? Based on context, we may not care.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think it's possible for any reasonable value for the constants at the top of the file, simply due to the EVM's gas limit.

// todo safemath negatives
GasConsumer gasConsumer = GasConsumer(resolveGasConsumer());
uint gasAlreadyConsumed = initialGas - gasleft();
uint gasLeftToConsume = _sanitizedGasCost - gasAlreadyConsumed;

Choose a reason for hiding this comment

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

Just want to make sure it's not possible for this to underflow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already have to make an assumption about the max gas an SM implementation will consume, which is why the constants above are called *_GAS_COST_UPPER_BOUND. If that breaks, then we will have bigger issues than the underflow here, so I figured it's fine.

uint _sanitizedGasCost,
uint _virtualGasCost
) internal {
uint initialGas = gasleft();

Choose a reason for hiding this comment

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

Should there be a require(_sanitizedGasCost >= _virtualGasCost, "Logic will underflow if sanitized gas cost is lower than virtual gas cost"); ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since those are parameters/constants we must set ourselves, I think okay to omit such a check.


// Overhead for checking methodId etc in this function before the actual call()
// This was figured out empirically during testing--adding methods or changing compiler settings will require recalibration.
uint constant constantOverheadEOA = 947;

Choose a reason for hiding this comment

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

Will this ever change through hardforks, and if so, how can we update it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It could, but it's a case where other critical components will break first. In this particular instance I believe the impact would be a constant size change in OVM gas consumed, but still deterministic.

uint gasToAlloc = _amount - constantOverhead;
// Overhead for checking methodId, etc. in this function before the actual call()
// This was figured out empirically during testing--adding methods or changing compiler settings will require recalibration.
uint constant constantOverheadInternal = 2514;

Choose a reason for hiding this comment

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

Same about updating

@ben-chain
Copy link
Collaborator Author

This PR didn't get in until some big upstream changes, closing in favor of #217 which built on top of this PR and then merged.

@ben-chain ben-chain closed this Aug 25, 2020
@gakonst gakonst deleted the feat/YAS422/enforce-SM-gas-determinism branch March 18, 2021 15:02
snario pushed a commit that referenced this pull request Apr 14, 2021
bap2pecs pushed a commit to babylonlabs-io/optimism that referenced this pull request Jul 31, 2024
xibao-nr pushed a commit to node-real/combo-optimism that referenced this pull request Feb 19, 2025
shenkeyao referenced this pull request in EspressoSystems/optimism-espresso-integration Mar 1, 2025
QuentinI referenced this pull request in EspressoSystems/optimism-espresso-integration Mar 7, 2025
Zena-park added a commit to tokamak-network/optimism that referenced this pull request Dec 30, 2025
theochap pushed a commit that referenced this pull request Jan 15, 2026
### Description

Documents the `op-alloy-consensus` crate in the mdbook.
emhane added a commit that referenced this pull request Feb 3, 2026
Closes #212

---------

Co-authored-by: Emilia Hane <[email protected]>
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.

3 participants