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

Remove buffer gas added in the gas consumption of EVM.dryRun #6844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions fvm/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ func (bl *BlockView) DryRunTransaction(
tx *gethTypes.Transaction,
from gethCommon.Address,
) (*types.Result, error) {
var txResult *types.Result

// create a new procedure
proc, err := bl.newProcedure()
if err != nil {
Expand All @@ -283,42 +281,8 @@ func (bl *BlockView) DryRunTransaction(
// we need to skip nonce check for dry run
msg.SkipAccountChecks = true

// call tracer
if proc.evm.Config.Tracer != nil && proc.evm.Config.Tracer.OnTxStart != nil {
proc.evm.Config.Tracer.OnTxStart(proc.evm.GetVMContext(), tx, msg.From)
}

// 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 &&
proc.evm.Config.Tracer.OnTxEnd != nil &&
txResult != nil {
proc.evm.Config.Tracer.OnTxEnd(txResult.Receipt(), txResult.ValidationError)
}

return txResult, err
// run and return without committing the state changes
return proc.run(msg, tx.Hash(), tx.Type())
}

func (bl *BlockView) newProcedure() (*procedure, error) {
Expand Down Expand Up @@ -707,11 +671,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
Loading