From 77e33e5a49be99130a02dc72d6a0e4739fdd44d6 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 7 Mar 2023 18:23:52 +0800 Subject: [PATCH] core, miner: revert block gas counter in case of invalid transaction (#26799) This change fixes a flaw where, in certain scenarios, the block sealer did not accurately reset the remaining gas after failing to include an invalid transaction. Fixes #26791 --- cmd/evm/internal/t8ntool/execution.go | 9 +++++++-- core/gaspool.go | 5 +++++ miner/worker.go | 7 +++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 2c68659945..b023dde932 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -176,8 +176,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, vmConfig.Tracer = tracer vmConfig.Debug = (tracer != nil) statedb.SetTxContext(tx.Hash(), txIndex) - txContext := core.NewEVMTxContext(msg) - snapshot := statedb.Snapshot() + + var ( + txContext = core.NewEVMTxContext(msg) + snapshot = statedb.Snapshot() + prevGas = gaspool.Gas() + ) evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig) // (ret []byte, usedGas uint64, failed bool, err error) @@ -186,6 +190,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, statedb.RevertToSnapshot(snapshot) log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err) rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) + gaspool.SetGas(prevGas) continue } includedTxs = append(includedTxs, tx) diff --git a/core/gaspool.go b/core/gaspool.go index e3795c1ee9..767222674f 100644 --- a/core/gaspool.go +++ b/core/gaspool.go @@ -49,6 +49,11 @@ func (gp *GasPool) Gas() uint64 { return uint64(*gp) } +// SetGas sets the amount of gas with the provided number. +func (gp *GasPool) SetGas(gas uint64) { + *(*uint64)(gp) = gas +} + func (gp *GasPool) String() string { return fmt.Sprintf("%d", *gp) } diff --git a/miner/worker.go b/miner/worker.go index 8940c5037b..67a5842d23 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -861,11 +861,14 @@ func (w *worker) updateSnapshot(env *environment) { } func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*types.Log, error) { - snap := env.state.Snapshot() - + var ( + snap = env.state.Snapshot() + gp = env.gasPool.Gas() + ) receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, *w.chain.GetVMConfig()) if err != nil { env.state.RevertToSnapshot(snap) + env.gasPool.SetGas(gp) return nil, err } env.txs = append(env.txs, tx)