Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: using snapshot insead of state copy #63

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 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,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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more information see bnb-chain/op-geth#240 (comment)

gasPool.SetGas(gp)
bundle.Txs = bundle.Txs.Remove(i)
txsLen = len(bundle.Txs)
i--
continue
}

Expand Down
Loading