Skip to content

Commit f4852b8

Browse files
karalabeholiman
andauthored
core/txpool, eth, miner: retrieve plain and blob txs separately (#29026)
* core/txpool, eth, miner: retrieve plain and blob txs separately * core/txpool: fix typo, no farming * miner: farm all the typos Co-authored-by: Martin HS <[email protected]> --------- Co-authored-by: Martin HS <[email protected]>
1 parent ac0ff04 commit f4852b8

File tree

12 files changed

+125
-50
lines changed

12 files changed

+125
-50
lines changed

core/txpool/blobpool/blobpool.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,12 @@ func (p *BlobPool) drop() {
14461446
//
14471447
// The transactions can also be pre-filtered by the dynamic fee components to
14481448
// reduce allocations and load on downstream subsystems.
1449-
func (p *BlobPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*txpool.LazyTransaction {
1449+
func (p *BlobPool) Pending(filter txpool.PendingFilter) map[common.Address][]*txpool.LazyTransaction {
1450+
// If only plain transactions are requested, this pool is unsuitable as it
1451+
// contains none, don't even bother.
1452+
if filter.OnlyPlainTxs {
1453+
return nil
1454+
}
14501455
// Track the amount of time waiting to retrieve the list of pending blob txs
14511456
// from the pool and the amount of time actually spent on assembling the data.
14521457
// The latter will be pretty much moot, but we've kept it to have symmetric
@@ -1466,20 +1471,20 @@ func (p *BlobPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *u
14661471
lazies := make([]*txpool.LazyTransaction, 0, len(txs))
14671472
for _, tx := range txs {
14681473
// If transaction filtering was requested, discard badly priced ones
1469-
if minTip != nil && baseFee != nil {
1470-
if tx.execFeeCap.Lt(baseFee) {
1474+
if filter.MinTip != nil && filter.BaseFee != nil {
1475+
if tx.execFeeCap.Lt(filter.BaseFee) {
14711476
break // basefee too low, cannot be included, discard rest of txs from the account
14721477
}
1473-
tip := new(uint256.Int).Sub(tx.execFeeCap, baseFee)
1478+
tip := new(uint256.Int).Sub(tx.execFeeCap, filter.BaseFee)
14741479
if tip.Gt(tx.execTipCap) {
14751480
tip = tx.execTipCap
14761481
}
1477-
if tip.Lt(minTip) {
1482+
if tip.Lt(filter.MinTip) {
14781483
break // allowed or remaining tip too low, cannot be included, discard rest of txs from the account
14791484
}
14801485
}
1481-
if blobFee != nil {
1482-
if tx.blobFeeCap.Lt(blobFee) {
1486+
if filter.BlobFee != nil {
1487+
if tx.blobFeeCap.Lt(filter.BlobFee) {
14831488
break // blobfee too low, cannot be included, discard rest of txs from the account
14841489
}
14851490
}

core/txpool/blobpool/blobpool_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,11 @@ func benchmarkPoolPending(b *testing.B, datacap uint64) {
13401340
b.ReportAllocs()
13411341

13421342
for i := 0; i < b.N; i++ {
1343-
p := pool.Pending(uint256.NewInt(1), chain.basefee, chain.blobfee)
1343+
p := pool.Pending(txpool.PendingFilter{
1344+
MinTip: uint256.NewInt(1),
1345+
BaseFee: chain.basefee,
1346+
BlobFee: chain.blobfee,
1347+
})
13441348
if len(p) != int(capacity) {
13451349
b.Fatalf("have %d want %d", len(p), capacity)
13461350
}

core/txpool/legacypool/legacypool.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,12 @@ func (pool *LegacyPool) ContentFrom(addr common.Address) ([]*types.Transaction,
522522
//
523523
// The transactions can also be pre-filtered by the dynamic fee components to
524524
// reduce allocations and load on downstream subsystems.
525-
func (pool *LegacyPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*txpool.LazyTransaction {
525+
func (pool *LegacyPool) Pending(filter txpool.PendingFilter) map[common.Address][]*txpool.LazyTransaction {
526+
// If only blob transactions are requested, this pool is unsuitable as it
527+
// contains none, don't even bother.
528+
if filter.OnlyBlobTxs {
529+
return nil
530+
}
526531
pool.mu.Lock()
527532
defer pool.mu.Unlock()
528533

@@ -531,13 +536,12 @@ func (pool *LegacyPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobF
531536
minTipBig *big.Int
532537
baseFeeBig *big.Int
533538
)
534-
if minTip != nil {
535-
minTipBig = minTip.ToBig()
539+
if filter.MinTip != nil {
540+
minTipBig = filter.MinTip.ToBig()
536541
}
537-
if baseFee != nil {
538-
baseFeeBig = baseFee.ToBig()
542+
if filter.BaseFee != nil {
543+
baseFeeBig = filter.BaseFee.ToBig()
539544
}
540-
541545
pending := make(map[common.Address][]*txpool.LazyTransaction, len(pool.pending))
542546
for addr, list := range pool.pending {
543547
txs := list.Flatten()

core/txpool/subpool.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ type LazyResolver interface {
7070
// may request (and relinquish) exclusive access to certain addresses.
7171
type AddressReserver func(addr common.Address, reserve bool) error
7272

73+
// PendingFilter is a collection of filter rules to allow retrieving a subset
74+
// of transactions for announcement or mining.
75+
//
76+
// Note, the entries here are not arbitrary useful filters, rather each one has
77+
// a very specific call site in mind and each one can be evaluated very cheaply
78+
// by the pool implementations. Only add new ones that satisfy those constraints.
79+
type PendingFilter struct {
80+
MinTip *uint256.Int // Minimum miner tip required to include a transaction
81+
BaseFee *uint256.Int // Minimum 1559 basefee needed to include a transaction
82+
BlobFee *uint256.Int // Minimum 4844 blobfee needed to include a blob transaction
83+
84+
OnlyPlainTxs bool // Return only plain EVM transactions (peer-join announces, block space filling)
85+
OnlyBlobTxs bool // Return only blob transactions (block blob-space filling)
86+
}
87+
7388
// SubPool represents a specialized transaction pool that lives on its own (e.g.
7489
// blob pool). Since independent of how many specialized pools we have, they do
7590
// need to be updated in lockstep and assemble into one coherent view for block
@@ -118,7 +133,7 @@ type SubPool interface {
118133
//
119134
// The transactions can also be pre-filtered by the dynamic fee components to
120135
// reduce allocations and load on downstream subsystems.
121-
Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*LazyTransaction
136+
Pending(filter PendingFilter) map[common.Address][]*LazyTransaction
122137

123138
// SubscribeTransactions subscribes to new transaction events. The subscriber
124139
// can decide whether to receive notifications only for newly seen transactions

core/txpool/txpool.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/ethereum/go-ethereum/event"
2929
"github.com/ethereum/go-ethereum/log"
3030
"github.com/ethereum/go-ethereum/metrics"
31-
"github.com/holiman/uint256"
3231
)
3332

3433
// TxStatus is the current status of a transaction as seen by the pool.
@@ -357,10 +356,10 @@ func (p *TxPool) Add(txs []*types.Transaction, local bool, sync bool) []error {
357356
//
358357
// The transactions can also be pre-filtered by the dynamic fee components to
359358
// reduce allocations and load on downstream subsystems.
360-
func (p *TxPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*LazyTransaction {
359+
func (p *TxPool) Pending(filter PendingFilter) map[common.Address][]*LazyTransaction {
361360
txs := make(map[common.Address][]*LazyTransaction)
362361
for _, subpool := range p.subpools {
363-
for addr, set := range subpool.Pending(minTip, baseFee, blobFee) {
362+
for addr, set := range subpool.Pending(filter) {
364363
txs[addr] = set
365364
}
366365
}

eth/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (b *EthAPIBackend) SendTx(ctx context.Context, signedTx *types.Transaction)
292292
}
293293

294294
func (b *EthAPIBackend) GetPoolTransactions() (types.Transactions, error) {
295-
pending := b.eth.txPool.Pending(nil, nil, nil)
295+
pending := b.eth.txPool.Pending(txpool.PendingFilter{})
296296
var txs types.Transactions
297297
for _, batch := range pending {
298298
for _, lazy := range batch {

eth/catalyst/simulated_beacon.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/ethereum/go-ethereum/beacon/engine"
2727
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/core/txpool"
2829
"github.com/ethereum/go-ethereum/core/types"
2930
"github.com/ethereum/go-ethereum/eth"
3031
"github.com/ethereum/go-ethereum/log"
@@ -263,7 +264,7 @@ func (c *SimulatedBeacon) Rollback() {
263264

264265
// Fork sets the head to the provided hash.
265266
func (c *SimulatedBeacon) Fork(parentHash common.Hash) error {
266-
if len(c.eth.TxPool().Pending(nil, nil, nil)) != 0 {
267+
if len(c.eth.TxPool().Pending(txpool.PendingFilter{})) != 0 {
267268
return errors.New("pending block dirty")
268269
}
269270
parent := c.eth.BlockChain().GetBlockByHash(parentHash)
@@ -275,7 +276,7 @@ func (c *SimulatedBeacon) Fork(parentHash common.Hash) error {
275276

276277
// AdjustTime creates a new block with an adjusted timestamp.
277278
func (c *SimulatedBeacon) AdjustTime(adjustment time.Duration) error {
278-
if len(c.eth.TxPool().Pending(nil, nil, nil)) != 0 {
279+
if len(c.eth.TxPool().Pending(txpool.PendingFilter{})) != 0 {
279280
return errors.New("could not adjust time on non-empty block")
280281
}
281282
parent := c.eth.BlockChain().CurrentBlock()

eth/handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"github.com/ethereum/go-ethereum/metrics"
4343
"github.com/ethereum/go-ethereum/p2p"
4444
"github.com/ethereum/go-ethereum/triedb/pathdb"
45-
"github.com/holiman/uint256"
4645
)
4746

4847
const (
@@ -74,7 +73,7 @@ type txPool interface {
7473

7574
// Pending should return pending transactions.
7675
// The slice should be modifiable by the caller.
77-
Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*txpool.LazyTransaction
76+
Pending(filter txpool.PendingFilter) map[common.Address][]*txpool.LazyTransaction
7877

7978
// SubscribeTransactions subscribes to new transaction events. The subscriber
8079
// can decide whether to receive notifications only for newly seen transactions

eth/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (p *testTxPool) Add(txs []*types.Transaction, local bool, sync bool) []erro
9393
}
9494

9595
// Pending returns all the transactions known to the pool
96-
func (p *testTxPool) Pending(minTip *uint256.Int, baseFee *uint256.Int, blobFee *uint256.Int) map[common.Address][]*txpool.LazyTransaction {
96+
func (p *testTxPool) Pending(filter txpool.PendingFilter) map[common.Address][]*txpool.LazyTransaction {
9797
p.lock.RLock()
9898
defer p.lock.RUnlock()
9999

eth/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
26+
"github.com/ethereum/go-ethereum/core/txpool"
2627
"github.com/ethereum/go-ethereum/eth/downloader"
2728
"github.com/ethereum/go-ethereum/eth/protocols/eth"
2829
"github.com/ethereum/go-ethereum/log"
@@ -36,7 +37,7 @@ const (
3637
// syncTransactions starts sending all currently pending transactions to the given peer.
3738
func (h *handler) syncTransactions(p *eth.Peer) {
3839
var hashes []common.Hash
39-
for _, batch := range h.txpool.Pending(nil, nil, nil) {
40+
for _, batch := range h.txpool.Pending(txpool.PendingFilter{OnlyPlainTxs: true}) {
4041
for _, tx := range batch {
4142
hashes = append(hashes, tx.Hash)
4243
}

0 commit comments

Comments
 (0)