fix(taiko-geth): stop using RevertToSnapshot when fetching mempool (#336)

This commit is contained in:
maskpp 2024-10-22 19:56:59 +08:00 committed by GitHub
parent 46576d2720
commit 1216d8d605
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 24 deletions

View file

@ -702,11 +702,6 @@ func (s *StateDB) Copy() *StateDB {
return state 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. // Snapshot returns an identifier for the current revision of the state.
func (s *StateDB) Snapshot() int { func (s *StateDB) Snapshot() int {
return s.journal.snapshot() return s.journal.snapshot()

View file

@ -5,6 +5,7 @@ import (
"compress/zlib" "compress/zlib"
"errors" "errors"
"fmt" "fmt"
"maps"
"math/big" "math/big"
"time" "time"
@ -17,8 +18,8 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"golang.org/x/exp/maps"
) )
// BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions // BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions
@ -71,14 +72,15 @@ func (w *Miner) buildTransactionsLists(
localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee) localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee)
) )
commitTxs := func() (*PreBuiltTxList, error) { commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) {
env.tcount = 0 env.tcount = 0
env.txs = []*types.Transaction{} env.txs = []*types.Transaction{}
env.gasPool = new(core.GasPool).AddGas(blockMaxGasLimit) env.gasPool = new(core.GasPool).AddGas(blockMaxGasLimit)
env.header.GasLimit = blockMaxGasLimit env.header.GasLimit = blockMaxGasLimit
w.commitL2Transactions( lastTransaction := w.commitL2Transactions(
env, env,
firstTransaction,
newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee), newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee),
newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee), newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee),
maxBytesPerTxList, maxBytesPerTxList,
@ -87,19 +89,22 @@ func (w *Miner) buildTransactionsLists(
b, err := encodeAndCompressTxList(env.txs) b, err := encodeAndCompressTxList(env.txs)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
return &PreBuiltTxList{ return lastTransaction, &PreBuiltTxList{
TxList: env.txs, TxList: env.txs,
EstimatedGasUsed: env.header.GasLimit - env.gasPool.Gas(), EstimatedGasUsed: env.header.GasLimit - env.gasPool.Gas(),
BytesLength: uint64(len(b)), BytesLength: uint64(len(b)),
}, nil }, nil
} }
var (
lastTx *types.Transaction
res *PreBuiltTxList
)
for i := 0; i < int(maxTransactionsLists); i++ { for i := 0; i < int(maxTransactionsLists); i++ {
res, err := commitTxs() if lastTx, res, err = commitTxs(lastTx); err != nil {
if err != nil {
return nil, err return nil, err
} }
@ -227,16 +232,22 @@ func (w *Miner) getPendingTxs(localAccounts []string, baseFee *big.Int) (
// commitL2Transactions tries to commit the transactions into the given state. // commitL2Transactions tries to commit the transactions into the given state.
func (w *Miner) commitL2Transactions( func (w *Miner) commitL2Transactions(
env *environment, env *environment,
firstTransaction *types.Transaction,
txsLocal *transactionsByPriceAndNonce, txsLocal *transactionsByPriceAndNonce,
txsRemote *transactionsByPriceAndNonce, txsRemote *transactionsByPriceAndNonce,
maxBytesPerTxList uint64, maxBytesPerTxList uint64,
minTip uint64, minTip uint64,
) { ) *types.Transaction {
var ( var (
txs = txsLocal txs = txsLocal
isLocal = true isLocal = true
lastTransaction *types.Transaction
) )
if firstTransaction != nil {
env.txs = append(env.txs, firstTransaction)
}
loop: loop:
for { for {
// If we don't have enough gas for any further transactions then we're done. // If we don't have enough gas for any further transactions then we're done.
@ -284,8 +295,6 @@ loop:
// Start executing the transaction // Start executing the transaction
env.state.SetTxContext(tx.Hash(), env.tcount) env.state.SetTxContext(tx.Hash(), env.tcount)
snap := env.state.RevisionId()
gasPool := env.gasPool.Gas()
err := w.commitTransaction(env, tx) err := w.commitTransaction(env, tx)
switch { switch {
case errors.Is(err, core.ErrNonceTooLow): case errors.Is(err, core.ErrNonceTooLow):
@ -303,7 +312,6 @@ loop:
txs.Pop() txs.Pop()
continue continue
} }
if len(data) >= int(maxBytesPerTxList) { if len(data) >= int(maxBytesPerTxList) {
// Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break. // Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break.
b, err := compress(data) b, err := compress(data)
@ -313,15 +321,12 @@ loop:
continue continue
} }
if len(b) > int(maxBytesPerTxList) { if len(b) > int(maxBytesPerTxList) {
lastTransaction = env.txs[env.tcount-1]
env.txs = env.txs[0 : env.tcount-1] env.txs = env.txs[0 : env.tcount-1]
env.state.RevertToSnapshot(snap)
env.gasPool.SetGas(gasPool)
break loop break loop
} }
} }
env.tcount++
default: default:
// Transaction is regarded as invalid, drop all consecutive transactions from // Transaction is regarded as invalid, drop all consecutive transactions from
// the same sender because of `nonce-too-high` clause. // the same sender because of `nonce-too-high` clause.
@ -329,6 +334,8 @@ loop:
txs.Pop() txs.Pop()
} }
} }
return lastTransaction
} }
// encodeAndCompressTxList encodes and compresses the given transactions list. // encodeAndCompressTxList encodes and compresses the given transactions list.

View file

@ -54,14 +54,14 @@ func testGenerateWorker(t *testing.T, txCount int) *Miner {
} }
func TestBuildTransactionsLists(t *testing.T) { func TestBuildTransactionsLists(t *testing.T) {
w := testGenerateWorker(t, 1000) w := testGenerateWorker(t, 2000)
maxBytesPerTxList := (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob maxBytesPerTxList := (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob
txList, err := w.BuildTransactionsLists( txList, err := w.BuildTransactionsLists(
testBankAddress, testBankAddress,
nil, nil,
240_000_000, 240_000_000,
uint64(maxBytesPerTxList), uint64(maxBytesPerTxList)/10,
nil, nil,
1, 1,
) )