From 95c278fc2055b94593bfffa29eacfd12e4c2dc8b Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 5 Mar 2026 18:05:05 +0100 Subject: [PATCH] core: fix mining issues --- core/gaspool.go | 10 ++++++++-- miner/worker.go | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/gaspool.go b/core/gaspool.go index 598d20daf1..3255e34835 100644 --- a/core/gaspool.go +++ b/core/gaspool.go @@ -76,12 +76,18 @@ func (gp *GasPool) ReturnGas(returned uint64, gasUsed uint64) error { // ReturnGasAmsterdam returns unused gas to the pool using two-dimensional // gas accounting (EIP-8037). receiptGasUsed is the per-tx gas the user pays // (tx.gas - gas_left - state_gas_reservoir), used for receipt cumulative tracking. +// +// The block gas increment for this tx is the change in max(sumR, sumS) after +// adding the tx's per-dimension values, NOT max(txR, txS) per-tx. This avoids +// overcounting when the dominant dimension switches between transactions. func (gp *GasPool) ReturnGasAmsterdam(txGasLimit, txRegular, txState, receiptGasUsed uint64) error { - txBlockGas := max(txRegular, txState) + oldUsed := max(gp.cumulativeRegular, gp.cumulativeState) gp.cumulativeRegular += txRegular gp.cumulativeState += txState gp.cumulativeUsed += receiptGasUsed - returned := txGasLimit - txBlockGas + newUsed := max(gp.cumulativeRegular, gp.cumulativeState) + blockGasIncrement := newUsed - oldUsed + returned := txGasLimit - blockGasIncrement if gp.remaining > math.MaxUint64-returned { return fmt.Errorf("%w: remaining: %d, returned: %d", ErrGasLimitOverflow, gp.remaining, returned) } diff --git a/miner/worker.go b/miner/worker.go index 3e53353071..165c42ec70 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -223,6 +223,10 @@ func (miner *Miner) generateWork(genParam *generateParams, witness bool) *newPay } } + // EIP-8037: set header.GasUsed before FinalizeAndAssemble so the block + // header reflects the correct 2D gas metric max(sum_regular, sum_state). + work.header.GasUsed = work.gasPool.Used() + block, err := miner.engine.FinalizeAndAssemble(miner.chain, work.header, work.state, &body, work.receipts, onBlockFinalization) if err != nil { return &newPayloadResult{err: err} @@ -329,7 +333,9 @@ func (miner *Miner) prepareWork(genParams *generateParams, witness bool) (*envir if miner.chainConfig.IsPrague(header.Number, header.Time) { mut.Merge(core.ProcessParentBlockHash(header.ParentHash, env.evm)) } - env.accessList.AccumulateMutations(mut, 0) + if env.accessList != nil { + env.accessList.AccumulateMutations(mut, 0) + } return env, nil }