From 7869c18c0a8165933efd747a3bf5f53c5a0a5b1b Mon Sep 17 00:00:00 2001 From: MariusVanDerWijden Date: Tue, 13 Jan 2026 09:33:05 +0100 Subject: [PATCH] core: implement eip-7778: block gas accounting without refunds --- core/state_transition.go | 16 +++++++++++----- core/tracing/hooks.go | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index bf5ac07636..b70391ac8c 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -528,6 +528,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { peakGasUsed := st.gasUsed() // Compute refund counter, capped to a refund quotient. + gasBeforeRefunds := peakGasUsed st.gasRemaining += st.calcRefund() if rules.IsPrague { // After EIP-7623: Data-heavy transactions pay the floor gas. @@ -542,7 +543,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { peakGasUsed = floorDataGas } } - st.returnGas() + st.returnGas(rules, gasBeforeRefunds) effectiveTip := msg.GasPrice if rules.IsLondon { @@ -652,7 +653,7 @@ func (st *stateTransition) calcRefund() uint64 { // returnGas returns ETH for remaining gas, // exchanged at the original rate. -func (st *stateTransition) returnGas() { +func (st *stateTransition) returnGas(rules params.Rules, gasBeforeRefunds uint64) { remaining := uint256.NewInt(st.gasRemaining) remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) @@ -661,9 +662,14 @@ func (st *stateTransition) returnGas() { st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned) } - // Also return remaining gas to the block gas counter so it is - // available for the next transaction. - st.gp.AddGas(st.gasRemaining) + if !rules.IsAmsterdam { + // Pre-Amsterdam return remaining gas to the block gas counter so it is + // available for the next transaction. + st.gp.AddGas(st.gasRemaining) + } else { + // Post-Amsterdam only return the remaining gas minus the refunds. + st.gp.AddGas(gasBeforeRefunds) + } } // gasUsed returns the amount of gas used up by the state transition. diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index d17b94cf9c..b7f81a813f 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -358,7 +358,7 @@ const ( // this generates an increase in gas. There is at most one of such gas change per transaction. GasChangeTxRefunds GasChangeReason = 3 // GasChangeTxLeftOverReturned is the amount of gas left over at the end of transaction's execution that will be returned - // to the chain. This change will always be a negative change as we "drain" left over gas towards 0. If there was no gas + // to the account. This change will always be a negative change as we "drain" left over gas towards 0. If there was no gas // left at the end of execution, no such even will be emitted. The returned gas's value in Wei is returned to caller. // There is at most one of such gas change per transaction. GasChangeTxLeftOverReturned GasChangeReason = 4