Skip to content

Commit a936f41

Browse files
committed
txorphanage: make m_peer_work_set private
1 parent 3614819 commit a936f41

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

src/net_processing.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,20 +2887,14 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
28872887
AssertLockHeld(cs_main);
28882888
AssertLockHeld(g_cs_orphans);
28892889

2890-
auto work_set_it = m_orphanage.m_peer_work_set.find(peer.m_id);
2891-
if (work_set_it == m_orphanage.m_peer_work_set.end()) return false;
2892-
2893-
std::set<uint256>& orphan_work_set = work_set_it->second;
2894-
2895-
while (!orphan_work_set.empty()) {
2896-
const uint256 orphanHash = *orphan_work_set.begin();
2897-
orphan_work_set.erase(orphan_work_set.begin());
2898-
2899-
const auto [porphanTx, from_peer] = m_orphanage.GetTx(orphanHash);
2900-
if (porphanTx == nullptr) continue;
2890+
CTransactionRef porphanTx = nullptr;
2891+
NodeId from_peer = -1;
2892+
bool more = false;
29012893

2894+
while (CTransactionRef porphanTx = m_orphanage.GetTxToReconsider(peer.m_id, from_peer, more)) {
29022895
const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
29032896
const TxValidationState& state = result.m_state;
2897+
const uint256& orphanHash = porphanTx->GetHash();
29042898

29052899
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
29062900
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
@@ -2957,7 +2951,7 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29572951
}
29582952
}
29592953

2960-
return !orphan_work_set.empty();
2954+
return more;
29612955
}
29622956

29632957
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,

src/test/fuzz/txorphan.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
8989
orphanage.AddChildrenToWorkSet(*tx, peer_id);
9090
},
9191
[&] {
92-
bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash()));
9392
{
9493
LOCK(g_cs_orphans);
95-
bool get_tx = orphanage.GetTx(tx->GetHash()).first != nullptr;
96-
Assert(have_tx == get_tx);
94+
NodeId originator;
95+
bool more = true;
96+
CTransactionRef ref = orphanage.GetTxToReconsider(peer_id, originator, more);
97+
if (!ref) {
98+
Assert(!more);
99+
} else {
100+
bool have_tx = orphanage.HaveTx(GenTxid::Txid(ref->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(ref->GetHash()));
101+
Assert(have_tx);
102+
}
97103
}
98104
},
99105
[&] {

src/txorphanage.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,27 @@ bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
167167
}
168168
}
169169

170-
std::pair<CTransactionRef, NodeId> TxOrphanage::GetTx(const uint256& txid) const
170+
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, NodeId& originator, bool& more)
171171
{
172172
AssertLockHeld(g_cs_orphans);
173173

174-
const auto it = m_orphans.find(txid);
175-
if (it == m_orphans.end()) return {nullptr, -1};
176-
return {it->second.tx, it->second.fromPeer};
174+
auto work_set_it = m_peer_work_set.find(peer);
175+
if (work_set_it != m_peer_work_set.end()) {
176+
auto& work_set = work_set_it->second;
177+
while (!work_set.empty()) {
178+
uint256 txid = *work_set.begin();
179+
work_set.erase(work_set.begin());
180+
181+
const auto orphan_it = m_orphans.find(txid);
182+
if (orphan_it != m_orphans.end()) {
183+
more = !work_set.empty();
184+
originator = orphan_it->second.fromPeer;
185+
return orphan_it->second.tx;
186+
}
187+
}
188+
}
189+
more = false;
190+
return nullptr;
177191
}
178192

179193
void TxOrphanage::EraseForBlock(const CBlock& block)

src/txorphanage.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include <primitives/transaction.h>
1111
#include <sync.h>
1212

13+
#include <map>
14+
#include <set>
15+
1316
/** Guards orphan transactions */
1417
extern RecursiveMutex g_cs_orphans;
1518

@@ -26,10 +29,14 @@ class TxOrphanage {
2629
/** Check if we already have an orphan transaction (by txid or wtxid) */
2730
bool HaveTx(const GenTxid& gtxid) const LOCKS_EXCLUDED(::g_cs_orphans);
2831

29-
/** Get an orphan transaction and its originating peer
30-
* (Transaction ref will be nullptr if not found)
32+
/** Extract a transaction from a peer's work set
33+
* Returns nullptr and sets more to false if there are no transactions
34+
* to work on. Otherwise returns the transaction reference, removes
35+
* the transaction from the work set, and populates its arguments with
36+
* the originating peer, and whether there are more orphans for this peer
37+
* to work on after this tx.
3138
*/
32-
std::pair<CTransactionRef, NodeId> GetTx(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
39+
CTransactionRef GetTxToReconsider(NodeId peer, NodeId& originator, bool& more) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
3340

3441
/** Erase an orphan by txid */
3542
int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
@@ -43,9 +50,6 @@ class TxOrphanage {
4350
/** Limit the orphanage to the given maximum */
4451
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
4552

46-
/** Which peer provided a parent tx of orphans that need to be reconsidered */
47-
std::map<NodeId, std::set<uint256>> m_peer_work_set GUARDED_BY(g_cs_orphans);
48-
4953
/** Add any orphans that list a particular tx as a parent into a peer's work set */
5054
void AddChildrenToWorkSet(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
5155

@@ -68,6 +72,9 @@ class TxOrphanage {
6872
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
6973
std::map<uint256, OrphanTx> m_orphans GUARDED_BY(g_cs_orphans);
7074

75+
/** Which peer provided a parent tx of orphans that need to be reconsidered */
76+
std::map<NodeId, std::set<uint256>> m_peer_work_set GUARDED_BY(g_cs_orphans);
77+
7178
using OrphanMap = decltype(m_orphans);
7279

7380
struct IteratorComparator

0 commit comments

Comments
 (0)