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

fix(levm): sstore refund logic #1559

Merged
merged 5 commits into from
Dec 23, 2024
Merged

fix(levm): sstore refund logic #1559

merged 5 commits into from
Dec 23, 2024

Conversation

damiramirez
Copy link
Contributor

@damiramirez damiramirez commented Dec 23, 2024

Motivation

There was a discrepancies in gas usage and refunds. The issue was related to an improper management of gas refunds in sstore.

Description

There was an error when calculating the gas refund. Adjust the conditional logic to subtract refunds when reverting storage changes and adding refunds appropriately. Also, reordered refund and cost logic.

@damiramirez damiramirez self-assigned this Dec 23, 2024
@damiramirez damiramirez added the levm Lambda EVM implementation label Dec 23, 2024
@damiramirez damiramirez marked this pull request as ready for review December 23, 2024 17:45
@damiramirez damiramirez requested a review from a team as a code owner December 23, 2024 17:45
Copy link
Contributor

@JereSalo JereSalo left a comment

Choose a reason for hiding this comment

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

Nice!
I noticed that in execution specs they do this at the beginning of the opcode:

if evm.gas_left <= GAS_CALL_STIPEND:
     raise OutOfGasError

I think it is for avoiding accessing to the storage slot if we don't have enough gas, it feels like an optimization. Do you think we can add it here?

@damiramirez
Copy link
Contributor Author

damiramirez commented Dec 23, 2024

@JereSalo

Nice!
I noticed that in execution specs they do this at the beginning of the opcode:

if evm.gas_left <= GAS_CALL_STIPEND:
     raise OutOfGasError

I think it is for avoiding accessing to the storage slot if we don't have enough gas, it feels like an optimization. Do you think we can add it here?

We are doing that in the cost function:

pub fn sstore(
    storage_slot: &StorageSlot,
    new_value: U256,
    storage_slot_was_cold: bool,
    current_call_frame: &CallFrame,
) -> Result<u64, VMError> {
    // EIP-2200
    let gas_left = current_call_frame
        .gas_limit
        .checked_sub(current_call_frame.gas_used)
        .ok_or(OutOfGasError::ConsumedGasOverflow)?;
    if gas_left <= SSTORE_STIPEND {
        return Err(VMError::OutOfGas(OutOfGasError::MaxGasLimitExceeded));
    }
...

I can move it to the opcode function and if this case happens, we avoid doing extra calculations.

Done fce0d53

Copy link
Contributor

@JereSalo JereSalo left a comment

Choose a reason for hiding this comment

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

n1

@JereSalo JereSalo added this pull request to the merge queue Dec 23, 2024
Merged via the queue into main with commit 3b440a8 Dec 23, 2024
4 checks passed
@JereSalo JereSalo deleted the fix/sstore-refund branch December 23, 2024 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
levm Lambda EVM implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants