Skip to content

Commit

Permalink
fix gas return amount
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Jan 17, 2025
1 parent dcc1de6 commit 40ab68c
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,16 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
var gasRefund uint64
if !rules.IsLondon {
// Before EIP-3529: refunds were capped to gasUsed / 2
gasRefund = st.refundGas(params.RefundQuotient)
gasRefund = st.refundAmount(params.RefundQuotient)
} else {
// After EIP-3529: refunds are capped to gasUsed / 5
gasRefund = st.refundGas(params.RefundQuotientEIP3529)
gasRefund = st.refundAmount(params.RefundQuotientEIP3529)
}

if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && gasRefund > 0 {
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+gasRefund, tracing.GasChangeTxRefunds)
}
st.gasRemaining += gasRefund
if rules.IsPrague {
// After EIP-7623: Data-heavy transactions pay the floor gas.
if st.gasUsed() < floorDataGas {
Expand All @@ -525,6 +530,8 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
}
}
}
st.returnGas()

effectiveTip := msg.GasPrice
if rules.IsLondon {
effectiveTip = new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee)
Expand Down Expand Up @@ -615,19 +622,16 @@ func (st *stateTransition) applyAuthorization(msg *Message, auth *types.SetCodeA
return nil
}

func (st *stateTransition) refundGas(refundQuotient uint64) uint64 {
func (st *stateTransition) refundAmount(refundQuotient uint64) uint64 {
// Apply refund counter, capped to a refund quotient
refund := st.gasUsed() / refundQuotient
if refund > st.state.GetRefund() {
refund = st.state.GetRefund()
}
return refund
}

if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && refund > 0 {
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, tracing.GasChangeTxRefunds)
}

st.gasRemaining += refund

func (st *stateTransition) returnGas() {
// Return ETH for remaining gas, exchanged at the original rate.
remaining := uint256.NewInt(st.gasRemaining)
remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice))
Expand All @@ -640,8 +644,6 @@ func (st *stateTransition) refundGas(refundQuotient uint64) uint64 {
// Also return remaining gas to the block gas counter so it is
// available for the next transaction.
st.gp.AddGas(st.gasRemaining)

return refund
}

// gasUsed returns the amount of gas used up by the state transition.
Expand Down

0 comments on commit 40ab68c

Please sign in to comment.