Skip to content

Commit f048869

Browse files
committed
net_processing: Add missing erase orphan transactions when a block is connected.
Eliminating a leak that causes the orphan map to always grow to its maximum size. Extra information: I have skipped several PRs from upstream, this was first inside ConnectBlock, then was moved to ActivateBestChain and finally ended up moving it to net_processing under the SyncTransaction slot that we don't have anymore because we are much more updated.
1 parent 71fd4b2 commit f048869

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/net_processing.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,37 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) :
632632
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
633633
}
634634

635+
void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
636+
{
637+
LOCK(cs_main);
638+
639+
std::vector<uint256> vOrphanErase;
640+
641+
for (const CTransactionRef& ptx : pblock->vtx) {
642+
const CTransaction& tx = *ptx;
643+
644+
// Which orphan pool entries must we evict?
645+
for (size_t j = 0; j < tx.vin.size(); j++) {
646+
auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
647+
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
648+
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
649+
const CTransaction& orphanTx = *(*mi)->second.tx;
650+
const uint256& orphanHash = orphanTx.GetHash();
651+
vOrphanErase.emplace_back(orphanHash);
652+
}
653+
}
654+
}
655+
656+
// Erase orphan transactions include or precluded by this block
657+
if (!vOrphanErase.empty()) {
658+
int nErased = 0;
659+
for (uint256& orphanHash : vOrphanErase) {
660+
nErased += EraseOrphanTx(orphanHash);
661+
}
662+
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
663+
}
664+
}
665+
635666
void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload)
636667
{
637668
const int nNewHeight = pindexNew->nHeight;

src/net_processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class PeerLogicValidation : public CValidationInterface {
4545
PeerLogicValidation(CConnman* connmanIn);
4646
~PeerLogicValidation() = default;
4747

48+
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex) override;
4849
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
4950
void BlockChecked(const CBlock& block, const CValidationState& state) override;
5051
};

0 commit comments

Comments
 (0)