From 96296fb42e08da4f0db1c836efb9c427740c92e4 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 29 Sep 2024 12:07:58 +0800 Subject: [PATCH] fix(taiko_api): fix an `EstimatedGasUsed` calculation issue (#322) * correct EstimatedGasUsed if lastTransaction is not null * correct EstimatedGasUsed if lastTransaction is not null * fix import order * Update core/state/statedb.go --------- Co-authored-by: David --- core/state/statedb.go | 5 +++++ miner/taiko_worker.go | 52 +++++++++++++------------------------------ 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index ebd2143882ac..b59169e58ba6 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -786,6 +786,11 @@ func (s *StateDB) Copy() *StateDB { return state } +// CHANGE(taiko): RevisionId returns the latest snapshot id. +func (s *StateDB) RevisionId() int { + return s.nextRevisionId +} + // Snapshot returns an identifier for the current revision of the state. func (s *StateDB) Snapshot() int { id := s.nextRevisionId diff --git a/miner/taiko_worker.go b/miner/taiko_worker.go index 5d539d7f313a..031a97ab5812 100644 --- a/miner/taiko_worker.go +++ b/miner/taiko_worker.go @@ -18,6 +18,7 @@ import ( "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 @@ -71,51 +72,35 @@ func (w *worker) BuildTransactionsLists( localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee) ) - commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) { + commitTxs := func() (*PreBuiltTxList, error) { env.tcount = 0 env.txs = []*types.Transaction{} env.gasPool = new(core.GasPool).AddGas(blockMaxGasLimit) env.header.GasLimit = blockMaxGasLimit - var ( - locals = make(map[common.Address][]*txpool.LazyTransaction) - remotes = make(map[common.Address][]*txpool.LazyTransaction) - ) - - for address, txs := range localTxs { - locals[address] = txs - } - for address, txs := range remoteTxs { - remotes[address] = txs - } - - lastTransaction := w.commitL2Transactions( + w.commitL2Transactions( env, - firstTransaction, - newTransactionsByPriceAndNonce(signer, locals, baseFee), - newTransactionsByPriceAndNonce(signer, remotes, baseFee), + newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee), + newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee), maxBytesPerTxList, minTip, ) b, err := encodeAndCompressTxList(env.txs) if err != nil { - return nil, nil, err + return nil, err } - return lastTransaction, &PreBuiltTxList{ + return &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++ { - if lastTx, res, err = commitTxs(lastTx); err != nil { + res, err := commitTxs() + if err != nil { return nil, err } @@ -238,22 +223,16 @@ func (w *worker) getPendingTxs(localAccounts []string, baseFee *big.Int) ( // commitL2Transactions tries to commit the transactions into the given state. func (w *worker) commitL2Transactions( env *environment, - firstTransaction *types.Transaction, txsLocal *transactionsByPriceAndNonce, txsRemote *transactionsByPriceAndNonce, maxBytesPerTxList uint64, minTip uint64, -) *types.Transaction { +) { var ( - txs = txsLocal - isLocal = true - lastTransaction *types.Transaction + txs = txsLocal + isLocal = true ) - 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. @@ -301,6 +280,8 @@ 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): @@ -321,8 +302,9 @@ 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 } default: @@ -332,8 +314,6 @@ loop: txs.Pop() } } - - return lastTransaction } // encodeAndCompressTxList encodes and compresses the given transactions list.