Skip to content

Commit ecb0a3e

Browse files
committed
net_processing: Don't process tx after processing orphans
If we made progress on orphans, consider that enough work for this peer for this round of ProcessMessages. This also allows cleaning up the api for TxOrphange:GetTxToReconsider().
1 parent c583775 commit ecb0a3e

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,17 @@ class PeerManagerImpl final : public PeerManager
584584
/**
585585
* Reconsider orphan transactions after a parent has been accepted to the mempool.
586586
*
587-
* @peer[in] peer The peer whose orphan transactions we will reconsider. Generally only one
588-
* orphan will be reconsidered on each call of this function. This set
589-
* may be added to if accepting an orphan causes its children to be
590-
* reconsidered.
591-
* @return True if there are still orphans in this peer's work set.
587+
* @peer[in] peer The peer whose orphan transactions we will reconsider. Generally only
588+
* one orphan will be reconsidered on each call of this function. If an
589+
* accepted orphan has orphaned children, those will need to be
590+
* reconsidered, creating more work, possibly for other peers.
591+
* @return True if meaningful work was done (an orphan was accepted/rejected).
592+
* If no meaningful work was done, then the work set for this peer
593+
* will be empty.
592594
*/
593595
bool ProcessOrphanTx(Peer& peer)
594596
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
597+
595598
/** Process a single headers message from a peer.
596599
*
597600
* @param[in] pfrom CNode of the peer
@@ -2892,9 +2895,8 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
28922895
LOCK(cs_main);
28932896

28942897
CTransactionRef porphanTx = nullptr;
2895-
bool more = false;
28962898

2897-
while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(peer.m_id, more)) {
2899+
while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(peer.m_id)) {
28982900
const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
28992901
const TxValidationState& state = result.m_state;
29002902
const uint256& orphanHash = porphanTx->GetHash();
@@ -2907,7 +2909,7 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29072909
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
29082910
AddToCompactExtraTransactions(removedTx);
29092911
}
2910-
break;
2912+
return true;
29112913
} else if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
29122914
if (state.IsInvalid()) {
29132915
LogPrint(BCLog::MEMPOOL, " invalid orphan tx %s from peer=%d. %s\n",
@@ -2950,11 +2952,11 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29502952
}
29512953
}
29522954
m_orphanage.EraseTx(orphanHash);
2953-
break;
2955+
return true;
29542956
}
29552957
}
29562958

2957-
return more;
2959+
return false;
29582960
}
29592961

29602962
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
@@ -4849,12 +4851,12 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
48494851
}
48504852
}
48514853

4852-
const bool has_more_orphans = ProcessOrphanTx(*peer);
4854+
const bool processed_orphan = ProcessOrphanTx(*peer);
48534855

48544856
if (pfrom->fDisconnect)
48554857
return false;
48564858

4857-
if (has_more_orphans) return true;
4859+
if (processed_orphan) return true;
48584860

48594861
// this maintains the order of responses
48604862
// and prevents m_getdata_requests to grow unbounded

src/test/fuzz/txorphan.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
8989
},
9090
[&] {
9191
{
92-
bool more = true;
93-
CTransactionRef ref = orphanage.GetTxToReconsider(peer_id, more);
94-
if (!ref) {
95-
Assert(!more);
96-
} else {
92+
CTransactionRef ref = orphanage.GetTxToReconsider(peer_id);
93+
if (ref) {
9794
bool have_tx = orphanage.HaveTx(GenTxid::Txid(ref->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(ref->GetHash()));
9895
Assert(have_tx);
9996
}

src/txorphanage.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
174174
}
175175
}
176176

177-
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, bool& more)
177+
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)
178178
{
179179
LOCK(m_mutex);
180180

@@ -187,12 +187,10 @@ CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, bool& more)
187187

188188
const auto orphan_it = m_orphans.find(txid);
189189
if (orphan_it != m_orphans.end()) {
190-
more = !work_set.empty();
191190
return orphan_it->second.tx;
192191
}
193192
}
194193
}
195-
more = false;
196194
return nullptr;
197195
}
198196

src/txorphanage.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ class TxOrphanage {
2727
bool HaveTx(const GenTxid& gtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
2828

2929
/** Extract a transaction from a peer's work set
30-
* Returns nullptr and sets more to false if there are no transactions
31-
* to work on. Otherwise returns the transaction reference, removes
32-
* the transaction from the work set, and sets "more" to indicate
33-
* if there are more orphans for this peer
34-
* to work on after this tx.
30+
* Returns nullptr if there are no transactions to work on.
31+
* Otherwise returns the transaction reference, and removes
32+
* it from the work set.
3533
*/
36-
CTransactionRef GetTxToReconsider(NodeId peer, bool& more) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
34+
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
3735

3836
/** Erase an orphan by txid */
3937
int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

0 commit comments

Comments
 (0)