Skip to content

Commit 96296fb

Browse files
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 <[email protected]>
1 parent 4e4c2a4 commit 96296fb

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

core/state/statedb.go

+5
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ func (s *StateDB) Copy() *StateDB {
786786
return state
787787
}
788788

789+
// CHANGE(taiko): RevisionId returns the latest snapshot id.
790+
func (s *StateDB) RevisionId() int {
791+
return s.nextRevisionId
792+
}
793+
789794
// Snapshot returns an identifier for the current revision of the state.
790795
func (s *StateDB) Snapshot() int {
791796
id := s.nextRevisionId

miner/taiko_worker.go

+16-36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/ethereum/go-ethereum/params"
1919
"github.com/ethereum/go-ethereum/rlp"
2020
"github.com/holiman/uint256"
21+
"golang.org/x/exp/maps"
2122
)
2223

2324
// BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions
@@ -71,51 +72,35 @@ func (w *worker) BuildTransactionsLists(
7172
localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee)
7273
)
7374

74-
commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) {
75+
commitTxs := func() (*PreBuiltTxList, error) {
7576
env.tcount = 0
7677
env.txs = []*types.Transaction{}
7778
env.gasPool = new(core.GasPool).AddGas(blockMaxGasLimit)
7879
env.header.GasLimit = blockMaxGasLimit
7980

80-
var (
81-
locals = make(map[common.Address][]*txpool.LazyTransaction)
82-
remotes = make(map[common.Address][]*txpool.LazyTransaction)
83-
)
84-
85-
for address, txs := range localTxs {
86-
locals[address] = txs
87-
}
88-
for address, txs := range remoteTxs {
89-
remotes[address] = txs
90-
}
91-
92-
lastTransaction := w.commitL2Transactions(
81+
w.commitL2Transactions(
9382
env,
94-
firstTransaction,
95-
newTransactionsByPriceAndNonce(signer, locals, baseFee),
96-
newTransactionsByPriceAndNonce(signer, remotes, baseFee),
83+
newTransactionsByPriceAndNonce(signer, maps.Clone(localTxs), baseFee),
84+
newTransactionsByPriceAndNonce(signer, maps.Clone(remoteTxs), baseFee),
9785
maxBytesPerTxList,
9886
minTip,
9987
)
10088

10189
b, err := encodeAndCompressTxList(env.txs)
10290
if err != nil {
103-
return nil, nil, err
91+
return nil, err
10492
}
10593

106-
return lastTransaction, &PreBuiltTxList{
94+
return &PreBuiltTxList{
10795
TxList: env.txs,
10896
EstimatedGasUsed: env.header.GasLimit - env.gasPool.Gas(),
10997
BytesLength: uint64(len(b)),
11098
}, nil
11199
}
112100

113-
var (
114-
lastTx *types.Transaction
115-
res *PreBuiltTxList
116-
)
117101
for i := 0; i < int(maxTransactionsLists); i++ {
118-
if lastTx, res, err = commitTxs(lastTx); err != nil {
102+
res, err := commitTxs()
103+
if err != nil {
119104
return nil, err
120105
}
121106

@@ -238,22 +223,16 @@ func (w *worker) getPendingTxs(localAccounts []string, baseFee *big.Int) (
238223
// commitL2Transactions tries to commit the transactions into the given state.
239224
func (w *worker) commitL2Transactions(
240225
env *environment,
241-
firstTransaction *types.Transaction,
242226
txsLocal *transactionsByPriceAndNonce,
243227
txsRemote *transactionsByPriceAndNonce,
244228
maxBytesPerTxList uint64,
245229
minTip uint64,
246-
) *types.Transaction {
230+
) {
247231
var (
248-
txs = txsLocal
249-
isLocal = true
250-
lastTransaction *types.Transaction
232+
txs = txsLocal
233+
isLocal = true
251234
)
252235

253-
if firstTransaction != nil {
254-
env.txs = append(env.txs, firstTransaction)
255-
}
256-
257236
loop:
258237
for {
259238
// If we don't have enough gas for any further transactions then we're done.
@@ -301,6 +280,8 @@ loop:
301280
// Start executing the transaction
302281
env.state.SetTxContext(tx.Hash(), env.tcount)
303282

283+
snap := env.state.RevisionId()
284+
gasPool := env.gasPool.Gas()
304285
_, err := w.commitTransaction(env, tx)
305286
switch {
306287
case errors.Is(err, core.ErrNonceTooLow):
@@ -321,8 +302,9 @@ loop:
321302
continue
322303
}
323304
if len(b) > int(maxBytesPerTxList) {
324-
lastTransaction = env.txs[env.tcount-1]
325305
env.txs = env.txs[0 : env.tcount-1]
306+
env.state.RevertToSnapshot(snap)
307+
env.gasPool.SetGas(gasPool)
326308
break loop
327309
}
328310
default:
@@ -332,8 +314,6 @@ loop:
332314
txs.Pop()
333315
}
334316
}
335-
336-
return lastTransaction
337317
}
338318

339319
// encodeAndCompressTxList encodes and compresses the given transactions list.

0 commit comments

Comments
 (0)