Skip to content

Commit

Permalink
orphanage: Extract EraseOrphansForBlock
Browse files Browse the repository at this point in the history
Summary:
Extract code that erases orphans when a new block is found into
EraseOrphansForBlock.

This is a backport of [[bitcoin/bitcoin#21148 | core#21148]] [10/14]
bitcoin/bitcoin@03257b8

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
  • Loading branch information
PiRK committed May 19, 2022
1 parent c15f8c9 commit ec313f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,43 +1968,9 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams &chainparams,
*/
void PeerManagerImpl::BlockConnected(
const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex) {
{
LOCK(g_cs_orphans);

std::vector<TxId> 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) {
Expand Down
36 changes: 36 additions & 0 deletions src/txorphanage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,39 @@ std::pair<CTransactionRef, NodeId> GetOrphanTx(const TxId &txid) {
}
return {it->second.tx, it->second.fromPeer};
}

void EraseOrphansForBlock(const CBlock &block) {
LOCK(g_cs_orphans);

std::vector<TxId> 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);
}
}
2 changes: 2 additions & 0 deletions src/txorphanage.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_TXORPHANAGE_H

#include <net.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <sync.h>

Expand All @@ -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,
Expand Down

0 comments on commit ec313f0

Please sign in to comment.