mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
feat: limit EVM max gas refund to 150% of used gas
- Encourages more accurate gas estimation - Previously, 100% of unused gas was refunded
This commit is contained in:
parent
67b7bcc379
commit
4c82168439
1 changed files with 21 additions and 13 deletions
|
|
@ -471,31 +471,39 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {
|
func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {
|
||||||
// Apply refund counter, capped to a refund quotient
|
// Calculate state change refund
|
||||||
refund := st.gasUsed() / refundQuotient
|
stateRefund := st.gasUsed() / refundQuotient
|
||||||
if refund > st.state.GetRefund() {
|
if stateRefund > st.state.GetRefund() {
|
||||||
refund = st.state.GetRefund()
|
stateRefund = st.state.GetRefund()
|
||||||
}
|
}
|
||||||
|
|
||||||
if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && refund > 0 {
|
// Calculate the total refund (unused gas + state refund)
|
||||||
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, tracing.GasChangeTxRefunds)
|
totalRefund := st.gasRemaining + stateRefund
|
||||||
|
|
||||||
|
// Calculate the maximum refund allowed (150% of gas used)
|
||||||
|
maxRefund := st.gasUsed()/2 + st.gasUsed()
|
||||||
|
|
||||||
|
// Ensure the refund does not exceed the maximum allowed
|
||||||
|
if totalRefund > maxRefund {
|
||||||
|
totalRefund = maxRefund
|
||||||
}
|
}
|
||||||
|
|
||||||
st.gasRemaining += refund
|
// Update gasRemaining
|
||||||
|
st.gasRemaining += stateRefund
|
||||||
|
|
||||||
// Return ETH for remaining gas, exchanged at the original rate.
|
// Return ETH for total refund, exchanged at the original rate.
|
||||||
remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gasRemaining), st.msg.GasPrice)
|
totalRefundValue := new(big.Int).Mul(new(big.Int).SetUint64(totalRefund), st.msg.GasPrice)
|
||||||
st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn)
|
st.state.AddBalance(st.msg.From, totalRefundValue, tracing.BalanceIncreaseGasReturn)
|
||||||
|
|
||||||
|
// Notify the tracer about the gas change
|
||||||
if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining > 0 {
|
if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining > 0 {
|
||||||
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned)
|
st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also return remaining gas to the block gas counter so it is
|
// Update gas pool
|
||||||
// available for the next transaction.
|
|
||||||
st.gp.AddGas(st.gasRemaining)
|
st.gp.AddGas(st.gasRemaining)
|
||||||
|
|
||||||
return refund
|
return stateRefund
|
||||||
}
|
}
|
||||||
|
|
||||||
// gasUsed returns the amount of gas used up by the state transition.
|
// gasUsed returns the amount of gas used up by the state transition.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue