Skip to content

Commit

Permalink
Add a buffer gas of all but one 64th for gas used on EVM.dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Jun 20, 2024
1 parent 0c0c0d2 commit c00bcec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions fvm/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,21 @@ func (bl *BlockView) DryRunTransaction(
// return without commiting the state
txResult, err := proc.run(msg, tx.Hash(), 0, tx.Type())
if txResult.Successful() {
// As mentioned in https://github.com/ethereum/EIPs/blob/master/EIPS/eip-150.md#specification
// Define "all but one 64th" of N as N - floor(N / 64).
// If a call asks for more gas than the maximum allowed amount
// (i.e. the total amount of gas remaining in the parent after subtracting
// the gas cost of the call and memory expansion), do not return an OOG error;
// instead, if a call asks for more gas than all but one 64th of the maximum
// allowed amount, call with all but one 64th of the maximum allowed amount of
// gas (this is equivalent to a version of EIP-901 plus EIP-1142).
// CREATE only provides all but one 64th of the parent gas to the child call.
txResult.GasConsumed = txResult.GasConsumed + (txResult.GasConsumed / 64)

// Adding `gethParams.SstoreSentryGasEIP2200` is needed for this condition:
// https://github.com/onflow/go-ethereum/blob/master/core/vm/operations_acl.go#L29-L32
txResult.GasConsumed += gethParams.SstoreSentryGasEIP2200

// Take into account any gas refunds, which are calculated only after
// transaction execution.
txResult.GasConsumed += txResult.GasRefund
Expand Down
9 changes: 6 additions & 3 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,9 +1534,10 @@ func TestDryRun(t *testing.T) {
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
expected := res.GasConsumed + (res.GasConsumed / 64) + gethParams.SstoreSentryGasEIP2200
require.Equal(
t,
res.GasConsumed+gethParams.SstoreSentryGasEIP2200,
expected,
dryRunResult.GasConsumed,
)
})
Expand Down Expand Up @@ -1667,9 +1668,10 @@ func TestDryRun(t *testing.T) {
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
expected := res.GasConsumed + (res.GasConsumed / 64) + gethParams.SstoreSentryGasEIP2200
require.Equal(
t,
res.GasConsumed+gethParams.SstoreSentryGasEIP2200,
expected,
dryRunResult.GasConsumed,
)
})
Expand Down Expand Up @@ -1798,9 +1800,10 @@ func TestDryRun(t *testing.T) {
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
expected := res.GasConsumed + (res.GasConsumed / 64) + gethParams.SstoreSentryGasEIP2200 + gethParams.SstoreClearsScheduleRefundEIP3529
require.Equal(
t,
res.GasConsumed+gethParams.SstoreSentryGasEIP2200+gethParams.SstoreClearsScheduleRefundEIP3529,
expected,
dryRunResult.GasConsumed,
)
})
Expand Down

0 comments on commit c00bcec

Please sign in to comment.