From 0265f725868185af3b12396253c3f67ec2df4802 Mon Sep 17 00:00:00 2001 From: Kevin Chen Date: Fri, 28 Jan 2022 14:46:57 -0600 Subject: [PATCH] Remove private transactions that are confirmed in blocks --- core/tx_pool.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/core/tx_pool.go b/core/tx_pool.go index ae16d6b01b25..b55d05c7aff2 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -1669,6 +1669,7 @@ func (pool *TxPool) demoteUnexecutables() { for _, tx := range olds { hash := tx.Hash() pool.all.Remove(hash) + pool.privateTxs.Remove(hash) log.Trace("Removed old pending transaction", "hash", hash) } // Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later @@ -1969,14 +1970,12 @@ func (t *txLookup) RemotesBelowTip(threshold *big.Int) types.Transactions { type timestampedTxHashSet struct { lock sync.RWMutex - hashes []common.Hash timestamps map[common.Hash]time.Time ttl time.Duration } func newExpiringTxHashSet(ttl time.Duration) *timestampedTxHashSet { s := ×tampedTxHashSet{ - hashes: make([]common.Hash, 0), timestamps: make(map[common.Hash]time.Time), ttl: ttl, } @@ -1988,8 +1987,10 @@ func (s *timestampedTxHashSet) Add(hash common.Hash) { s.lock.Lock() defer s.lock.Unlock() - s.hashes = append(s.hashes, hash) - s.timestamps[hash] = time.Now().Add(s.ttl) + _, ok := s.timestamps[hash] + if !ok { + s.timestamps[hash] = time.Now().Add(s.ttl) + } } func (s *timestampedTxHashSet) Contains(hash common.Hash) bool { @@ -1999,25 +2000,26 @@ func (s *timestampedTxHashSet) Contains(hash common.Hash) bool { return ok } -func (s *timestampedTxHashSet) prune() { +func (s *timestampedTxHashSet) Remove(hash common.Hash) { s.lock.Lock() defer s.lock.Unlock() - var ( - count int - now = time.Now() - ) - for _, hash := range s.hashes { - ts := s.timestamps[hash] - if ts.After(now) { - break - } - + _, ok := s.timestamps[hash] + if ok { delete(s.timestamps, hash) - count += 1 } +} + +func (s *timestampedTxHashSet) prune() { + s.lock.Lock() + defer s.lock.Unlock() - s.hashes = s.hashes[count:] + now := time.Now() + for hash, ts := range s.timestamps { + if ts.Before(now) { + delete(s.timestamps, hash) + } + } } // numSlots calculates the number of slots needed for a single transaction.