Skip to content

fix(levm): fix gas refunds #1410

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

Merged
merged 1 commit into from
Dec 5, 2024
Merged

fix(levm): fix gas refunds #1410

merged 1 commit into from
Dec 5, 2024

Conversation

maximopalopoli
Copy link
Contributor

@maximopalopoli maximopalopoli commented Dec 4, 2024

Motivation

The gas refunds had a bug in the sstore calculation. Fixes all the tests from stShift folder.

Description

Also, this PR sums the refunds to the sender's balance.

@maximopalopoli maximopalopoli added ef-tests Hive tests, execution-spec-tests levm Lambda EVM implementation labels Dec 4, 2024
@maximopalopoli maximopalopoli self-assigned this Dec 4, 2024
@maximopalopoli maximopalopoli marked this pull request as ready for review December 4, 2024 21:05
@maximopalopoli maximopalopoli requested a review from a team as a code owner December 4, 2024 21:05
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.

LGTM

sender,
U256::from(report.gas_refunded)
.checked_mul(self.env.gas_price)
.ok_or(VMError::GasLimitPriceProductOverflow)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this error name is not the most appropriate but it's ok though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be OutOfGas or BalanceUnderflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The overflow would be the result of multiplying the refunded gas by the gas price, so it should not be an issue in the balance sheet itself. Furthermore, since it is added to the balance sheet, it would be an overflow in any case.

@JereSalo
Copy link
Contributor

JereSalo commented Dec 5, 2024

Wait, I remembered that, according to the Yellowpaper, the gas refunds have to be capped at 1/5th of the total gas used.
I would do something like:

        let refunded_gas = report.gas_refunded.min(
            consumed_gas
                .checked_div(5)
                .ok_or(VMError::Internal(InternalError::UndefinedState(-1)))?,
        );

This PR is a good fit for that I believe, but we have to ensure that at least the same amount of tests pass

@JereSalo
Copy link
Contributor

JereSalo commented Dec 5, 2024

One extra thing, today (or at the latest tomorrow) I'll probably change the logic behind gas consumption and the update of the user's balance. So we can merge this and then change that.

@JereSalo JereSalo added this pull request to the merge queue Dec 5, 2024
Merged via the queue into main with commit 378fa53 Dec 5, 2024
7 checks passed
@JereSalo JereSalo deleted the levm_gas_refunds_fix branch December 5, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ef-tests Hive tests, execution-spec-tests levm Lambda EVM implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants