Skip to content

Commit 03257b8

Browse files
committed
txorphanage: Extract EraseOrphansForBlock
Extract code that erases orphans when a new block is found into EraseOrphansForBlock.
1 parent 3c4c3c2 commit 03257b8

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

src/net_processing.cpp

+2-30
Original file line numberDiff line numberDiff line change
@@ -1261,37 +1261,9 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn
12611261
*/
12621262
void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
12631263
{
1264-
{
1265-
LOCK(g_cs_orphans);
1266-
1267-
std::vector<uint256> vOrphanErase;
1268-
1269-
for (const CTransactionRef& ptx : pblock->vtx) {
1270-
const CTransaction& tx = *ptx;
1264+
EraseOrphansForBlock(*pblock);
1265+
m_last_tip_update = GetTime();
12711266

1272-
// Which orphan pool entries must we evict?
1273-
for (const auto& txin : tx.vin) {
1274-
auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
1275-
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
1276-
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
1277-
const CTransaction& orphanTx = *(*mi)->second.tx;
1278-
const uint256& orphanHash = orphanTx.GetHash();
1279-
vOrphanErase.push_back(orphanHash);
1280-
}
1281-
}
1282-
}
1283-
1284-
// Erase orphan transactions included or precluded by this block
1285-
if (vOrphanErase.size()) {
1286-
int nErased = 0;
1287-
for (const uint256& orphanHash : vOrphanErase) {
1288-
nErased += EraseOrphanTx(orphanHash);
1289-
}
1290-
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
1291-
}
1292-
1293-
m_last_tip_update = GetTime();
1294-
}
12951267
{
12961268
LOCK(m_recent_confirmed_transactions_mutex);
12971269
for (const auto& ptx : pblock->vtx) {

src/txorphanage.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,34 @@ std::pair<CTransactionRef, NodeId> GetOrphanTx(const uint256& txid)
175175
if (it == mapOrphanTransactions.end()) return {nullptr, -1};
176176
return {it->second.tx, it->second.fromPeer};
177177
}
178+
179+
void EraseOrphansForBlock(const CBlock& block)
180+
{
181+
LOCK(g_cs_orphans);
182+
183+
std::vector<uint256> vOrphanErase;
184+
185+
for (const CTransactionRef& ptx : block.vtx) {
186+
const CTransaction& tx = *ptx;
187+
188+
// Which orphan pool entries must we evict?
189+
for (const auto& txin : tx.vin) {
190+
auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
191+
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
192+
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
193+
const CTransaction& orphanTx = *(*mi)->second.tx;
194+
const uint256& orphanHash = orphanTx.GetHash();
195+
vOrphanErase.push_back(orphanHash);
196+
}
197+
}
198+
}
199+
200+
// Erase orphan transactions included or precluded by this block
201+
if (vOrphanErase.size()) {
202+
int nErased = 0;
203+
for (const uint256& orphanHash : vOrphanErase) {
204+
nErased += EraseOrphanTx(orphanHash);
205+
}
206+
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
207+
}
208+
}

src/txorphanage.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_TXORPHANAGE_H
77

88
#include <net.h>
9+
#include <primitives/block.h>
910
#include <primitives/transaction.h>
1011
#include <sync.h>
1112

@@ -22,6 +23,7 @@ struct COrphanTx {
2223

2324
int EraseOrphanTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
2425
void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
26+
void EraseOrphansForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
2527
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
2628
void AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
2729
bool HaveOrphanTx(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);

0 commit comments

Comments
 (0)