Skip to content
Merged
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
9 changes: 6 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
// the gas used (which includes gas refunds) and an error if it failed. An error always
// indicates a core error meaning that the message would always fail for that particular
// state and would never be accepted within a block.
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, error) {
return NewStateTransition(evm, msg, gp).TransitionDb()
func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool, opts ...StateTransitionOption) (*ExecutionResult, error) {
return NewStateTransition(evm, msg, gp, opts...).TransitionDb()
}

// StateTransition represents a state transition.
Expand Down Expand Up @@ -218,15 +218,18 @@ type StateTransition struct {
initialGas uint64
state vm.StateDB
evm *vm.EVM

opts []StateTransitionOption //libevm
}

// NewStateTransition initialises and returns a new state transition object.
func NewStateTransition(evm *vm.EVM, msg *Message, gp *GasPool) *StateTransition {
func NewStateTransition(evm *vm.EVM, msg *Message, gp *GasPool, opts ...StateTransitionOption) *StateTransition {
return &StateTransition{
gp: gp,
evm: evm,
msg: msg,
state: evm.StateDB,
opts: opts,
}
}

Expand Down
21 changes: 21 additions & 0 deletions core/state_transition.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/libevm"
"github.com/ava-labs/libevm/libevm/options"
"github.com/ava-labs/libevm/log"
"github.com/ava-labs/libevm/params"
)
Expand All @@ -44,6 +45,21 @@ func (st *StateTransition) rulesHooks() params.RulesHooks {
// comments like [vm.EVM].
var _ = (*vm.EVM)(nil)

type stateTransitionConfig struct {
disableMinimumGasConsumption bool
}

// StateTransitionOption configures a [StateTransition].
type StateTransitionOption = options.Option[stateTransitionConfig]

// WithoutMinimumGasConsumption disables the
// [params.RulesHooks.MinimumGasConsumption] hook for a state transition.
func WithoutMinimumGasConsumption() StateTransitionOption {
return options.Func[stateTransitionConfig](func(c *stateTransitionConfig) {
c.disableMinimumGasConsumption = true
})
}

// TransitionDb will transition the state by applying the current message and
// returning the evm execution result with following fields.
//
Expand Down Expand Up @@ -106,6 +122,11 @@ func (st *StateTransition) afterGasRefund(refunded uint64) {
st.gasRemaining -= refunded
}

c := options.As[stateTransitionConfig](st.opts...)
if c.disableMinimumGasConsumption {
return
}

limit := st.msg.GasLimit
minConsume := min(
limit, // as documented in [params.RulesHooks]
Expand Down
55 changes: 55 additions & 0 deletions core/state_transition.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/consensus"
"github.com/ava-labs/libevm/core"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/core/vm"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/libevm/eth/gasestimator"
"github.com/ava-labs/libevm/eth/tracers"
"github.com/ava-labs/libevm/eth/tracers/native"
"github.com/ava-labs/libevm/libevm"
Expand Down Expand Up @@ -304,6 +306,59 @@ func TestMinimumGasConsumption(t *testing.T) {
}
}

type stubChainContext struct {
core.ChainContext
}

func (stubChainContext) Engine() consensus.Engine { return stubEngine{} }

type stubEngine struct {
consensus.Engine
}

func (stubEngine) Author(h *types.Header) (common.Address, error) {
return h.Coinbase, nil
}

func TestGasEstimationIgnoresMinConsumption(t *testing.T) {
const limit = 1e8
from := common.Address{'m', 'e'}
hooks := &hookstest.Stub{
MinimumGasConsumptionFn: func(gasLimit uint64) uint64 {
return gasLimit / 2
Comment thread
JonathanOppenheimer marked this conversation as resolved.
},
}
hooks.Register(t)

_, _, sdb := ethtest.NewEmptyStateDB(t)
sdb.SetBalance(from, new(uint256.Int).SetAllOne())

opts := &gasestimator.Options{
Config: params.MergedTestChainConfig,
Chain: stubChainContext{},
Header: &types.Header{
Number: big.NewInt(1),
BaseFee: big.NewInt(1),
GasLimit: limit,
Coinbase: common.Address{1},
Difficulty: big.NewInt(1),
},
State: sdb,
}
msg := &core.Message{
From: from,
GasPrice: big.NewInt(1),
GasFeeCap: big.NewInt(1),
GasTipCap: big.NewInt(0),
GasLimit: limit,
Value: big.NewInt(0),
}

got, _, err := gasestimator.Estimate(t.Context(), msg, opts, limit)
require.NoError(t, err, "gasestimator.Estimate(...)")
require.Equal(t, params.TxGasContractCreation, got, "gasestimator.Estimate(...)")
}

// TestCreditBaseFeeToCoinbase tests that the coinbase is credited with the
// base fee if enabled in the hooks and post-London. Additionally, these state
// changes should be conveyed to the tracer.
Expand Down
2 changes: 1 addition & 1 deletion eth/gasestimator/gasestimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func run(ctx context.Context, call *core.Message, opts *Options) (*core.Executio
evm.Cancel()
}()
// Execute the call, returning a wrapped error or the result
result, err := core.ApplyMessage(evm, call, new(core.GasPool).AddGas(math.MaxUint64))
result, err := core.ApplyMessage(evm, call, new(core.GasPool).AddGas(math.MaxUint64), core.WithoutMinimumGasConsumption())
if vmerr := dirtyState.Error(); vmerr != nil {
return nil, vmerr
}
Expand Down
3 changes: 2 additions & 1 deletion params/hooks.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type RulesHooks interface {
// minimum quantity of gas units to be charged for said transaction. If the
// returned value is greater than the transaction's limit, the minimum spend
// will be capped at the limit. The minimum spend will be applied _after_
// refunds, if any.
// refunds, if any. A state transition can explicitly disable the minimum,
// such as when estimating the gas required for execution.
MinimumGasConsumption(txGasLimit uint64) (gas uint64)
// ShouldCreditBaseFeeToCoinbase returns whether or not to credit the
// block's base fee to the coinbase address. By default, it will NOT be
Expand Down
Loading