Skip to content

Commit 733d85f

Browse files
committed
Move all g_cs_orphans locking to txorphanage
1 parent a936f41 commit 733d85f

File tree

5 files changed

+43
-42
lines changed

5 files changed

+43
-42
lines changed

src/net_processing.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ class PeerManagerImpl final : public PeerManager
595595
* reconsidered.
596596
* @return True if there are still orphans in this peer's work set.
597597
*/
598-
bool ProcessOrphanTx(Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans)
599-
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex);
598+
bool ProcessOrphanTx(Peer& peer)
599+
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, cs_main);
600600
/** Process a single headers message from a peer.
601601
*
602602
* @param[in] pfrom CNode of the peer
@@ -1501,7 +1501,7 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
15011501
for (const QueuedBlock& entry : state->vBlocksInFlight) {
15021502
mapBlocksInFlight.erase(entry.pindex->GetBlockHash());
15031503
}
1504-
WITH_LOCK(g_cs_orphans, m_orphanage.EraseForPeer(nodeid));
1504+
m_orphanage.EraseForPeer(nodeid);
15051505
m_txrequest.DisconnectedPeer(nodeid);
15061506
m_num_preferred_download_peers -= state->fPreferredDownload;
15071507
m_peers_downloading_from -= (state->nBlocksInFlight != 0);
@@ -2885,7 +2885,6 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
28852885
{
28862886
AssertLockHeld(g_msgproc_mutex);
28872887
AssertLockHeld(cs_main);
2888-
AssertLockHeld(g_cs_orphans);
28892888

28902889
CTransactionRef porphanTx = nullptr;
28912890
NodeId from_peer = -1;
@@ -3903,7 +3902,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
39033902
AddKnownTx(*peer, txid);
39043903
}
39053904

3906-
LOCK2(cs_main, g_cs_orphans);
3905+
LOCK(cs_main);
39073906

39083907
m_txrequest.ReceivedResponse(pfrom.GetId(), txid);
39093908
if (tx.HasWitness()) m_txrequest.ReceivedResponse(pfrom.GetId(), wtxid);
@@ -4139,7 +4138,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
41394138
bool fBlockReconstructed = false;
41404139

41414140
{
4142-
LOCK2(cs_main, g_cs_orphans);
4141+
LOCK(cs_main);
41434142
// If AcceptBlockHeader returned true, it set pindex
41444143
assert(pindex);
41454144
UpdateBlockAvailability(pfrom.GetId(), pindex->GetBlockHash());
@@ -4769,7 +4768,7 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
47694768

47704769
bool has_more_orphans;
47714770
{
4772-
LOCK2(cs_main, g_cs_orphans);
4771+
LOCK(cs_main);
47734772
has_more_orphans = ProcessOrphanTx(*peer);
47744773
}
47754774

src/test/fuzz/txorphan.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
8585
CallOneOf(
8686
fuzzed_data_provider,
8787
[&] {
88-
LOCK(g_cs_orphans);
8988
orphanage.AddChildrenToWorkSet(*tx, peer_id);
9089
},
9190
[&] {
9291
{
93-
LOCK(g_cs_orphans);
9492
NodeId originator;
9593
bool more = true;
9694
CTransactionRef ref = orphanage.GetTxToReconsider(peer_id, originator, more);
@@ -107,14 +105,12 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
107105
// AddTx should return false if tx is too big or already have it
108106
// tx weight is unknown, we only check when tx is already in orphanage
109107
{
110-
LOCK(g_cs_orphans);
111108
bool add_tx = orphanage.AddTx(tx, peer_id);
112109
// have_tx == true -> add_tx == false
113110
Assert(!have_tx || !add_tx);
114111
}
115112
have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash()));
116113
{
117-
LOCK(g_cs_orphans);
118114
bool add_tx = orphanage.AddTx(tx, peer_id);
119115
// if have_tx is still false, it must be too big
120116
Assert(!have_tx == (GetTransactionWeight(*tx) > MAX_STANDARD_TX_WEIGHT));
@@ -125,25 +121,22 @@ FUZZ_TARGET_INIT(txorphan, initialize_orphanage)
125121
bool have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash()));
126122
// EraseTx should return 0 if m_orphans doesn't have the tx
127123
{
128-
LOCK(g_cs_orphans);
129124
Assert(have_tx == orphanage.EraseTx(tx->GetHash()));
130125
}
131126
have_tx = orphanage.HaveTx(GenTxid::Txid(tx->GetHash())) || orphanage.HaveTx(GenTxid::Wtxid(tx->GetHash()));
132127
// have_tx should be false and EraseTx should fail
133128
{
134-
LOCK(g_cs_orphans);
135129
Assert(!have_tx && !orphanage.EraseTx(tx->GetHash()));
136130
}
137131
},
138132
[&] {
139-
LOCK(g_cs_orphans);
140133
orphanage.EraseForPeer(peer_id);
141134
},
142135
[&] {
143136
// test mocktime and expiry
144137
SetMockTime(ConsumeTime(fuzzed_data_provider));
145138
auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
146-
WITH_LOCK(g_cs_orphans, orphanage.LimitOrphans(limit));
139+
orphanage.LimitOrphans(limit);
147140
Assert(orphanage.Size() <= limit);
148141
});
149142
}

src/test/orphanage_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
2020
class TxOrphanageTest : public TxOrphanage
2121
{
2222
public:
23-
inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
23+
inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans)
2424
{
25+
LOCK(g_cs_orphans);
2526
return m_orphans.size();
2627
}
2728

28-
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans)
29+
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans)
2930
{
31+
LOCK(g_cs_orphans);
3032
std::map<uint256, OrphanTx>::iterator it;
3133
it = m_orphans.lower_bound(InsecureRand256());
3234
if (it == m_orphans.end())
@@ -59,8 +61,6 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
5961
FillableSigningProvider keystore;
6062
BOOST_CHECK(keystore.AddKey(key));
6163

62-
LOCK(g_cs_orphans);
63-
6464
// 50 orphan transactions:
6565
for (int i = 0; i < 50; i++)
6666
{

src/txorphanage.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
1515
/** Minimum time between orphan transactions expire time checks in seconds */
1616
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
1717

