Skip to content

Commit

Permalink
fix: using snapshot instead of state copy (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
irrun authored Dec 26, 2024
1 parent ad4b66d commit 462129f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,15 @@ func (w *worker) simulateBundle(
ethSentToSystem = new(big.Int)
)

currentState := state.Copy()
txsLen := len(bundle.Txs)
for i := 0; i < txsLen; i++ {
tx := bundle.Txs[i]

for i, tx := range bundle.Txs {
state.SetTxContext(tx.Hash(), i+currentTxCount)
sysBalanceBefore := state.GetBalance(consensus.SystemAddress)

prevState := currentState.Copy()
prevGasPool := new(core.GasPool).AddGas(gasPool.Gas())
snap := state.Snapshot()
gp := gasPool.Gas()

receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &w.coinbase, gasPool, state, env.header, tx,
&tempGasUsed, *w.chain.GetVMConfig())
Expand All @@ -429,9 +430,11 @@ func (w *worker) simulateBundle(

if containsHash(bundle.DroppingTxHashes, tx.Hash()) {
log.Warn("drop tx in bundle", "hash", tx.Hash().String())
state = prevState
gasPool = prevGasPool
state.RevertToSnapshot(snap)
gasPool.SetGas(gp)
bundle.Txs = bundle.Txs.Remove(i)
txsLen = len(bundle.Txs)
i--
continue
}

Expand All @@ -451,9 +454,10 @@ func (w *worker) simulateBundle(
// for unRevertible tx but itself can be dropped, we drop it and revert the state and gas pool
if containsHash(bundle.DroppingTxHashes, receipt.TxHash) {
log.Warn("drop tx in bundle", "hash", receipt.TxHash.String())
state = prevState
gasPool = prevGasPool
// do not need to revert the state and gas pool for they are already reverted in ApplyTransaction
bundle.Txs = bundle.Txs.Remove(i)
txsLen = len(bundle.Txs)
i--
continue
}

Expand Down

0 comments on commit 462129f

Please sign in to comment.