@@ -100,6 +100,7 @@ struct IteratorComparator
100
100
struct COrphanTx {
101
101
CTransaction tx;
102
102
NodeId fromPeer;
103
+ int64_t nTimeExpire;
103
104
};
104
105
map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY (cs_main);
105
106
map<COutPoint, set<map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY (cs_main);
@@ -641,7 +642,7 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(c
641
642
return false ;
642
643
}
643
644
644
- auto ret = mapOrphanTransactions.emplace (hash, COrphanTx{tx, peer});
645
+ auto ret = mapOrphanTransactions.emplace (hash, COrphanTx{tx, peer, GetTime () + ORPHAN_TX_EXPIRE_TIME });
645
646
assert (ret.second );
646
647
BOOST_FOREACH (const CTxIn& txin, tx.vin ) {
647
648
mapOrphanTransactionsByPrev[txin.prevout ].insert (ret.first );
@@ -689,6 +690,26 @@ void EraseOrphansFor(NodeId peer)
689
690
unsigned int LimitOrphanTxSize (unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
690
691
{
691
692
unsigned int nEvicted = 0 ;
693
+ static int64_t nNextSweep;
694
+ int64_t nNow = GetTime ();
695
+ if (nNextSweep <= nNow) {
696
+ // Sweep out expired orphan pool entries:
697
+ int nErased = 0 ;
698
+ int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
699
+ map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin ();
700
+ while (iter != mapOrphanTransactions.end ())
701
+ {
702
+ map<uint256, COrphanTx>::iterator maybeErase = iter++;
703
+ if (maybeErase->second .nTimeExpire <= nNow) {
704
+ nErased += EraseOrphanTx (maybeErase->second .tx .GetHash ());
705
+ } else {
706
+ nMinExpTime = std::min (maybeErase->second .nTimeExpire , nMinExpTime);
707
+ }
708
+ }
709
+ // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
710
+ nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
711
+ if (nErased > 0 ) LogPrint (" mempool" , " Erased %d orphan tx due to expiration\n " , nErased);
712
+ }
692
713
while (mapOrphanTransactions.size () > nMaxOrphans)
693
714
{
694
715
// Evict a random orphan:
0 commit comments