Skip to content

Commit

Permalink
Txpool optimization: filter out staled transactions of "nonce too low…
Browse files Browse the repository at this point in the history
…" when providing all pending list to miner (bnb-chain#244)

Co-authored-by: Owen <[email protected]>
Co-authored-by: andyzhang2023 <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent 9c779d5 commit cb54455
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
29 changes: 29 additions & 0 deletions core/txpool/legacypool/legacypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,44 @@ func (pool *LegacyPool) Pending(filter txpool.PendingFilter) map[common.Address]
var (
minTipBig *big.Int
baseFeeBig *big.Int

blockNumber uint64 = 0
blockHash common.Hash = common.Hash{}
nonceTooLowCount = 0
staled = make(map[common.Hash]struct{})
)
defer func() {
log.Debug("perf-trace txpool-trace Pending() nonce too low", "blockNumber", blockNumber, "blockHash", blockHash, "nonceTooLowCount", nonceTooLowCount, "staled", len(staled))
}()
if filter.MinTip != nil {
minTipBig = filter.MinTip.ToBig()
}
if filter.BaseFee != nil {
baseFeeBig = filter.BaseFee.ToBig()
}
pending := make(map[common.Address][]*txpool.LazyTransaction, len(pool.pending))
if currHeader := pool.chain.CurrentBlock(); currHeader != nil {
blockNumber = currHeader.Number.Uint64()
blockHash = currHeader.Hash()
currBlock := pool.chain.GetBlock(blockHash, currHeader.Number.Uint64())
staled = make(map[common.Hash]struct{}, len(currBlock.Transactions()))
for _, tx := range currBlock.Transactions() {
staled[tx.Hash()] = struct{}{}
}
}
for addr, txs := range pool.pendingCache.dump() {
// remove nonce too low transactions
if len(staled) > 0 {
noncetoolow := -1
for i, tx := range txs {
if _, hit := staled[tx.Hash()]; !hit {
break
}
noncetoolow = i
}
nonceTooLowCount += noncetoolow + 1
txs = txs[noncetoolow+1:]
}

// If the miner requests tip enforcement, cap the lists now
if minTipBig != nil && !pool.locals.contains(addr) {
Expand Down
9 changes: 5 additions & 4 deletions miner/worker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package miner

import (
"errors"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/holiman/uint256"
"math/big"
"slices"
"sync"
"sync/atomic"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/holiman/uint256"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (w *worker) fillTransactionsAndBundles(interrupt *atomic.Int32, env *enviro
pendingBlobTxs := w.eth.TxPool().Pending(filter)

packFromTxpoolTimer.UpdateSince(start)
log.Debug("packFromTxpoolTimer", "duration", common.PrettyDuration(time.Since(start)), "hash", env.header.Hash())
log.Debug("packFromTxpoolTimer", "duration", common.PrettyDuration(time.Since(start)), "hash", env.header.Hash(), "txs", len(pendingPlainTxs))

// Split the pending transactions into locals and remotes.
localPlainTxs, remotePlainTxs := make(map[common.Address][]*txpool.LazyTransaction), pendingPlainTxs
Expand Down

0 comments on commit cb54455

Please sign in to comment.