Skip to content

Commit 53b7ac1

Browse files
committed
[net processing] Move block inventory data to Peer
1 parent 78040f9 commit 53b7ac1

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

src/net.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,6 @@ class CNode
10061006
std::chrono::microseconds m_next_addr_send GUARDED_BY(cs_sendProcessing){0};
10071007
std::chrono::microseconds m_next_local_addr_send GUARDED_BY(cs_sendProcessing){0};
10081008

1009-
// List of block ids we still have announce.
1010-
// There is no final sorting before sending, as they are always sent immediately
1011-
// and in the order requested.
1012-
std::vector<uint256> vInventoryBlockToSend GUARDED_BY(cs_inventory);
1013-
Mutex cs_inventory;
1014-
10151009
struct TxRelay {
10161010
mutable RecursiveMutex cs_filter;
10171011
// We use fRelayTxes for two purposes -
@@ -1042,9 +1036,6 @@ class CNode
10421036
// m_tx_relay == nullptr if we're not relaying transactions with this peer
10431037
std::unique_ptr<TxRelay> m_tx_relay;
10441038

1045-
// Used for headers announcements - unfiltered blocks to relay
1046-
std::vector<uint256> vBlockHashesToAnnounce GUARDED_BY(cs_inventory);
1047-
10481039
/** UNIX epoch time of the last block received from this peer that we had
10491040
* not yet seen (e.g. not already received from another peer), that passed
10501041
* preliminary validity checks and was saved to disk, even if we don't

src/net_processing.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,13 +1315,17 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInde
13151315
}
13161316
}
13171317

1318-
// Relay to all peers
1319-
m_connman.ForEachNode([&vHashes](CNode* pnode) {
1320-
LOCK(pnode->cs_inventory);
1321-
for (const uint256& hash : reverse_iterate(vHashes)) {
1322-
pnode->vBlockHashesToAnnounce.push_back(hash);
1318+
{
1319+
LOCK(m_peer_mutex);
1320+
for (auto& it : m_peer_map) {
1321+
Peer& peer = *it.second;
1322+
LOCK(peer.m_block_inv_mutex);
1323+
for (const uint256& hash : reverse_iterate(vHashes)) {
1324+
peer.vBlockHashesToAnnounce.push_back(hash);
1325+
}
13231326
}
1324-
});
1327+
}
1328+
13251329
m_connman.WakeMessageHandler();
13261330
}
13271331

@@ -2795,7 +2799,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
27952799
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
27962800
break;
27972801
}
2798-
WITH_LOCK(pfrom.cs_inventory, pfrom.vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
2802+
WITH_LOCK(peer->m_block_inv_mutex, peer->vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
27992803
if (--nLimit <= 0)
28002804
{
28012805
// When this block is requested, we'll send an inv that'll
@@ -4218,11 +4222,11 @@ bool PeerManager::SendMessages(CNode* pto)
42184222
// If no header would connect, or if we have too many
42194223
// blocks, or if the peer doesn't want headers, just
42204224
// add all to the inv queue.
4221-
LOCK(pto->cs_inventory);
4225+
LOCK(peer->m_block_inv_mutex);
42224226
std::vector<CBlock> vHeaders;
42234227
bool fRevertToInv = ((!state.fPreferHeaders &&
4224-
(!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
4225-
pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
4228+
(!state.fPreferHeaderAndIDs || peer->vBlockHashesToAnnounce.size() > 1)) ||
4229+
peer->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
42264230
const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
42274231
ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
42284232

@@ -4231,7 +4235,7 @@ bool PeerManager::SendMessages(CNode* pto)
42314235
// Try to find first header that our peer doesn't have, and
42324236
// then send all headers past that one. If we come across any
42334237
// headers that aren't on ::ChainActive(), give up.
4234-
for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
4238+
for (const uint256& hash : peer->vBlockHashesToAnnounce) {
42354239
const CBlockIndex* pindex = LookupBlockIndex(hash);
42364240
assert(pindex);
42374241
if (::ChainActive()[pindex->nHeight] != pindex) {
@@ -4322,8 +4326,8 @@ bool PeerManager::SendMessages(CNode* pto)
43224326
// If falling back to using an inv, just try to inv the tip.
43234327
// The last entry in vBlockHashesToAnnounce was our tip at some point
43244328
// in the past.
4325-
if (!pto->vBlockHashesToAnnounce.empty()) {
4326-
const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
4329+
if (!peer->vBlockHashesToAnnounce.empty()) {
4330+
const uint256& hashToAnnounce = peer->vBlockHashesToAnnounce.back();
43274331
const CBlockIndex* pindex = LookupBlockIndex(hashToAnnounce);
43284332
assert(pindex);
43294333

@@ -4337,32 +4341,32 @@ bool PeerManager::SendMessages(CNode* pto)
43374341

43384342
// If the peer's chain has this block, don't inv it back.
43394343
if (!PeerHasHeader(&state, pindex)) {
4340-
pto->vInventoryBlockToSend.push_back(hashToAnnounce);
4344+
peer->vInventoryBlockToSend.push_back(hashToAnnounce);
43414345
LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
43424346
pto->GetId(), hashToAnnounce.ToString());
43434347
}
43444348
}
43454349
}
4346-
pto->vBlockHashesToAnnounce.clear();
4350+
peer->vBlockHashesToAnnounce.clear();
43474351
}
43484352

43494353
//
43504354
// Message: inventory
43514355
//
43524356
std::vector<CInv> vInv;
43534357
{
4354-
LOCK(pto->cs_inventory);
4355-
vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
4358+
LOCK(peer->m_block_inv_mutex);
4359+
vInv.reserve(std::max<size_t>(peer->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
43564360

43574361
// Add blocks
4358-
for (const uint256& hash : pto->vInventoryBlockToSend) {
4362+
for (const uint256& hash : peer->vInventoryBlockToSend) {
43594363
vInv.push_back(CInv(MSG_BLOCK, hash));
43604364
if (vInv.size() == MAX_INV_SZ) {
43614365
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
43624366
vInv.clear();
43634367
}
43644368
}
4365-
pto->vInventoryBlockToSend.clear();
4369+
peer->vInventoryBlockToSend.clear();
43664370

43674371
if (pto->m_tx_relay != nullptr) {
43684372
LOCK(pto->m_tx_relay->cs_tx_inventory);

src/net_processing.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ struct Peer {
6363
/** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
6464
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
6565

66+
/** Protects block inventory data members */
67+
Mutex m_block_inv_mutex;
68+
/** List of blocks that we'll anounce via an `inv` message.
69+
* There is no final sorting before sending, as they are always sent
70+
* immediately and in the order requested. */
71+
std::vector<uint256> vInventoryBlockToSend GUARDED_BY(m_block_inv_mutex);
72+
/** Unfiltered list of blocks that we'd like to announce via a `headers`
73+
* message. If we can't announce via a `headers` message, we'll fall back to
74+
* announcing via `inv`. */
75+
std::vector<uint256> vBlockHashesToAnnounce GUARDED_BY(m_block_inv_mutex);
76+
6677
/** This peer's reported block height when we connected */
6778
std::atomic<int> m_starting_height{-1};
6879

0 commit comments

Comments
 (0)