| 755 | |
| 756 | |
| 757 | unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) |
| 758 | { |
| 759 | LOCK(g_cs_orphans); |
| 760 | |
| 761 | unsigned int nEvicted = 0; |
| 762 | static int64_t nNextSweep; |
| 763 | int64_t nNow = GetTime(); |
| 764 | if (nNextSweep <= nNow) { |
| 765 | // Sweep out expired orphan pool entries: |
| 766 | int nErased = 0; |
| 767 | int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL; |
| 768 | std::map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin(); |
| 769 | while (iter != mapOrphanTransactions.end()) |
| 770 | { |
| 771 | std::map<uint256, COrphanTx>::iterator maybeErase = iter++; |
| 772 | if (maybeErase->second.nTimeExpire <= nNow) { |
| 773 | nErased += EraseOrphanTx(maybeErase->second.tx->GetHash()); |
| 774 | } else { |
| 775 | nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime); |
| 776 | } |
| 777 | } |
| 778 | // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan. |
| 779 | nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL; |
| 780 | if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased); |
| 781 | } |
| 782 | while (mapOrphanTransactions.size() > nMaxOrphans) |
| 783 | { |
| 784 | // Evict a random orphan: |
| 785 | uint256 randomhash = GetRandHash(); |
| 786 | std::map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash); |
| 787 | if (it == mapOrphanTransactions.end()) |
| 788 | it = mapOrphanTransactions.begin(); |
| 789 | EraseOrphanTx(it->first); |
| 790 | ++nEvicted; |
| 791 | } |
| 792 | return nEvicted; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Mark a misbehaving peer to be banned depending upon the value of `-banscore`. |