Skip to content

Commit

Permalink
Rename vBlockHashesToAnnounce and vInventoryBlockToSend
Browse files Browse the repository at this point in the history
Summary:
Original rename script:
```
-BEGIN VERIFY SCRIPT-
sed -i 's/vBlockHashesToAnnounce/m_blocks_for_headers_relay/g' src/net_processing.*
sed -i 's/vInventoryBlockToSend/m_blocks_for_inv_relay/g' src/net_processing.*
-END VERIFY SCRIPT-
```

Partial backport of [[bitcoin/bitcoin#19829 | core#19829]]:
bitcoin/bitcoin@c853ef0

Ref T1696.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1696

Differential Revision: https://reviews.bitcoinabc.org/D10870
  • Loading branch information
jnewbery authored and Fabcien committed Jan 24, 2022
1 parent 5586623 commit 81ea1b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew,
Peer &peer = *it.second;
LOCK(peer.m_block_inv_mutex);
for (const BlockHash &hash : reverse_iterate(vHashes)) {
peer.vBlockHashesToAnnounce.push_back(hash);
peer.m_blocks_for_headers_relay.push_back(hash);
}
}
}
Expand Down Expand Up @@ -3525,7 +3525,7 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
}
WITH_LOCK(
peer->m_block_inv_mutex,
peer->vInventoryBlockToSend.push_back(pindex->GetBlockHash()));
peer->m_blocks_for_inv_relay.push_back(pindex->GetBlockHash()));
if (--nLimit <= 0) {
// When this block is requested, we'll send an inv that'll
// trigger the peer to getblocks the next batch of inventory.
Expand Down Expand Up @@ -5392,8 +5392,9 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
bool fRevertToInv =
((!state.fPreferHeaders &&
(!state.fPreferHeaderAndIDs ||
peer->vBlockHashesToAnnounce.size() > 1)) ||
peer->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
peer->m_blocks_for_headers_relay.size() > 1)) ||
peer->m_blocks_for_headers_relay.size() >
MAX_BLOCKS_TO_ANNOUNCE);
// last header queued for delivery
const CBlockIndex *pBestIndex = nullptr;
// ensure pindexBestKnownBlock is up-to-date
Expand All @@ -5404,7 +5405,7 @@ bool PeerManager::SendMessages(const Config &config, 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 an headers
// that aren't on ::ChainActive(), give up.
for (const BlockHash &hash : peer->vBlockHashesToAnnounce) {
for (const BlockHash &hash : peer->m_blocks_for_headers_relay) {
const CBlockIndex *pindex = LookupBlockIndex(hash);
assert(pindex);
if (::ChainActive()[pindex->nHeight] != pindex) {
Expand All @@ -5421,8 +5422,8 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
// prior check), but one way this could happen is by
// using invalidateblock / reconsiderblock repeatedly on
// the tip, causing it to be added multiple times to
// vBlockHashesToAnnounce. Robustly deal with this rare
// situation by reverting to an inv.
// m_blocks_for_headers_relay. Robustly deal with this
// rare situation by reverting to an inv.
fRevertToInv = true;
break;
}
Expand Down Expand Up @@ -5508,11 +5509,11 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
}
if (fRevertToInv) {
// If falling back to using an inv, just try to inv the tip. The
// last entry in vBlockHashesToAnnounce was our tip at some
// last entry in m_blocks_for_headers_relay was our tip at some
// point in the past.
if (!peer->vBlockHashesToAnnounce.empty()) {
if (!peer->m_blocks_for_headers_relay.empty()) {
const BlockHash &hashToAnnounce =
peer->vBlockHashesToAnnounce.back();
peer->m_blocks_for_headers_relay.back();
const CBlockIndex *pindex =
LookupBlockIndex(hashToAnnounce);
assert(pindex);
Expand All @@ -5530,14 +5531,14 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,

// If the peer's chain has this block, don't inv it back.
if (!PeerHasHeader(&state, pindex)) {
peer->vInventoryBlockToSend.push_back(hashToAnnounce);
peer->m_blocks_for_inv_relay.push_back(hashToAnnounce);
LogPrint(BCLog::NET,
"%s: sending inv peer=%d hash=%s\n", __func__,
pto->GetId(), hashToAnnounce.ToString());
}
}
}
peer->vBlockHashesToAnnounce.clear();
peer->m_blocks_for_headers_relay.clear();
}
} // release cs_main

Expand All @@ -5557,15 +5558,15 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
{
LOCK2(cs_main, peer->m_block_inv_mutex);

vInv.reserve(std::max<size_t>(peer->vInventoryBlockToSend.size(),
vInv.reserve(std::max<size_t>(peer->m_blocks_for_inv_relay.size(),
INVENTORY_BROADCAST_MAX_PER_MB *
config.GetMaxBlockSize() / 1000000));

// Add blocks
for (const BlockHash &hash : peer->vInventoryBlockToSend) {
for (const BlockHash &hash : peer->m_blocks_for_inv_relay) {
addInvAndMaybeFlush(MSG_BLOCK, hash);
}
peer->vInventoryBlockToSend.clear();
peer->m_blocks_for_inv_relay.clear();

auto computeNextInvSendTime =
[&](std::chrono::microseconds &next) -> bool {
Expand Down
5 changes: 3 additions & 2 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ struct Peer {
* There is no final sorting before sending, as they are always sent
* immediately and in the order requested.
*/
std::vector<BlockHash> vInventoryBlockToSend GUARDED_BY(m_block_inv_mutex);
std::vector<BlockHash> m_blocks_for_inv_relay GUARDED_BY(m_block_inv_mutex);
/**
* Unfiltered list of blocks that we'd like to announce via a `headers`
* message. If we can't announce via a `headers` message, we'll fall back to
* announcing via `inv`.
*/
std::vector<BlockHash> vBlockHashesToAnnounce GUARDED_BY(m_block_inv_mutex);
std::vector<BlockHash>
m_blocks_for_headers_relay GUARDED_BY(m_block_inv_mutex);

/** This peer's reported block height when we connected */
std::atomic<int> m_starting_height{-1};
Expand Down

0 comments on commit 81ea1b1

Please sign in to comment.