diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go index 6a9c91750f..2ace4754a0 100644 --- a/core/parallel_state_processor.go +++ b/core/parallel_state_processor.go @@ -76,12 +76,16 @@ func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateR return cmp.Compare(a.TransactionIndex, b.TransactionIndex) }) - var cumulativeGasUsed uint64 + var ( + cumulativeGasUsed uint64 + cumulativeBlockGasUsed uint64 + ) var allLogs []*types.Log for _, receipt := range receipts { receipt.CumulativeGasUsed = cumulativeGasUsed + receipt.GasUsed cumulativeGasUsed += receipt.GasUsed - if receipt.CumulativeGasUsed > header.GasLimit { + cumulativeBlockGasUsed += receipt.BlockGasUsed + if cumulativeBlockGasUsed > header.GasLimit { return &ProcessResultWithMetrics{ ProcessResult: &ProcessResult{Error: fmt.Errorf("gas limit exceeded")}, } @@ -184,7 +188,7 @@ func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateRea if res.err != nil { execErr = res.err } else { - if err := gp.SubGas(res.receipt.GasUsed); err != nil { + if err := gp.SubGas(res.receipt.BlockGasUsed); err != nil { execErr = err } else { receipts = append(receipts, res.receipt) diff --git a/core/state_processor.go b/core/state_processor.go index 510d5fa1ab..6d545ef582 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -108,16 +108,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg if err != nil { return nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } - usedGas += receipt.GasUsed + usedGas += receipt.BlockGasUsed receipts = append(receipts, receipt) allLogs = append(allLogs, receipt.Logs...) - - /* - enc, _ := json.MarshalIndent(receipt, "", " ") - fmt.Printf("receipt json %s\n", string(enc)) - encRLP, _ := rlp.EncodeToBytes(receipt) - fmt.Printf("receipt rlp %x\n", encRLP) - */ } // Read requests if Prague is enabled. @@ -210,10 +203,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 + receipt.BlockGasUsed = result.MaxUsedGas } else { - receipt.GasUsed = result.UsedGas + receipt.BlockGasUsed = result.UsedGas } if tx.Type() == types.BlobTxType { diff --git a/core/state_transition.go b/core/state_transition.go index 140bdd0fbd..1d2f359967 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -33,7 +33,7 @@ import ( // ExecutionResult includes all output after executing given evm // message no matter the execution itself is successful or not. type ExecutionResult struct { - UsedGas uint64 // Total used gas, not including the refunded gas + UsedGas uint64 // Total gas used, with refunds subtracted MaxUsedGas uint64 // Maximum gas consumed during execution, excluding gas refunds. Err error // Any error encountered during the execution(listed in core/vm/errors.go) ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode) diff --git a/core/types/receipt.go b/core/types/receipt.go index 5b6669f274..6800efd08d 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -63,6 +63,7 @@ type Receipt struct { TxHash common.Hash `json:"transactionHash" gencodec:"required"` ContractAddress common.Address `json:"contractAddress"` GasUsed uint64 `json:"gasUsed" gencodec:"required"` + BlockGasUsed uint64 `json:"blockGasUsed"` EffectiveGasPrice *big.Int `json:"effectiveGasPrice"` // required, but tag omitted for backwards compatibility BlobGasUsed uint64 `json:"blobGasUsed,omitempty"` BlobGasPrice *big.Int `json:"blobGasPrice,omitempty"` diff --git a/miner/worker.go b/miner/worker.go index 7547fe0d4e..73bd8e3ac1 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -66,7 +66,9 @@ type environment struct { coinbase common.Address evm *vm.EVM - header *types.Header + header *types.Header + blockGasUsed uint64 // block gas used incl refunds + txs []*types.Transaction receipts []*types.Receipt sidecars []*types.BlobTxSidecar @@ -382,11 +384,12 @@ func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (* snap = env.state.Snapshot() gp = env.gasPool.Gas() ) - receipt, err := core.ApplyTransaction(env.evm, env.gasPool, env.state, env.header, tx, &env.header.GasUsed) + receipt, err := core.ApplyTransaction(env.evm, env.gasPool, env.state, env.header, tx, &env.blockGasUsed) if err != nil { env.state.RevertToSnapshot(snap) env.gasPool.SetGas(gp) } + env.header.GasUsed += receipt.BlockGasUsed return receipt, err }