Skip to content

Commit 77e33e5

Browse files
authored
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
1 parent 4688d3c commit 77e33e5

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
176176
vmConfig.Tracer = tracer
177177
vmConfig.Debug = (tracer != nil)
178178
statedb.SetTxContext(tx.Hash(), txIndex)
179-
txContext := core.NewEVMTxContext(msg)
180-
snapshot := statedb.Snapshot()
179+
180+
var (
181+
txContext = core.NewEVMTxContext(msg)
182+
snapshot = statedb.Snapshot()
183+
prevGas = gaspool.Gas()
184+
)
181185
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
182186

183187
// (ret []byte, usedGas uint64, failed bool, err error)
@@ -186,6 +190,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
186190
statedb.RevertToSnapshot(snapshot)
187191
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err)
188192
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
193+
gaspool.SetGas(prevGas)
189194
continue
190195
}
191196
includedTxs = append(includedTxs, tx)

core/gaspool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func (gp *GasPool) Gas() uint64 {
4949
return uint64(*gp)
5050
}
5151

52+
// SetGas sets the amount of gas with the provided number.
53+
func (gp *GasPool) SetGas(gas uint64) {
54+
*(*uint64)(gp) = gas
55+
}
56+
5257
func (gp *GasPool) String() string {
5358
return fmt.Sprintf("%d", *gp)
5459
}

miner/worker.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,14 @@ func (w *worker) updateSnapshot(env *environment) {
861861
}
862862

863863
func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*types.Log, error) {
864-
snap := env.state.Snapshot()
865-
864+
var (
865+
snap = env.state.Snapshot()
866+
gp = env.gasPool.Gas()
867+
)
866868
receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, env.gasPool, env.state, env.header, tx, &env.header.GasUsed, *w.chain.GetVMConfig())
867869
if err != nil {
868870
env.state.RevertToSnapshot(snap)
871+
env.gasPool.SetGas(gp)
869872
return nil, err
870873
}
871874
env.txs = append(env.txs, tx)

0 commit comments

Comments
 (0)