Skip to content

Commit b33ce77

Browse files
committed
core, eth/gasestimator: introduce MaximumGasUsed for estimation
Issue: ethereum/go-ethereum#31682 Reference: ethereum/go-ethereum#31735
1 parent 23d0fa0 commit b33ce77

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

core/state_transition.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ type Message interface {
9797
// ExecutionResult includes all output after executing given evm
9898
// message no matter the execution itself is successful or not.
9999
type ExecutionResult struct {
100-
UsedGas uint64 // Total used gas, not including the refunded gas
101-
RefundedGas uint64 // Total gas refunded after execution
102-
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
103-
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
100+
UsedGas uint64 // Total used gas, not including the refunded gas
101+
MaxUsedGas uint64 // Maximum gas consumed during execution, excluding gas refunds.
102+
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
103+
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
104104
}
105105

106106
// Unwrap returns the internal evm error which allows us for further
@@ -502,16 +502,22 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
502502
ret, st.gas, vmerr = st.evm.Call(sender, st.to(), st.data, st.gas, st.value)
503503
}
504504

505-
var gasRefund uint64
505+
var peakGasUsed uint64
506506
if !st.evm.Config.IsSystemTransaction {
507+
// Record the gas used excluding gas refunds. This value represents the actual
508+
// gas allowance required to complete execution.
509+
peakGasUsed = st.gasUsed()
510+
507511
// Compute refund counter, capped to a refund quotient.
508-
gasRefund = st.calcRefund()
509-
st.gas += gasRefund
512+
st.gas += st.calcRefund()
510513
if rules.IsPrague {
511514
// After EIP-7623: Data-heavy transactions pay the floor gas.
512515
if st.gasUsed() < floorDataGas {
513516
st.gas = st.initialGas - floorDataGas
514517
}
518+
if peakGasUsed < floorDataGas {
519+
peakGasUsed = floorDataGas
520+
}
515521
}
516522
st.returnGas()
517523

@@ -539,10 +545,10 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
539545
}
540546

541547
return &ExecutionResult{
542-
UsedGas: st.gasUsed(),
543-
RefundedGas: gasRefund,
544-
Err: vmerr,
545-
ReturnData: ret,
548+
UsedGas: st.gasUsed(),
549+
MaxUsedGas: peakGasUsed,
550+
Err: vmerr,
551+
ReturnData: ret,
546552
}, nil
547553
}
548554

eth/gasestimator/gasestimator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Estimate(ctx context.Context, call core.Message, opts *Options, gasCap uint
142142
// There's a fairly high chance for the transaction to execute successfully
143143
// with gasLimit set to the first execution's usedGas + gasRefund. Explicitly
144144
// check that gas amount and use as a limit for the binary search.
145-
optimisticGasLimit := (result.UsedGas + result.RefundedGas + params.CallStipend) * 64 / 63
145+
optimisticGasLimit := (result.MaxUsedGas + params.CallStipend) * 64 / 63
146146
if optimisticGasLimit < hi {
147147
failed, _, err = execute(ctx, call, opts, optimisticGasLimit)
148148
if err != nil {

0 commit comments

Comments
 (0)