Skip to content

Commit 6e0ba55

Browse files
authored
fix eip-7778 in the miner and BAL parallel state processor (#33761)
alternative to #33754 Adds an extra non-consensus field on the `Receipt`: `BlockGasUsed`. This reflects the gas used as it counts towards the block gas limit, whereas the existing field `GasUsed` is the consensus field: the gas used with refunds subtracted. Changes an incorrect comment describing the field `ExecutionResult.UsedGas`.
1 parent 022e460 commit 6e0ba55

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

core/parallel_state_processor.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateR
7676
return cmp.Compare(a.TransactionIndex, b.TransactionIndex)
7777
})
7878

79-
var cumulativeGasUsed uint64
79+
var (
80+
cumulativeGasUsed uint64
81+
cumulativeBlockGasUsed uint64
82+
)
8083
var allLogs []*types.Log
8184
for _, receipt := range receipts {
8285
receipt.CumulativeGasUsed = cumulativeGasUsed + receipt.GasUsed
8386
cumulativeGasUsed += receipt.GasUsed
84-
if receipt.CumulativeGasUsed > header.GasLimit {
87+
cumulativeBlockGasUsed += receipt.BlockGasUsed
88+
if cumulativeBlockGasUsed > header.GasLimit {
8589
return &ProcessResultWithMetrics{
8690
ProcessResult: &ProcessResult{Error: fmt.Errorf("gas limit exceeded")},
8791
}
@@ -184,7 +188,7 @@ func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateRea
184188
if res.err != nil {
185189
execErr = res.err
186190
} else {
187-
if err := gp.SubGas(res.receipt.GasUsed); err != nil {
191+
if err := gp.SubGas(res.receipt.BlockGasUsed); err != nil {
188192
execErr = err
189193
} else {
190194
receipts = append(receipts, res.receipt)

core/state_processor.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,9 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
108108
if err != nil {
109109
return nil, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
110110
}
111-
usedGas += receipt.GasUsed
111+
usedGas += receipt.BlockGasUsed
112112
receipts = append(receipts, receipt)
113113
allLogs = append(allLogs, receipt.Logs...)
114-
115-
/*
116-
enc, _ := json.MarshalIndent(receipt, "", " ")
117-
fmt.Printf("receipt json %s\n", string(enc))
118-
encRLP, _ := rlp.EncodeToBytes(receipt)
119-
fmt.Printf("receipt rlp %x\n", encRLP)
120-
*/
121114
}
122115

123116
// Read requests if Prague is enabled.
@@ -210,10 +203,11 @@ func MakeReceipt(evm *vm.EVM, result *ExecutionResult, statedb *state.StateDB, b
210203
receipt.Status = types.ReceiptStatusSuccessful
211204
}
212205
receipt.TxHash = tx.Hash()
206+
receipt.GasUsed = result.UsedGas
213207
if evm.ChainConfig().IsAmsterdam(blockNumber, blockTime) {
214-
receipt.GasUsed = result.MaxUsedGas
208+
receipt.BlockGasUsed = result.MaxUsedGas
215209
} else {
216-
receipt.GasUsed = result.UsedGas
210+
receipt.BlockGasUsed = result.UsedGas
217211
}
218212

219213
if tx.Type() == types.BlobTxType {

core/state_transition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// ExecutionResult includes all output after executing given evm
3434
// message no matter the execution itself is successful or not.
3535
type ExecutionResult struct {
36-
UsedGas uint64 // Total used gas, not including the refunded gas
36+
UsedGas uint64 // Total gas used, with refunds subtracted
3737
MaxUsedGas uint64 // Maximum gas consumed during execution, excluding gas refunds.
3838
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
3939
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)

core/types/receipt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Receipt struct {
6363
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
6464
ContractAddress common.Address `json:"contractAddress"`
6565
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
66+
BlockGasUsed uint64 `json:"blockGasUsed"`
6667
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"` // required, but tag omitted for backwards compatibility
6768
BlobGasUsed uint64 `json:"blobGasUsed,omitempty"`
6869
BlobGasPrice *big.Int `json:"blobGasPrice,omitempty"`

miner/worker.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ type environment struct {
6666
coinbase common.Address
6767
evm *vm.EVM
6868

69-
header *types.Header
69+
header *types.Header
70+
blockGasUsed uint64 // block gas used incl refunds
71+
7072
txs []*types.Transaction
7173
receipts []*types.Receipt
7274
sidecars []*types.BlobTxSidecar
@@ -382,11 +384,12 @@ func (miner *Miner) applyTransaction(env *environment, tx *types.Transaction) (*
382384
snap = env.state.Snapshot()
383385
gp = env.gasPool.Gas()
384386
)
385-
receipt, err := core.ApplyTransaction(env.evm, env.gasPool, env.state, env.header, tx, &env.header.GasUsed)
387+
receipt, err := core.ApplyTransaction(env.evm, env.gasPool, env.state, env.header, tx, &env.blockGasUsed)
386388
if err != nil {
387389
env.state.RevertToSnapshot(snap)
388390
env.gasPool.SetGas(gp)
389391
}
392+
env.header.GasUsed += receipt.BlockGasUsed
390393
return receipt, err
391394
}
392395

0 commit comments

Comments
 (0)