core: fix mining issues

This commit is contained in:
Marius van der Wijden 2026-03-05 18:05:05 +01:00
parent 964a117ea7
commit 95c278fc20
2 changed files with 15 additions and 3 deletions

View file

@ -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)
}

View file

@ -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
}