From 9fca860a03dc7512adb0dde0b9a19d6720cf4837 Mon Sep 17 00:00:00 2001 From: irrun Date: Fri, 27 Dec 2024 11:28:50 +0800 Subject: [PATCH] fix: using snapshot insead of state copy (#63) --- miner/worker_builder.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/miner/worker_builder.go b/miner/worker_builder.go index 8a152f8d2c..abd562f67b 100644 --- a/miner/worker_builder.go +++ b/miner/worker_builder.go @@ -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()) @@ -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 } @@ -451,9 +454,12 @@ 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 + // NOTE: here should not revert state, when no err returned by ApplyTransaction, state.clearJournalAndRefund() + // must had been called to avoid reverting across transactions, so we can directly remove the tx from bundle + gasPool.SetGas(gp) bundle.Txs = bundle.Txs.Remove(i) + txsLen = len(bundle.Txs) + i-- continue }