18-
RecursiveMutex g_cs_orphans;
18+
RecursiveMutex TxOrphanage::g_cs_orphans;
1919

2020
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
2121
{
22-
AssertLockHeld(g_cs_orphans);
22+
LOCK(g_cs_orphans);
2323

2424
const uint256& hash = tx->GetHash();
2525
if (m_orphans.count(hash))
@@ -54,6 +54,12 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
5454
}
5555

5656
int TxOrphanage::EraseTx(const uint256& txid)
57+
{
58+
LOCK(g_cs_orphans);
59+
return _EraseTx(txid);
60+
}
61+
62+
int TxOrphanage::_EraseTx(const uint256& txid)
5763
{
5864
AssertLockHeld(g_cs_orphans);
5965
std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
@@ -87,7 +93,7 @@ int TxOrphanage::EraseTx(const uint256& txid)
8793

8894
void TxOrphanage::EraseForPeer(NodeId peer)
8995
{
90-
AssertLockHeld(g_cs_orphans);
96+
LOCK(g_cs_orphans);
9197

9298
m_peer_work_set.erase(peer);
9399

@@ -98,15 +104,15 @@ void TxOrphanage::EraseForPeer(NodeId peer)
98104
std::map<uint256, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
99105
if (maybeErase->second.fromPeer == peer)
100106
{
101-
nErased += EraseTx(maybeErase->second.tx->GetHash());
107+
nErased += _EraseTx(maybeErase->second.tx->GetHash());
102108
}
103109
}
104110
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
105111
}
106112

