From ce8f1bd856911b90b6712f5c8acc43b4a6c33e6e Mon Sep 17 00:00:00 2001 From: MariusVanDerWijden Date: Tue, 3 Feb 2026 08:15:18 +0100 Subject: [PATCH] core: fix 7778 logic --- core/state_processor.go | 14 ++++++++++---- core/state_transition.go | 21 +++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/state_processor.go b/core/state_processor.go index b4b22e4318..f1a69fb96d 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -61,7 +61,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg var ( config = p.chainConfig() receipts types.Receipts - usedGas = new(uint64) + chargedGas = new(uint64) + usedGas = uint64(0) header = block.Header() blockHash = block.Hash() blockNumber = block.Number() @@ -102,10 +103,11 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg } statedb.SetTxContext(tx.Hash(), i) - receipt, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, context.Time, tx, usedGas, evm) + receipt, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, context.Time, tx, chargedGas, evm) if err != nil { return nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } + usedGas += receipt.GasUsed receipts = append(receipts, receipt) allLogs = append(allLogs, receipt.Logs...) } @@ -134,7 +136,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg Receipts: receipts, Requests: requests, Logs: allLogs, - GasUsed: *usedGas, + GasUsed: usedGas, }, nil } @@ -183,7 +185,11 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b receipt.Status = types.ReceiptStatusSuccessful } receipt.TxHash = tx.Hash() - receipt.GasUsed = result.UsedGas + if evm.ChainConfig().IsAmsterdam(blockNumber, blockTime) { + receipt.GasUsed = result.MaxUsedGas + } else { + receipt.GasUsed = result.UsedGas + } if tx.Type() == types.BlobTxType { receipt.BlobGasUsed = uint64(len(tx.BlobHashes()) * params.BlobTxBlobGasPerBlob) diff --git a/core/state_transition.go b/core/state_transition.go index b70391ac8c..bcce8508a1 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -528,7 +528,6 @@ 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. @@ -543,7 +542,14 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { peakGasUsed = floorDataGas } } - st.returnGas(rules, gasBeforeRefunds) + // Return gas to the user + st.returnGas() + // Return gas to the gas pool + if rules.IsAmsterdam { + st.gp.AddGas(st.initialGas - peakGasUsed) + } else { + st.gp.AddGas(st.gasRemaining) + } effectiveTip := msg.GasPrice if rules.IsLondon { @@ -653,7 +659,7 @@ func (st *stateTransition) calcRefund() uint64 { // returnGas returns ETH for remaining gas, // exchanged at the original rate. -func (st *stateTransition) returnGas(rules params.Rules, gasBeforeRefunds uint64) { +func (st *stateTransition) returnGas() { remaining := uint256.NewInt(st.gasRemaining) remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) @@ -661,15 +667,6 @@ func (st *stateTransition) returnGas(rules params.Rules, gasBeforeRefunds uint64 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) } - - 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.