Skip to content

Commit 3614819

Browse files
committed
txorphange: move orphan workset to txorphanage
1 parent 6f8e442 commit 3614819

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

src/net_processing.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,6 @@ struct Peer {
368368
/** Total number of addresses that were processed (excludes rate-limited ones). */
369369
std::atomic<uint64_t> m_addr_processed{0};
370370

371-
/** Set of txids to reconsider once their parent transactions have been accepted **/
372-
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
373-
374371
/** Whether we've sent this peer a getheaders in response to an inv prior to initial-headers-sync completing */
375372
bool m_inv_triggered_getheaders_before_sync GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
376373

@@ -2890,11 +2887,14 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
28902887
AssertLockHeld(cs_main);
28912888
AssertLockHeld(g_cs_orphans);
28922889

2893-
if (peer.m_orphan_work_set.empty()) return false;
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;
28942894

2895-
while (!peer.m_orphan_work_set.empty()) {
2896-
const uint256 orphanHash = *peer.m_orphan_work_set.begin();
2897-
peer.m_orphan_work_set.erase(peer.m_orphan_work_set.begin());
2895+
while (!orphan_work_set.empty()) {
2896+
const uint256 orphanHash = *orphan_work_set.begin();
2897+
orphan_work_set.erase(orphan_work_set.begin());
28982898

28992899
const auto [porphanTx, from_peer] = m_orphanage.GetTx(orphanHash);
29002900
if (porphanTx == nullptr) continue;
@@ -2905,7 +2905,7 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29052905
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
29062906
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
29072907
RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
2908-
m_orphanage.AddChildrenToWorkSet(*porphanTx, peer.m_orphan_work_set);
2908+
m_orphanage.AddChildrenToWorkSet(*porphanTx, peer.m_id);
29092909
m_orphanage.EraseTx(orphanHash);
29102910
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
29112911
AddToCompactExtraTransactions(removedTx);
@@ -2957,7 +2957,7 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
29572957
}
29582958
}
29592959

2960-
return !peer.m_orphan_work_set.empty();
2960+
return !orphan_work_set.empty();
29612961
}
29622962

29632963
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
@@ -3950,7 +3950,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
39503950
m_txrequest.ForgetTxHash(tx.GetHash());
39513951
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
39523952
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3953-
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
3953+
m_orphanage.AddChildrenToWorkSet(tx, peer->m_id);
39543954

39553955
pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
39563956

src/test/fuzz/txorphan.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
3636
SetMockTime(ConsumeTime(fuzzed_data_provider));
3737

3838
TxOrphanage orphanage;
39-
std::set<uint256> orphan_work_set;
4039
std::vector<COutPoint> outpoints;
4140
// initial outpoints used to construct transactions later
4241
for (uint8_t i = 0; i < 4; i++) {
@@ -87,7 +86,7 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
8786
fuzzed_data_provider,
8887
[&] {
8988
LOCK(g_cs_orphans);
90-
orphanage.AddChildrenToWorkSet(*tx, orphan_work_set);
89+
orphanage.AddChildrenToWorkSet(*tx, peer_id);
9190
},
9291
[&] {
9392
bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash()));

src/txorphanage.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void TxOrphanage::EraseForPeer(NodeId peer)
8989
{
9090
AssertLockHeld(g_cs_orphans);
9191

92+
m_peer_work_set.erase(peer);
93+
9294
int nErased = 0;
9395
std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
9496
while (iter != m_orphans.end())
@@ -138,9 +140,13 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
138140
if (nEvicted > 0) LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
139141
}
140142

141-
void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) const
143+
void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx, NodeId peer)
142144
{
143145
AssertLockHeld(g_cs_orphans);
146+
147+
// Get this peer's work set, emplacing an empty set it didn't exist
148+
std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(peer).first->second;
149+
144150
for (unsigned int i = 0; i < tx.vout.size(); i++) {
145151
const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i));
146152
if (it_by_prev != m_outpoint_to_orphan_it.end()) {

src/txorphanage.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ class TxOrphanage {
4343
/** Limit the orphanage to the given maximum */
4444
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
4545

46-
/** Add any orphans that list a particular tx as a parent into a peer's work set
47-
* (ie orphans that may have found their final missing parent, and so should be reconsidered for the mempool) */
48-
void AddChildrenToWorkSet(const CTransaction& tx, std::set<uint256>& orphan_work_set) const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
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+
49+
/** Add any orphans that list a particular tx as a parent into a peer's work set */
50+
void AddChildrenToWorkSet(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
4951

5052
/** Return how many entries exist in the orphange */
5153
size_t Size() LOCKS_EXCLUDED(::g_cs_orphans)

0 commit comments

Comments
 (0)