diff --git a/core/state_processor.go b/core/state_processor.go index f61c20035d..510d5fa1ab 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -62,7 +62,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() @@ -103,10 +104,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...) @@ -147,7 +149,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg Receipts: receipts, Requests: requests, Logs: allLogs, - GasUsed: *usedGas, + GasUsed: usedGas, }, nil } @@ -208,7 +210,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 f2dc377a86..0e9c3283f9 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -541,7 +541,14 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { peakGasUsed = floorDataGas } } + // 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 { @@ -669,10 +676,6 @@ func (st *stateTransition) returnGas() { 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) } - - // Also return remaining gas to the block gas counter so it is - // available for the next transaction. - st.gp.AddGas(st.gasRemaining) } // 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 75a277be40..402a652258 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -398,12 +398,10 @@ const ( // 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 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 is the amount of gas left over at the end of transaction's execution that will be returned + // 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 // GasChangeCallInitialBalance is the initial balance for the call which