From 4c2b5671165346a8dea1d10e686a62c6c66c4d0e Mon Sep 17 00:00:00 2001 From: georgehao Date: Mon, 8 Apr 2024 10:13:43 +0800 Subject: [PATCH] fix(txpool): rollback false optimization (#694) * feat: add transactions len metrics of block processer * feat: rollback false optimize * feat: update * feat: bump version --- core/tx_pool.go | 45 ++++++++++----------------------------------- params/version.go | 2 +- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/core/tx_pool.go b/core/tx_pool.go index 063686e342..273c31e50c 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -131,19 +131,6 @@ var ( reheapTimer = metrics.NewRegisteredTimer("txpool/reheap", nil) ) -var ( - addrsPool = sync.Pool{ - New: func() interface{} { - return make([]common.Address, 0, 8) - }, - } - addrBeatPool = sync.Pool{ - New: func() interface{} { - return make(addressesByHeartbeat, 0, 8) - }, - } -) - // TxStatus is the current status of a transaction as seen by the pool. type TxStatus uint @@ -283,8 +270,6 @@ type TxPool struct { wg sync.WaitGroup // tracks loop, scheduleReorgLoop initDoneCh chan struct{} // is closed once the pool is initialized (for tests) - spammers *prque.Prque - changesSinceReorg int // A counter for how many drops we've performed in-between reorg. } @@ -316,7 +301,6 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain block reorgShutdownCh: make(chan struct{}), initDoneCh: make(chan struct{}), gasPrice: new(big.Int).SetUint64(config.PriceLimit), - spammers: prque.New(nil), } pool.locals = newAccountSet(pool.signer) for _, addr := range config.Locals { @@ -1218,14 +1202,13 @@ func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirt } } // Reset needs promote for all addresses - promoteAddrs = addrsPool.Get().([]common.Address) + promoteAddrs = make([]common.Address, 0, len(pool.queue)) for addr := range pool.queue { promoteAddrs = append(promoteAddrs, addr) } } // Check for pending transactions for every account that sent new ones promoted := pool.promoteExecutables(promoteAddrs) - defer addrsPool.Put(promoteAddrs[:0]) // If a new block appeared, validate the pool of pending transactions. This will // remove any transaction that has been included in the block or was invalidated @@ -1465,19 +1448,18 @@ func (pool *TxPool) truncatePending() { pendingBeforeCap := pending // Assemble a spam order to penalize large transactors first - pool.spammers.Reset() + spammers := prque.New(nil) for addr, list := range pool.pending { // Only evict transactions from high rollers if !pool.locals.contains(addr) && uint64(list.Len()) > pool.config.AccountSlots { - pool.spammers.Push(addr, int64(list.Len())) + spammers.Push(addr, int64(list.Len())) } } // Gradually drop transactions from offenders - offenders := addrsPool.Get().([]common.Address) - defer addrsPool.Put(offenders[:0]) - for pending > pool.config.GlobalSlots && !pool.spammers.Empty() { + offenders := []common.Address{} + for pending > pool.config.GlobalSlots && !spammers.Empty() { // Retrieve the next offender if not local address - offender, _ := pool.spammers.Pop() + offender, _ := spammers.Pop() offenders = append(offenders, offender.(common.Address)) // Equalize balances until all the same or below threshold @@ -1550,8 +1532,7 @@ func (pool *TxPool) truncateQueue() { } // Sort all accounts with queued transactions by heartbeat - addresses := addrBeatPool.Get().(addressesByHeartbeat) - defer addrBeatPool.Put(addresses[:0]) + addresses := make(addressesByHeartbeat, 0, len(pool.queue)) for addr := range pool.queue { if !pool.locals.contains(addr) { // don't drop locals addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]}) @@ -1701,10 +1682,7 @@ func (as *accountSet) containsTx(tx *types.Transaction) bool { // add inserts a new address into the set to track. func (as *accountSet) add(addr common.Address) { as.accounts[addr] = struct{}{} - if as.cache != nil { - addrsPool.Put((*as.cache)[:0]) - as.cache = nil - } + as.cache = nil } // addTx adds the sender of tx into the set. @@ -1718,7 +1696,7 @@ func (as *accountSet) addTx(tx *types.Transaction) { // reuse. The returned slice should not be changed! func (as *accountSet) flatten() []common.Address { if as.cache == nil { - accounts := addrsPool.Get().([]common.Address) + accounts := make([]common.Address, 0, len(as.accounts)) for account := range as.accounts { accounts = append(accounts, account) } @@ -1732,10 +1710,7 @@ func (as *accountSet) merge(other *accountSet) { for addr := range other.accounts { as.accounts[addr] = struct{}{} } - if as.cache != nil { - addrsPool.Put((*as.cache)[:0]) - as.cache = nil - } + as.cache = nil } // txLookup is used internally by TxPool to track transactions while allowing diff --git a/params/version.go b/params/version.go index 129463c0a4..498a80425e 100644 --- a/params/version.go +++ b/params/version.go @@ -24,7 +24,7 @@ import ( const ( VersionMajor = 5 // Major version component of the current release VersionMinor = 1 // Minor version component of the current release - VersionPatch = 32 // Patch version component of the current release + VersionPatch = 33 // Patch version component of the current release VersionMeta = "mainnet" // Version metadata to append to the version string )