Skip to content

Commit

Permalink
fix(taiko-geth): stop using RevertToSnapshot when fetching mempool (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored and lwedge99 committed Nov 8, 2024
1 parent 49b8dd2 commit d91e0c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
5 changes: 0 additions & 5 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,6 @@ func (s *StateDB) Copy() *StateDB {
return state
}

// CHANGE(taiko): RevisionId returns the latest snapshot id.
func (s *StateDB) RevisionId() int {
return s.journal.nextRevisionId
}

// Snapshot returns an identifier for the current revision of the state.
func (s *StateDB) Snapshot() int {
return s.journal.snapshot()
Expand Down
41 changes: 24 additions & 17 deletions miner/taiko_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/zlib"
"errors"
"fmt"
"maps"
"math/big"
"time"

Expand All @@ -17,8 +18,8 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"

"github.com/holiman/uint256"
"golang.org/x/exp/maps"
)

// BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions
Expand Down Expand Up @@ -71,14 +72,15 @@ func (w *Miner) buildTransactionsLists(
localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee)
)

commitTxs := func() (*PreBuiltTxList, error) {
commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) {
env.tcount = 0
env.txs = []*types.Transaction{}
env.gasPool = new(core.GasPool).AddGas(blockMaxGasLimit)
env.header.GasLimit = blockMaxGasLimit

w.commitL2Transactions(
lastTransaction := w.commitL2Transactions(
env,
firstTransaction,
newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee),
newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee),
maxBytesPerTxList,
Expand All @@ -87,19 +89,22 @@ func (w *Miner) buildTransactionsLists(

b, err := encodeAndCompressTxList(env.txs)
if err != nil {
return nil, err
return nil, nil, err
}

return &PreBuiltTxList{
return lastTransaction, &PreBuiltTxList{
TxList: env.txs,
EstimatedGasUsed: env.header.GasLimit - env.gasPool.Gas(),
BytesLength: uint64(len(b)),
}, nil
}

var (
lastTx *types.Transaction
res *PreBuiltTxList
)
for i := 0; i < int(maxTransactionsLists); i++ {
res, err := commitTxs()
if err != nil {
if lastTx, res, err = commitTxs(lastTx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -227,16 +232,22 @@ func (w *Miner) getPendingTxs(localAccounts []string, baseFee *big.Int) (
// commitL2Transactions tries to commit the transactions into the given state.
func (w *Miner) commitL2Transactions(
env *environment,
firstTransaction *types.Transaction,
txsLocal *transactionsByPriceAndNonce,
txsRemote *transactionsByPriceAndNonce,
maxBytesPerTxList uint64,
minTip uint64,
) {
) *types.Transaction {
var (
txs = txsLocal
isLocal = true
txs = txsLocal
isLocal = true
lastTransaction *types.Transaction
)

if firstTransaction != nil {
env.txs = append(env.txs, firstTransaction)
}

loop:
for {
// If we don't have enough gas for any further transactions then we're done.
Expand Down Expand Up @@ -284,8 +295,6 @@ loop:
// Start executing the transaction
env.state.SetTxContext(tx.Hash(), env.tcount)

snap := env.state.RevisionId()
gasPool := env.gasPool.Gas()
err := w.commitTransaction(env, tx)
switch {
case errors.Is(err, core.ErrNonceTooLow):
Expand All @@ -303,7 +312,6 @@ loop:
txs.Pop()
continue
}

if len(data) >= int(maxBytesPerTxList) {
// Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break.
b, err := compress(data)
Expand All @@ -313,22 +321,21 @@ loop:
continue
}
if len(b) > int(maxBytesPerTxList) {
lastTransaction = env.txs[env.tcount-1]
env.txs = env.txs[0 : env.tcount-1]
env.state.RevertToSnapshot(snap)
env.gasPool.SetGas(gasPool)
break loop
}
}

env.tcount++

default:
// Transaction is regarded as invalid, drop all consecutive transactions from
// the same sender because of `nonce-too-high` clause.
log.Trace("Transaction failed, account skipped", "hash", ltx.Hash, "err", err)
txs.Pop()
}
}

return lastTransaction
}

// encodeAndCompressTxList encodes and compresses the given transactions list.
Expand Down
4 changes: 2 additions & 2 deletions miner/taiko_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func testGenerateWorker(t *testing.T, txCount int) *Miner {
}

func TestBuildTransactionsLists(t *testing.T) {
w := testGenerateWorker(t, 1000)
w := testGenerateWorker(t, 2000)

maxBytesPerTxList := (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob
txList, err := w.BuildTransactionsLists(
testBankAddress,
nil,
240_000_000,
uint64(maxBytesPerTxList),
uint64(maxBytesPerTxList)/10,
nil,
1,
)
Expand Down

0 comments on commit d91e0c9

Please sign in to comment.