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 <david@taiko.xyz>
This commit is contained in:
maskpp 2024-09-29 12:07:58 +08:00 committed by GitHub
parent 4e4c2a4b65
commit 96296fb42e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 36 deletions

View file

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

View file

@ -18,6 +18,7 @@ import (
"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,51 +72,35 @@ func (w *worker) BuildTransactionsLists(
localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee) localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee)
) )
commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) { commitTxs := func() (*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
var ( w.commitL2Transactions(
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(
env, env,
firstTransaction, newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee),
newTransactionsByPriceAndNonce(signer, locals, baseFee), newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee),
newTransactionsByPriceAndNonce(signer, remotes, baseFee),
maxBytesPerTxList, maxBytesPerTxList,
minTip, minTip,
) )
b, err := encodeAndCompressTxList(env.txs) b, err := encodeAndCompressTxList(env.txs)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
return lastTransaction, &PreBuiltTxList{ return &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++ {
if lastTx, res, err = commitTxs(lastTx); err != nil { res, err := commitTxs()
if err != nil {
return nil, err 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. // commitL2Transactions tries to commit the transactions into the given state.
func (w *worker) commitL2Transactions( func (w *worker) 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.
@ -301,6 +280,8 @@ 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):
@ -321,8 +302,9 @@ 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
} }
default: default:
@ -332,8 +314,6 @@ loop:
txs.Pop() txs.Pop()
} }
} }
return lastTransaction
} }
// encodeAndCompressTxList encodes and compresses the given transactions list. // encodeAndCompressTxList encodes and compresses the given transactions list.