Skip to content

Commit

Permalink
core, miner: revert block gas counter in case of invalid transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Mar 6, 2023
1 parent 27e5982 commit 1ac3c16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/gaspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 5 additions & 2 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1ac3c16

Please sign in to comment.