Skip to content

Commit

Permalink
Remove buffer gas added in the gas consumption of EVM.dryRun
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Dec 31, 2024
1 parent 219660a commit 1326a7e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
25 changes: 0 additions & 25 deletions fvm/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,6 @@ func (bl *BlockView) DryRunTransaction(

// return without committing the state
txResult, err = proc.run(msg, tx.Hash(), 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 = AddOne64th(txResult.GasConsumed)

// 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
}

// call tracer on tx end
if proc.evm.Config.Tracer != nil &&
Expand Down Expand Up @@ -707,11 +687,6 @@ func (proc *procedure) run(
return &res, nil
}

func AddOne64th(n uint64) uint64 {
// NOTE: Go's integer division floors, but that is desirable here
return n + (n / 64)
}

func convertAndCheckValue(input *big.Int) (isValid bool, converted *uint256.Int) {
// check for negative input
if input.Sign() < 0 {
Expand Down
41 changes: 10 additions & 31 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/onflow/flow-go/fvm/environment"
envMock "github.com/onflow/flow-go/fvm/environment/mock"
"github.com/onflow/flow-go/fvm/evm"
"github.com/onflow/flow-go/fvm/evm/emulator"
"github.com/onflow/flow-go/fvm/evm/events"
"github.com/onflow/flow-go/fvm/evm/impl"
"github.com/onflow/flow-go/fvm/evm/stdlib"
Expand Down Expand Up @@ -1513,11 +1512,13 @@ func TestDryRun(t *testing.T) {
evmAddress,
))

// Use the gas estimation from Evm.dryRun with some buffer
gasLimit := dryRunResult.GasConsumed + gethParams.SstoreSentryGasEIP2200
innerTxBytes := testAccount.PrepareSignAndEncodeTx(t,
testContract.DeployedAt.ToCommon(),
data,
big.NewInt(0),
dryRunResult.GasConsumed, // use the gas estimation from Evm.dryRun
gasLimit,
big.NewInt(0),
)

Expand Down Expand Up @@ -1545,15 +1546,7 @@ func TestDryRun(t *testing.T) {
require.NoError(t, err)
require.Equal(t, types.StatusSuccessful, res.Status)
require.Equal(t, types.ErrCodeNoError, res.ErrorCode)
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
totalGas := emulator.AddOne64th(res.GasConsumed) + gethParams.SstoreSentryGasEIP2200
require.Equal(
t,
totalGas,
dryRunResult.GasConsumed,
)
require.Equal(t, res.GasConsumed, dryRunResult.GasConsumed)
})
})

Expand Down Expand Up @@ -1679,15 +1672,7 @@ func TestDryRun(t *testing.T) {
require.NoError(t, err)
require.Equal(t, types.StatusSuccessful, res.Status)
require.Equal(t, types.ErrCodeNoError, res.ErrorCode)
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
totalGas := emulator.AddOne64th(res.GasConsumed) + gethParams.SstoreSentryGasEIP2200
require.Equal(
t,
totalGas,
dryRunResult.GasConsumed,
)
require.Equal(t, res.GasConsumed, dryRunResult.GasConsumed)
})
})

Expand Down Expand Up @@ -1779,11 +1764,13 @@ func TestDryRun(t *testing.T) {
evmAddress,
))

// use the gas estimation from Evm.dryRun with the necessary buffer gas
gasLimit := dryRunResult.GasConsumed + gethParams.SstoreSentryGasEIP2200 + gethParams.SstoreClearsScheduleRefundEIP3529
innerTxBytes = testAccount.PrepareSignAndEncodeTx(t,
testContract.DeployedAt.ToCommon(),
data,
big.NewInt(0),
dryRunResult.GasConsumed, // use the gas estimation from Evm.dryRun
gasLimit,
big.NewInt(0),
)

Expand All @@ -1809,17 +1796,9 @@ func TestDryRun(t *testing.T) {

res, err := impl.ResultSummaryFromEVMResultValue(output.Value)
require.NoError(t, err)
//require.Equal(t, types.StatusSuccessful, res.Status)
require.Equal(t, types.StatusSuccessful, res.Status)
require.Equal(t, types.ErrCodeNoError, res.ErrorCode)
// Make sure that gas consumed from `EVM.dryRun` is bigger
// than the actual gas consumption of the equivalent
// `EVM.run`.
totalGas := emulator.AddOne64th(res.GasConsumed) + gethParams.SstoreSentryGasEIP2200 + gethParams.SstoreClearsScheduleRefundEIP3529
require.Equal(
t,
totalGas,
dryRunResult.GasConsumed,
)
require.Equal(t, res.GasConsumed, dryRunResult.GasConsumed)
})
})

Expand Down

0 comments on commit 1326a7e

Please sign in to comment.