Skip to content

Commit 9f4c0a9

Browse files
author
MarcoFalke
committed
Merge #19347: [net] Make cs_inventory nonrecursive
e8a2822 [net] Don't try to take cs_inventory before deleting CNode (John Newbery) 3556227 [net] Make cs_inventory a non-recursive mutex (John Newbery) 344e831 [net processing] Remove PushBlockInventory and PushBlockHash (John Newbery) Pull request description: - Remove PushBlockInventory() and PushBlockHash(). These are one-line functions that can easy be inlined into the calling code. Doing so also allows us to eliminate the one place that cs_inventory is recursively locked. - Make cs_inventory a nonrecursive mutex - Remove a redundant TRY_LOCK of cs_inventory when deleting CNode. ACKs for top commit: sipa: utACK e8a2822 MarcoFalke: ACK e8a2822 🍬 hebasto: re-ACK e8a2822 Tree-SHA512: dbc721d102cdef7b5827a8f2549daf8b54f543050266999a7ea56c9f36618565b71e31ce0beb1209ba2db43d15388be173355a03fb6db8ad24e2475b145050bd
2 parents abdfd2d + e8a2822 commit 9f4c0a9

File tree

3 files changed

+9
-23
lines changed

3 files changed

+9
-23
lines changed

src/net.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,12 +1110,9 @@ void CConnman::DisconnectNodes()
11101110
if (pnode->GetRefCount() <= 0) {
11111111
bool fDelete = false;
11121112
{
1113-
TRY_LOCK(pnode->cs_inventory, lockInv);
1114-
if (lockInv) {
1115-
TRY_LOCK(pnode->cs_vSend, lockSend);
1116-
if (lockSend) {
1117-
fDelete = true;
1118-
}
1113+
TRY_LOCK(pnode->cs_vSend, lockSend);
1114+
if (lockSend) {
1115+
fDelete = true;
11191116
}
11201117
}
11211118
if (fDelete) {

src/net.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ class CNode
803803
// There is no final sorting before sending, as they are always sent immediately
804804
// and in the order requested.
805805
std::vector<uint256> vInventoryBlockToSend GUARDED_BY(cs_inventory);
806-
RecursiveMutex cs_inventory;
806+
Mutex cs_inventory;
807807

808808
struct TxRelay {
809809
mutable RecursiveMutex cs_filter;
@@ -982,18 +982,6 @@ class CNode
982982
}
983983
}
984984

985-
void PushBlockInventory(const uint256& hash)
986-
{
987-
LOCK(cs_inventory);
988-
vInventoryBlockToSend.push_back(hash);
989-
}
990-
991-
void PushBlockHash(const uint256 &hash)
992-
{
993-
LOCK(cs_inventory);
994-
vBlockHashesToAnnounce.push_back(hash);
995-
}
996-
997985
void CloseSocketDisconnect();
998986

999987
void copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap);

src/net_processing.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,9 +1325,10 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CB
13251325
}
13261326
// Relay inventory, but don't relay old inventory during initial block download.
13271327
connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
1328+
LOCK(pnode->cs_inventory);
13281329
if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
13291330
for (const uint256& hash : reverse_iterate(vHashes)) {
1330-
pnode->PushBlockHash(hash);
1331+
pnode->vBlockHashesToAnnounce.push_back(hash);
13311332
}
13321333
}
13331334
});
@@ -1604,7 +1605,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
16041605
// Trigger the peer node to send a getblocks request for the next batch of inventory
16051606
if (inv.hash == pfrom.hashContinue)
16061607
{
1607-
// Bypass PushBlockInventory, this must send even if redundant,
1608+
// Send immediately. This must send even if redundant,
16081609
// and we want it right after the last block so they don't
16091610
// wait for other stuff first.
16101611
std::vector<CInv> vInv;
@@ -2664,7 +2665,7 @@ void ProcessMessage(
26642665
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
26652666
break;
26662667
}
2667-
pfrom.PushBlockInventory(pindex->GetBlockHash());
2668+
WITH_LOCK(pfrom.cs_inventory, pfrom.vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
26682669
if (--nLimit <= 0)
26692670
{
26702671
// When this block is requested, we'll send an inv that'll
@@ -4082,7 +4083,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
40824083

40834084
// If the peer's chain has this block, don't inv it back.
40844085
if (!PeerHasHeader(&state, pindex)) {
4085-
pto->PushBlockInventory(hashToAnnounce);
4086+
pto->vInventoryBlockToSend.push_back(hashToAnnounce);
40864087
LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
40874088
pto->GetId(), hashToAnnounce.ToString());
40884089
}

0 commit comments

Comments
 (0)