feat(miner): reduce the number compression attempts when fetching transactions list (#406)

* feat(miner): reduce the number compression attempts when fetching transactions list

* chore: update ci

* chore: update ci

* chore: update ci

* chore: update ci

* Update miner/taiko_worker.go

Co-authored-by: maskpp <maskpp266@gmail.com>

* chore: update ci

* feat: rename

* feat: rename

* feat: one more optimization

* feat: update TODO && checks

* feat: introduce `txsPruningResult.GasPruned`

* fix: fix an issue

* feat: more improvements

* feat: update TODO && checks

* feat: rename

* Update miner/taiko_worker.go

Co-authored-by: maskpp <maskpp266@gmail.com>

---------

Co-authored-by: maskpp <maskpp266@gmail.com>
This commit is contained in:
David 2025-03-29 12:11:52 +08:00 committed by GitHub
parent c98487d175
commit 9e6edc51db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,6 +22,11 @@ import (
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
const (
TxListCompressionCheckInterval = 100
TxListCompressionPruneStep = 10
)
// BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions // BuildTransactionsLists builds multiple transactions lists which satisfy all the given conditions
// 1. All transactions should all be able to pay the given base fee. // 1. All transactions should all be able to pay the given base fee.
// 2. The total gas used should not exceed the given blockMaxGasLimit // 2. The total gas used should not exceed the given blockMaxGasLimit
@ -46,7 +51,13 @@ func (w *Miner) buildTransactionsLists(
} }
// Check if tx pool is empty at first. // Check if tx pool is empty at first.
if len(w.txpool.Pending(txpool.PendingFilter{MinTip: uint256.NewInt(minTip), BaseFee: uint256.MustFromBig(baseFee), OnlyPlainTxs: true})) == 0 { if len(w.txpool.Pending(
txpool.PendingFilter{
MinTip: uint256.NewInt(minTip),
BaseFee: uint256.MustFromBig(baseFee),
OnlyPlainTxs: true,
},
)) == 0 {
return txsLists, nil return txsLists, nil
} }
@ -72,47 +83,46 @@ func (w *Miner) buildTransactionsLists(
localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee) localTxs, remoteTxs = w.getPendingTxs(localAccounts, baseFee)
) )
commitTxs := func(firstTransaction *types.Transaction) (*types.Transaction, *PreBuiltTxList, error) { commitTxs := func(pruningResult *txsPruningResult) (*txsPruningResult, *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 - accumulateGasUsed(pruningResult.ReceiptsPruned))
env.header.GasLimit = blockMaxGasLimit env.header.GasLimit = blockMaxGasLimit
lastTransaction := w.commitL2Transactions( result, err := w.commitL2Transactions(
env, env,
firstTransaction, pruningResult.TxsPruned,
pruningResult.ReceiptsPruned,
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,
minTip, minTip,
) )
b, err := encodeAndCompressTxList(env.txs)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
return lastTransaction, &PreBuiltTxList{ return result, &PreBuiltTxList{
TxList: env.txs, TxList: result.TxsRemaining,
EstimatedGasUsed: env.header.GasLimit - env.gasPool.Gas(), EstimatedGasUsed: accumulateGasUsed(result.ReceiptsRemaining),
BytesLength: uint64(len(b)), BytesLength: uint64(result.Size),
}, nil }, nil
} }
var ( var (
lastTx *types.Transaction pruningResult = new(txsPruningResult)
res *PreBuiltTxList preBuiltTxList *PreBuiltTxList
) )
for i := 0; i < int(maxTransactionsLists); i++ { for range int(maxTransactionsLists) {
if lastTx, res, err = commitTxs(lastTx); err != nil { if pruningResult, preBuiltTxList, err = commitTxs(pruningResult); err != nil {
return nil, err return nil, err
} }
if len(res.TxList) == 0 { if len(preBuiltTxList.TxList) == 0 {
break break
} }
txsLists = append(txsLists, res) txsLists = append(txsLists, preBuiltTxList)
} }
return txsLists, nil return txsLists, nil
@ -133,7 +143,7 @@ func (w *Miner) sealBlockWith(
} }
if len(txs) == 0 { if len(txs) == 0 {
// A L2 block needs to have have at least one `TaikoL2.anchor` / `TaikoL2.anchorV2`. // A L2 block needs to have have at least one `TaikoL2.anchor` / `TaikoL2.anchorV2` / `TaikoL2.anchorV3`.
return nil, fmt.Errorf("too less transactions in the block") return nil, fmt.Errorf("too less transactions in the block")
} }
@ -232,20 +242,24 @@ 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, presetTxs []*types.Transaction,
presetReceipts []*types.Receipt,
txsLocal *transactionsByPriceAndNonce, txsLocal *transactionsByPriceAndNonce,
txsRemote *transactionsByPriceAndNonce, txsRemote *transactionsByPriceAndNonce,
maxBytesPerTxList uint64, maxBytesPerTxList uint64,
minTip uint64, minTip uint64,
) *types.Transaction { ) (*txsPruningResult, error) {
var ( var (
txs = txsLocal txs = txsLocal
isLocal = true isLocal = true
lastTransaction *types.Transaction pruningResult *txsPruningResult
err error
) )
if firstTransaction != nil { if presetTxs != nil {
env.txs = append(env.txs, firstTransaction) env.tcount = len(presetTxs)
env.txs = append(env.txs, presetTxs...)
env.receipts = append(env.receipts, presetReceipts...)
} }
loop: loop:
@ -306,23 +320,13 @@ loop:
// Everything ok, collect the logs and shift in the next transaction from the same account // Everything ok, collect the logs and shift in the next transaction from the same account
txs.Shift() txs.Shift()
data, err := rlp.EncodeToBytes(env.txs) // Check the size of the compressed txList, if it exceeds the maxBytesPerTxList, break the loop.
if err != nil { if env.tcount%TxListCompressionCheckInterval == 0 {
log.Trace("Failed to rlp encode the pending transaction %s: %w", tx.Hash(), err) if pruningResult, err = pruneTransactions(env.txs, env.receipts, maxBytesPerTxList); err != nil {
txs.Pop() return nil, err
continue
}
if len(data) >= int(maxBytesPerTxList) {
// Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break.
b, err := compress(data)
if err != nil {
log.Trace("Failed to rlp encode and compress the pending transaction %s: %w", tx.Hash(), err)
txs.Pop()
continue
} }
if len(b) > int(maxBytesPerTxList) { // If there are pruned transactions, break the loop.
lastTransaction = env.txs[env.tcount-1] if len(pruningResult.TxsPruned) > 0 {
env.txs = env.txs[0 : env.tcount-1]
break loop break loop
} }
} }
@ -335,7 +339,11 @@ loop:
} }
} }
return lastTransaction if pruningResult, err = pruneTransactions(env.txs, env.receipts, maxBytesPerTxList); err != nil {
return nil, err
}
return pruningResult, nil
} }
// encodeAndCompressTxList encodes and compresses the given transactions list. // encodeAndCompressTxList encodes and compresses the given transactions list.
@ -364,3 +372,68 @@ func compress(txListBytes []byte) ([]byte, error) {
return b.Bytes(), nil return b.Bytes(), nil
} }
// txsPruningResult represents the result of a transactions list pruning.
type txsPruningResult struct {
TxsPruned []*types.Transaction
ReceiptsPruned []*types.Receipt
TxsRemaining []*types.Transaction
ReceiptsRemaining []*types.Receipt
Size int
}
// pruneTransactions prunes the transactions from the given environment to fit the size limit.
func pruneTransactions(
txs []*types.Transaction,
receipts []*types.Receipt,
sizeLimit uint64,
) (*txsPruningResult, error) {
var (
prunedTxs []*types.Transaction
prunedReceipts []*types.Receipt
)
for len(txs) > 0 {
b, err := encodeAndCompressTxList(txs)
if err != nil {
return nil, err
}
if len(b) <= int(sizeLimit) {
return &txsPruningResult{
TxsPruned: prunedTxs,
ReceiptsPruned: prunedReceipts,
TxsRemaining: txs,
Size: len(b),
}, nil
}
if len(txs) < TxListCompressionPruneStep {
prunedTxs = append(txs, prunedTxs...)
prunedReceipts = append(receipts, prunedReceipts...)
txs = []*types.Transaction{}
receipts = []*types.Receipt{}
break
}
prunedTxs = append(txs[len(txs)-TxListCompressionPruneStep:], prunedTxs...)
prunedReceipts = append(receipts[len(receipts)-TxListCompressionPruneStep:], prunedReceipts...)
txs = txs[:len(txs)-TxListCompressionPruneStep]
receipts = receipts[:len(receipts)-TxListCompressionPruneStep]
}
// All transactions are pruned.
return &txsPruningResult{
TxsPruned: prunedTxs,
ReceiptsPruned: prunedReceipts,
TxsRemaining: txs,
ReceiptsRemaining: receipts,
Size: 0,
}, nil
}
// accumulateGasUsed sums up the gas used from the receipts.
func accumulateGasUsed(receipts []*types.Receipt) uint64 {
var gasUsed uint64
for _, receipt := range receipts {
gasUsed += receipt.GasUsed
}
return gasUsed
}