Skip to content

Commit 4c2b567

Browse files
authored
fix(txpool): rollback false optimization (#694)
* feat: add transactions len metrics of block processer * feat: rollback false optimize * feat: update * feat: bump version
1 parent 0bccb7b commit 4c2b567

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

core/tx_pool.go

+10-35
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,6 @@ var (
131131
reheapTimer = metrics.NewRegisteredTimer("txpool/reheap", nil)
132132
)
133133

134-
var (
135-
addrsPool = sync.Pool{
136-
New: func() interface{} {
137-
return make([]common.Address, 0, 8)
138-
},
139-
}
140-
addrBeatPool = sync.Pool{
141-
New: func() interface{} {
142-
return make(addressesByHeartbeat, 0, 8)
143-
},
144-
}
145-
)
146-
147134
// TxStatus is the current status of a transaction as seen by the pool.
148135
type TxStatus uint
149136

@@ -283,8 +270,6 @@ type TxPool struct {
283270
wg sync.WaitGroup // tracks loop, scheduleReorgLoop
284271
initDoneCh chan struct{} // is closed once the pool is initialized (for tests)
285272

286-
spammers *prque.Prque
287-
288273
changesSinceReorg int // A counter for how many drops we've performed in-between reorg.
289274
}
290275

@@ -316,7 +301,6 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block
316301
reorgShutdownCh: make(chan struct{}),
317302
initDoneCh: make(chan struct{}),
318303
gasPrice: new(big.Int).SetUint64(config.PriceLimit),
319-
spammers: prque.New(nil),
320304
}
321305
pool.locals = newAccountSet(pool.signer)
322306
for _, addr := range config.Locals {
@@ -1218,14 +1202,13 @@ func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirt
12181202
}
12191203
}
12201204
// Reset needs promote for all addresses
1221-
promoteAddrs = addrsPool.Get().([]common.Address)
1205+
promoteAddrs = make([]common.Address, 0, len(pool.queue))
12221206
for addr := range pool.queue {
12231207
promoteAddrs = append(promoteAddrs, addr)
12241208
}
12251209
}
12261210
// Check for pending transactions for every account that sent new ones
12271211
promoted := pool.promoteExecutables(promoteAddrs)
1228-
defer addrsPool.Put(promoteAddrs[:0])
12291212

12301213
// If a new block appeared, validate the pool of pending transactions. This will
12311214
// remove any transaction that has been included in the block or was invalidated
@@ -1465,19 +1448,18 @@ func (pool *TxPool) truncatePending() {
14651448

14661449
pendingBeforeCap := pending
14671450
// Assemble a spam order to penalize large transactors first
1468-
pool.spammers.Reset()
1451+
spammers := prque.New(nil)
14691452
for addr, list := range pool.pending {
14701453
// Only evict transactions from high rollers
14711454
if !pool.locals.contains(addr) && uint64(list.Len()) > pool.config.AccountSlots {
1472-
pool.spammers.Push(addr, int64(list.Len()))
1455+
spammers.Push(addr, int64(list.Len()))
14731456
}
14741457
}
14751458
// Gradually drop transactions from offenders
1476-
offenders := addrsPool.Get().([]common.Address)
1477-
defer addrsPool.Put(offenders[:0])
1478-
for pending > pool.config.GlobalSlots && !pool.spammers.Empty() {
1459+
offenders := []common.Address{}
1460+
for pending > pool.config.GlobalSlots && !spammers.Empty() {
14791461
// Retrieve the next offender if not local address
1480-
offender, _ := pool.spammers.Pop()
1462+
offender, _ := spammers.Pop()
14811463
offenders = append(offenders, offender.(common.Address))
14821464

14831465
// Equalize balances until all the same or below threshold
@@ -1550,8 +1532,7 @@ func (pool *TxPool) truncateQueue() {
15501532
}
15511533

15521534
// Sort all accounts with queued transactions by heartbeat
1553-
addresses := addrBeatPool.Get().(addressesByHeartbeat)
1554-
defer addrBeatPool.Put(addresses[:0])
1535+
addresses := make(addressesByHeartbeat, 0, len(pool.queue))
15551536
for addr := range pool.queue {
15561537
if !pool.locals.contains(addr) { // don't drop locals
15571538
addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
@@ -1701,10 +1682,7 @@ func (as *accountSet) containsTx(tx *types.Transaction) bool {
17011682
// add inserts a new address into the set to track.
17021683
func (as *accountSet) add(addr common.Address) {
17031684
as.accounts[addr] = struct{}{}
1704-
if as.cache != nil {
1705-
addrsPool.Put((*as.cache)[:0])
1706-
as.cache = nil
1707-
}
1685+
as.cache = nil
17081686
}
17091687

17101688
// addTx adds the sender of tx into the set.
@@ -1718,7 +1696,7 @@ func (as *accountSet) addTx(tx *types.Transaction) {
17181696
// reuse. The returned slice should not be changed!
17191697
func (as *accountSet) flatten() []common.Address {
17201698
if as.cache == nil {
1721-
accounts := addrsPool.Get().([]common.Address)
1699+
accounts := make([]common.Address, 0, len(as.accounts))
17221700
for account := range as.accounts {
17231701
accounts = append(accounts, account)
17241702
}
@@ -1732,10 +1710,7 @@ func (as *accountSet) merge(other *accountSet) {
17321710
for addr := range other.accounts {
17331711
as.accounts[addr] = struct{}{}
17341712
}
1735-
if as.cache != nil {
1736-
addrsPool.Put((*as.cache)[:0])
1737-
as.cache = nil
1738-
}
1713+
as.cache = nil
17391714
}
17401715

17411716
// txLookup is used internally by TxPool to track transactions while allowing

params/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 1 // Minor version component of the current release
27-
VersionPatch = 32 // Patch version component of the current release
27+
VersionPatch = 33 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

0 commit comments

Comments
 (0)