From ec313f0eff80b29a1e43008739fa945a2a6a0ecd Mon Sep 17 00:00:00 2001 From: PiRK Date: Wed, 18 May 2022 11:20:39 +0200 Subject: [PATCH] orphanage: Extract EraseOrphansForBlock Summary: Extract code that erases orphans when a new block is found into EraseOrphansForBlock. This is a backport of [[https://github.com/bitcoin/bitcoin/pull/21148 | core#21148]] [10/14] https://github.com/bitcoin/bitcoin/pull/21148/commits/03257b832debcb1470420d8657d30ba30f4be770 Depends on D11487 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D11489 --- src/net_processing.cpp | 38 ++------------------------------------ src/txorphanage.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/txorphanage.h | 2 ++ 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 52e8916d78..f8c27a9e00 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1968,43 +1968,9 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams &chainparams, */ void PeerManagerImpl::BlockConnected( const std::shared_ptr &pblock, const CBlockIndex *pindex) { - { - LOCK(g_cs_orphans); - - std::vector vOrphanErase; - - for (const CTransactionRef &ptx : pblock->vtx) { - const CTransaction &tx = *ptx; - - // Which orphan pool entries must we evict? - for (const auto &txin : tx.vin) { - auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout); - if (itByPrev == mapOrphanTransactionsByPrev.end()) { - continue; - } - - for (auto mi = itByPrev->second.begin(); - mi != itByPrev->second.end(); ++mi) { - const CTransaction &orphanTx = *(*mi)->second.tx; - const TxId &orphanId = orphanTx.GetId(); - vOrphanErase.push_back(orphanId); - } - } - } + EraseOrphansForBlock(*pblock); + m_last_tip_update = GetTime(); - // Erase orphan transactions included or precluded by this block - if (vOrphanErase.size()) { - int nErased = 0; - for (const auto &orphanId : vOrphanErase) { - nErased += EraseOrphanTx(orphanId); - } - LogPrint(BCLog::MEMPOOL, - "Erased %d orphan tx included or conflicted by block\n", - nErased); - } - - m_last_tip_update = GetTime(); - } { LOCK(m_recent_confirmed_transactions_mutex); for (const CTransactionRef &ptx : pblock->vtx) { diff --git a/src/txorphanage.cpp b/src/txorphanage.cpp index e760fcbe4d..e76a48b811 100644 --- a/src/txorphanage.cpp +++ b/src/txorphanage.cpp @@ -180,3 +180,39 @@ std::pair GetOrphanTx(const TxId &txid) { } return {it->second.tx, it->second.fromPeer}; } + +void EraseOrphansForBlock(const CBlock &block) { + LOCK(g_cs_orphans); + + std::vector vOrphanErase; + + for (const CTransactionRef &ptx : block.vtx) { + const CTransaction &tx = *ptx; + + // Which orphan pool entries must we evict? + for (const auto &txin : tx.vin) { + auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout); + if (itByPrev == mapOrphanTransactionsByPrev.end()) { + continue; + } + + for (auto mi = itByPrev->second.begin(); + mi != itByPrev->second.end(); ++mi) { + const CTransaction &orphanTx = *(*mi)->second.tx; + const TxId &orphanId = orphanTx.GetId(); + vOrphanErase.push_back(orphanId); + } + } + } + + // Erase orphan transactions included or precluded by this block + if (vOrphanErase.size()) { + int nErased = 0; + for (const auto &orphanId : vOrphanErase) { + nErased += EraseOrphanTx(orphanId); + } + LogPrint(BCLog::MEMPOOL, + "Erased %d orphan tx included or conflicted by block\n", + nErased); + } +} diff --git a/src/txorphanage.h b/src/txorphanage.h index f981744d1a..bf2b6715be 100644 --- a/src/txorphanage.h +++ b/src/txorphanage.h @@ -6,6 +6,7 @@ #define BITCOIN_TXORPHANAGE_H #include +#include #include #include @@ -22,6 +23,7 @@ struct COrphanTx { int EraseOrphanTx(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); +void EraseOrphansForBlock(const CBlock &block) LOCKS_EXCLUDED(g_cs_orphans); unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); void AddChildrenToWorkSet(const CTransaction &tx,