Skip to content

Commit 257293f

Browse files
committed
refactor: move consumeMinGas() call inside refundGas()
1 parent f1c61f5 commit 257293f

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

core/state_transition.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
446446
// After EIP-3529: refunds are capped to gasUsed / 5
447447
gasRefund = st.refundGas(params.RefundQuotientEIP3529)
448448
}
449-
st.consumeMinimumGas() // libevm: see comment on method re call-site requirements
450449
effectiveTip := msg.GasPrice
451450
if rules.IsLondon {
452451
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
@@ -479,6 +478,8 @@ func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {
479478
}
480479
st.gasRemaining += refund
481480

481+
st.consumeMinimumGas() // libevm: see comment on method re call-site requirements
482+
482483
// Return ETH for remaining gas, exchanged at the original rate.
483484
remaining := uint256.NewInt(st.gasRemaining)
484485
remaining = remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice))

core/state_transition.libevm.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
package core
1818

1919
import (
20-
"math/big"
21-
22-
"github.com/holiman/uint256"
23-
2420
"github.com/ava-labs/libevm/log"
2521
"github.com/ava-labs/libevm/params"
2622
)
@@ -50,39 +46,16 @@ func (st *StateTransition) canExecuteTransaction() error {
5046

5147
// consumeMinimumGas updates the gas remaining to reflect the value returned by
5248
// [params.RulesHooks.MinimumGasConsumption]. It MUST be called after all code
53-
// that modifies gas consumption; i.e. `st.gasRemaining` MUST remain constant
54-
// after consumeMinimumGas returns.
49+
// that modifies gas consumption but before the balance is returned for
50+
// remaining gas.
5551
func (st *StateTransition) consumeMinimumGas() {
5652
limit := st.msg.GasLimit
57-
minConsume := st.rulesHooks().MinimumGasConsumption(st.msg.GasLimit)
58-
minConsume = min(minConsume, limit) // as documented in [params.RulesHooks]
59-
60-
maxRemaining := limit - minConsume
61-
if st.gasRemaining < maxRemaining {
62-
return
63-
}
64-
65-
diff := st.gasRemaining - maxRemaining
66-
st.gasRemaining -= diff
67-
if err := st.gp.SubGas(diff); err != nil {
68-
// This would mean that the transaction wouldn't have been able to spend
69-
// up to its limit.
70-
log.Crit(
71-
"Broken gas-charging invariant",
72-
"tx limit", limit,
73-
"min consume", minConsume,
74-
"extra consume", diff,
75-
"SubGas() error", err,
76-
)
77-
}
78-
79-
spend := new(big.Int).Mul(st.msg.GasPrice, new(big.Int).SetUint64(diff))
80-
st.state.SubBalance(st.msg.From, uint256.MustFromBig(spend))
81-
82-
log.Debug(
83-
"Consumed extra gas to enforce minimum",
84-
"tx_limit", limit,
85-
"min_consumption", minConsume,
86-
"extra_consumption", diff,
53+
minConsume := min(
54+
limit, // as documented in [params.RulesHooks]
55+
st.rulesHooks().MinimumGasConsumption(limit),
56+
)
57+
st.gasRemaining = min(
58+
st.gasRemaining,
59+
limit-minConsume,
8760
)
8861
}

0 commit comments

Comments
 (0)