Skip to content

Commit 5586623

Browse files
jnewberyFabcien
authored andcommitted
[net processing] Move block inventory data to Peer
Summary: Partial backport of [[bitcoin/bitcoin#19829 | core#19829]]: bitcoin/bitcoin@53b7ac1 Ref T1696. Depends on D10867. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Maniphest Tasks: T1696 Differential Revision: https://reviews.bitcoinabc.org/D10868
1 parent a694ef7 commit 5586623

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

src/net.h

-9
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,6 @@ class CNode {
11061106
std::chrono::microseconds
11071107
m_next_local_addr_send GUARDED_BY(cs_sendProcessing){0};
11081108

1109-
// List of block ids we still have to announce.
1110-
// There is no final sorting before sending, as they are always sent
1111-
// immediately and in the order requested.
1112-
std::vector<BlockHash> vInventoryBlockToSend GUARDED_BY(cs_inventory);
1113-
Mutex cs_inventory;
1114-
11151109
struct TxRelay {
11161110
mutable RecursiveMutex cs_filter;
11171111
// We use fRelayTxes for two purposes -
@@ -1212,9 +1206,6 @@ class CNode {
12121206
// m_avalanche_state == nullptr if we're not using avalanche with this peer
12131207
std::unique_ptr<AvalancheState> m_avalanche_state;
12141208

1215-
// Used for headers announcements - unfiltered blocks to relay
1216-
std::vector<BlockHash> vBlockHashesToAnnounce GUARDED_BY(cs_inventory);
1217-
12181209
/**
12191210
* UNIX epoch time of the last block received from this peer that we had
12201211
* not yet seen (e.g. not already received from another peer), that passed

src/net_processing.cpp

+25-20
Original file line numberDiff line numberDiff line change
@@ -1722,13 +1722,17 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew,
17221722
}
17231723
}
17241724

1725-
// Relay to all peers
1726-
m_connman.ForEachNode([&vHashes](CNode *pnode) {
1727-
LOCK(pnode->cs_inventory);
1728-
for (const BlockHash &hash : reverse_iterate(vHashes)) {
1729-
pnode->vBlockHashesToAnnounce.push_back(hash);
1725+
{
1726+
LOCK(m_peer_mutex);
1727+
for (auto &it : m_peer_map) {
1728+
Peer &peer = *it.second;
1729+
LOCK(peer.m_block_inv_mutex);
1730+
for (const BlockHash &hash : reverse_iterate(vHashes)) {
1731+
peer.vBlockHashesToAnnounce.push_back(hash);
1732+
}
17301733
}
1731-
});
1734+
}
1735+
17321736
m_connman.WakeMessageHandler();
17331737
}
17341738

@@ -3519,8 +3523,9 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
35193523
pindex->nHeight, pindex->GetBlockHash().ToString());
35203524
break;
35213525
}
3522-
WITH_LOCK(pfrom.cs_inventory, pfrom.vInventoryBlockToSend.push_back(
3523-
pindex->GetBlockHash()));
3526+
WITH_LOCK(
3527+
peer->m_block_inv_mutex,
3528+
peer->vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
35243529
if (--nLimit <= 0) {
35253530
// When this block is requested, we'll send an inv that'll
35263531
// trigger the peer to getblocks the next batch of inventory.
@@ -5382,13 +5387,13 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
53825387
// connect, and send. If no header would connect, or if we have too
53835388
// many blocks, or if the peer doesn't want headers, just add all to
53845389
// the inv queue.
5385-
LOCK(pto->cs_inventory);
5390+
LOCK(peer->m_block_inv_mutex);
53865391
std::vector<CBlock> vHeaders;
53875392
bool fRevertToInv =
53885393
((!state.fPreferHeaders &&
53895394
(!state.fPreferHeaderAndIDs ||
5390-
pto->vBlockHashesToAnnounce.size() > 1)) ||
5391-
pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
5395+
peer->vBlockHashesToAnnounce.size() > 1)) ||
5396+
peer->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
53925397
// last header queued for delivery
53935398
const CBlockIndex *pBestIndex = nullptr;
53945399
// ensure pindexBestKnownBlock is up-to-date
@@ -5399,7 +5404,7 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
53995404
// Try to find first header that our peer doesn't have, and then
54005405
// send all headers past that one. If we come across an headers
54015406
// that aren't on ::ChainActive(), give up.
5402-
for (const BlockHash &hash : pto->vBlockHashesToAnnounce) {
5407+
for (const BlockHash &hash : peer->vBlockHashesToAnnounce) {
54035408
const CBlockIndex *pindex = LookupBlockIndex(hash);
54045409
assert(pindex);
54055410
if (::ChainActive()[pindex->nHeight] != pindex) {
@@ -5505,9 +5510,9 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
55055510
// If falling back to using an inv, just try to inv the tip. The
55065511
// last entry in vBlockHashesToAnnounce was our tip at some
55075512
// point in the past.
5508-
if (!pto->vBlockHashesToAnnounce.empty()) {
5513+
if (!peer->vBlockHashesToAnnounce.empty()) {
55095514
const BlockHash &hashToAnnounce =
5510-
pto->vBlockHashesToAnnounce.back();
5515+
peer->vBlockHashesToAnnounce.back();
55115516
const CBlockIndex *pindex =
55125517
LookupBlockIndex(hashToAnnounce);
55135518
assert(pindex);
@@ -5525,14 +5530,14 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
55255530

55265531
// If the peer's chain has this block, don't inv it back.
55275532
if (!PeerHasHeader(&state, pindex)) {
5528-
pto->vInventoryBlockToSend.push_back(hashToAnnounce);
5533+
peer->vInventoryBlockToSend.push_back(hashToAnnounce);
55295534
LogPrint(BCLog::NET,
55305535
"%s: sending inv peer=%d hash=%s\n", __func__,
55315536
pto->GetId(), hashToAnnounce.ToString());
55325537
}
55335538
}
55345539
}
5535-
pto->vBlockHashesToAnnounce.clear();
5540+
peer->vBlockHashesToAnnounce.clear();
55365541
}
55375542
} // release cs_main
55385543

@@ -5550,17 +5555,17 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
55505555
};
55515556

55525557
{
5553-
LOCK2(cs_main, pto->cs_inventory);
5558+
LOCK2(cs_main, peer->m_block_inv_mutex);
55545559

5555-
vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(),
5560+
vInv.reserve(std::max<size_t>(peer->vInventoryBlockToSend.size(),
55565561
INVENTORY_BROADCAST_MAX_PER_MB *
55575562
config.GetMaxBlockSize() / 1000000));
55585563

55595564
// Add blocks
5560-
for (const BlockHash &hash : pto->vInventoryBlockToSend) {
5565+
for (const BlockHash &hash : peer->vInventoryBlockToSend) {
55615566
addInvAndMaybeFlush(MSG_BLOCK, hash);
55625567
}
5563-
pto->vInventoryBlockToSend.clear();
5568+
peer->vInventoryBlockToSend.clear();
55645569

55655570
auto computeNextInvSendTime =
55665571
[&](std::chrono::microseconds &next) -> bool {

src/net_processing.h

+15
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ struct Peer {
7575
* (unless it has the noban permission). */
7676
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
7777

78+
/** Protects block inventory data members */
79+
Mutex m_block_inv_mutex;
80+
/**
81+
* List of blocks that we'll anounce via an `inv` message.
82+
* There is no final sorting before sending, as they are always sent
83+
* immediately and in the order requested.
84+
*/
85+
std::vector<BlockHash> vInventoryBlockToSend GUARDED_BY(m_block_inv_mutex);
86+
/**
87+
* Unfiltered list of blocks that we'd like to announce via a `headers`
88+
* message. If we can't announce via a `headers` message, we'll fall back to
89+
* announcing via `inv`.
90+
*/
91+
std::vector<BlockHash> vBlockHashesToAnnounce GUARDED_BY(m_block_inv_mutex);
92+
7893
/** This peer's reported block height when we connected */
7994
std::atomic<int> m_starting_height{-1};
8095

0 commit comments

Comments
 (0)