aboutsummaryrefslogtreecommitdiff
path: root/src/net_processing.cpp
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2020-06-19 13:29:05 -0400
committerJohn Newbery <john@johnnewbery.com>2020-12-20 10:01:48 +0000
commit53b7ac1b7d3394aeaad1e4a6e3b323d17cdf5994 (patch)
tree9bcabdd4081079e069133d84897c8f3593bfb384 /src/net_processing.cpp
parent78040f91687e7f1986e466d448c9b9530830e9b8 (diff)
[net processing] Move block inventory data to Peer
Diffstat (limited to 'src/net_processing.cpp')
-rw-r--r--src/net_processing.cpp42
1 files changed, 23 insertions, 19 deletions
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index ee049b0339..8f9eb62100 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -1315,13 +1315,17 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInde
}
}
- // Relay to all peers
- m_connman.ForEachNode([&vHashes](CNode* pnode) {
- LOCK(pnode->cs_inventory);
- for (const uint256& hash : reverse_iterate(vHashes)) {
- pnode->vBlockHashesToAnnounce.push_back(hash);
+ {
+ LOCK(m_peer_mutex);
+ for (auto& it : m_peer_map) {
+ Peer& peer = *it.second;
+ LOCK(peer.m_block_inv_mutex);
+ for (const uint256& hash : reverse_iterate(vHashes)) {
+ peer.vBlockHashesToAnnounce.push_back(hash);
+ }
}
- });
+ }
+
m_connman.WakeMessageHandler();
}
@@ -2795,7 +2799,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
break;
}
- WITH_LOCK(pfrom.cs_inventory, pfrom.vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
+ WITH_LOCK(peer->m_block_inv_mutex, peer->vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
if (--nLimit <= 0)
{
// When this block is requested, we'll send an inv that'll
@@ -4218,11 +4222,11 @@ bool PeerManager::SendMessages(CNode* pto)
// If no header would connect, or if we have too many
// blocks, or if the peer doesn't want headers, just
// add all to the inv queue.
- LOCK(pto->cs_inventory);
+ LOCK(peer->m_block_inv_mutex);
std::vector<CBlock> vHeaders;
bool fRevertToInv = ((!state.fPreferHeaders &&
- (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
- pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
+ (!state.fPreferHeaderAndIDs || peer->vBlockHashesToAnnounce.size() > 1)) ||
+ peer->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
@@ -4231,7 +4235,7 @@ bool PeerManager::SendMessages(CNode* pto)
// Try to find first header that our peer doesn't have, and
// then send all headers past that one. If we come across any
// headers that aren't on ::ChainActive(), give up.
- for (const uint256 &hash : pto->vBlockHashesToAnnounce) {
+ for (const uint256& hash : peer->vBlockHashesToAnnounce) {
const CBlockIndex* pindex = LookupBlockIndex(hash);
assert(pindex);
if (::ChainActive()[pindex->nHeight] != pindex) {
@@ -4322,8 +4326,8 @@ bool PeerManager::SendMessages(CNode* pto)
// If falling back to using an inv, just try to inv the tip.
// The last entry in vBlockHashesToAnnounce was our tip at some point
// in the past.
- if (!pto->vBlockHashesToAnnounce.empty()) {
- const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
+ if (!peer->vBlockHashesToAnnounce.empty()) {
+ const uint256& hashToAnnounce = peer->vBlockHashesToAnnounce.back();
const CBlockIndex* pindex = LookupBlockIndex(hashToAnnounce);
assert(pindex);
@@ -4337,13 +4341,13 @@ bool PeerManager::SendMessages(CNode* pto)
// If the peer's chain has this block, don't inv it back.
if (!PeerHasHeader(&state, pindex)) {
- pto->vInventoryBlockToSend.push_back(hashToAnnounce);
+ peer->vInventoryBlockToSend.push_back(hashToAnnounce);
LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
pto->GetId(), hashToAnnounce.ToString());
}
}
}
- pto->vBlockHashesToAnnounce.clear();
+ peer->vBlockHashesToAnnounce.clear();
}
//
@@ -4351,18 +4355,18 @@ bool PeerManager::SendMessages(CNode* pto)
//
std::vector<CInv> vInv;
{
- LOCK(pto->cs_inventory);
- vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
+ LOCK(peer->m_block_inv_mutex);
+ vInv.reserve(std::max<size_t>(peer->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
// Add blocks
- for (const uint256& hash : pto->vInventoryBlockToSend) {
+ for (const uint256& hash : peer->vInventoryBlockToSend) {
vInv.push_back(CInv(MSG_BLOCK, hash));
if (vInv.size() == MAX_INV_SZ) {
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
vInv.clear();
}
}
- pto->vInventoryBlockToSend.clear();
+ peer->vInventoryBlockToSend.clear();
if (pto->m_tx_relay != nullptr) {
LOCK(pto->m_tx_relay->cs_tx_inventory);