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(taiko_api): correct EstimatedGasUsed if lastTransaction is not null #322

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 16 additions & 36 deletions miner/taiko_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -332,8 +314,6 @@ loop:
txs.Pop()
}
}

return lastTransaction
}

// encodeAndCompressTxList encodes and compresses the given transactions list.
Expand Down
Loading