107113
void TxOrphanage::LimitOrphans(unsigned int max_orphans)
108114
{
109-
AssertLockHeld(g_cs_orphans);
115+
LOCK(g_cs_orphans);
110116

111117
unsigned int nEvicted = 0;
112118
static int64_t nNextSweep;
@@ -120,7 +126,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
120126
{
121127
std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
122128
if (maybeErase->second.nTimeExpire <= nNow) {
123-
nErased += EraseTx(maybeErase->second.tx->GetHash());
129+
nErased += _EraseTx(maybeErase->second.tx->GetHash());
124130
} else {
125131
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
126132
}
@@ -134,15 +140,15 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
134140
{
135141
// Evict a random orphan:
136142
size_t randompos = rng.randrange(m_orphan_list.size());
137-
EraseTx(m_orphan_list[randompos]->first);
143+
_EraseTx(m_orphan_list[randompos]->first);
138144
++nEvicted;
139145
}
140146
if (nEvicted > 0) LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
141147
}
142148

143149
void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx, NodeId peer)
144150
{
145-
AssertLockHeld(g_cs_orphans);
151+
LOCK(g_cs_orphans);
146152

147153
// Get this peer's work set, emplacing an empty set it didn't exist
148154
std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(peer).first->second;
@@ -169,7 +175,7 @@ bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
169175

170176
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, NodeId& originator, bool& more)
171177
{
172-
AssertLockHeld(g_cs_orphans);
178+
LOCK(g_cs_orphans);
173179

174180
auto work_set_it = m_peer_work_set.find(peer);
175181
if (work_set_it != m_peer_work_set.end()) {
@@ -215,7 +221,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
215221
if (vOrphanErase.size()) {
216222
int nErased = 0;
217223
for (const uint256& orphanHash : vOrphanErase) {
218-
nErased += EraseTx(orphanHash);
224+
nErased += _EraseTx(orphanHash);
219225
}
220226
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
221227
}

src/txorphanage.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include <map>
1414
#include <set>
1515

16-
/** Guards orphan transactions */
17-
extern RecursiveMutex g_cs_orphans;
18-
1916
/** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
2017
* Since we cannot distinguish orphans from bad transactions with
2118
* non-existent inputs, we heavily limit the number of orphans
@@ -24,10 +21,10 @@ extern RecursiveMutex g_cs_orphans;
2421
class TxOrphanage {
2522
public:
2623
/** Add a new orphan transaction */
27-
bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
24+
bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
2825

2926
/** Check if we already have an orphan transaction (by txid or wtxid) */
30-
bool HaveTx(const GenTxid& gtxid) const LOCKS_EXCLUDED(::g_cs_orphans);
27+
bool HaveTx(const GenTxid& gtxid) const EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
3128

3229
/** Extract a transaction from a peer's work set
3330
* Returns nullptr and sets more to false if there are no transactions
@@ -36,31 +33,34 @@ class TxOrphanage {
3633
* the originating peer, and whether there are more orphans for this peer
3734
* to work on after this tx.
3835
*/
39-
CTransactionRef GetTxToReconsider(NodeId peer, NodeId& originator, bool& more) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
36+
CTransactionRef GetTxToReconsider(NodeId peer, NodeId& originator, bool& more) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
4037

4138
/** Erase an orphan by txid */
42-
int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
39+
int EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
4340

4441
/** Erase all orphans announced by a peer (eg, after that peer disconnects) */
45-
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
42+
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
4643

4744
/** Erase all orphans included in or invalidated by a new block */
48-
void EraseForBlock(const CBlock& block) LOCKS_EXCLUDED(::g_cs_orphans);
45+
void EraseForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
4946

5047
/** Limit the orphanage to the given maximum */
51-
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
48+
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
5249

5350
/** Add any orphans that list a particular tx as a parent into a peer's work set */
54-
void AddChildrenToWorkSet(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
51+
void AddChildrenToWorkSet(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans);
5552

5653
/** Return how many entries exist in the orphange */
57-
size_t Size() LOCKS_EXCLUDED(::g_cs_orphans)
54+
size_t Size() EXCLUSIVE_LOCKS_REQUIRED(!g_cs_orphans)
5855
{
59-
LOCK(::g_cs_orphans);
56+
LOCK(g_cs_orphans);
6057
return m_orphans.size();
6158
}
6259

6360
protected:
61+
/** Guards orphan transactions */
62+
static RecursiveMutex g_cs_orphans;
63+
6464
struct OrphanTx {
6565
CTransactionRef tx;
6666
NodeId fromPeer;
@@ -96,6 +96,9 @@ class TxOrphanage {
9696
/** Index from wtxid into the m_orphans to lookup orphan
9797
* transactions using their witness ids. */
9898
std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(g_cs_orphans);
99+
100+
/** Erase an orphan by txid */
101+
int _EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
99102
};
100103

101104
#endif // BITCOIN_TXORPHANAGE_H

0 commit comments

Comments
 (0)