Skip to content

Commit eec9366

Browse files
author
MarcoFalke
committed
Merge #20624: net processing: Remove nStartingHeight check from block relay
f636008 [net processing] Clarify UpdatedBlockTip() (John Newbery) 94d2cc3 [net processing] Remove unnecesary nNewHeight variable in UpdatedBlockTip() (John Newbery) 8b57013 [net processing] Remove nStartingHeight check from block relay (John Newbery) Pull request description: nStartingHeight was introduced in commit 7a47324 (Bitcoin version 0.2.9, P2P version 209) with the comment "better prevention of inventory relaying during initial download". At that time, there was no function to determine whether the node was still in Initial Block Download, so to prevent syncing nodes from relaying old blocks to their peers, a check was added to never relay a block to a peer where the height was lower than 2000 less than the peer's best block. That check was updated several times in later commits to ensure that we weren't relaying blocks before the latest checkpoint if the peer didn't provide a startingheight. The checkpoint comparison was changed to compare with an estimate of the highest block in commit eae82d8. In commit 202e019, all block relay was gated on being out of Initial Block Download. In commit 0278fb5, the comparison to nBlockEstimate was removed since "we already checked IsIBD()". We can remove the check against nStartingHeight entirely. If the node is out of Initial Block Download, then its tip height must have been within 24 hours of current time, so should not be more than ~144 blocks behind the most work tip. This simplifies moving block inventory state into the `Peer` object (#19829). ACKs for top commit: Sjors: utACK f636008 jonatack: ACK f636008 MarcoFalke: ACK f636008 💽 ariard: Code Review ACK f636008 Tree-SHA512: 4959cf35f1dcde46f34bffec1375729a157e1b2a1fd8a8ca33da9771c3c89a6c43e7050cdeeab8d90bb507b0795703db8c8bc304a1a5065ef00aae7a6992ca4f
2 parents 096bd37 + f636008 commit eec9366

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/net_processing.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,34 +1290,33 @@ void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_
12901290
* in ::ChainActive() to our peers.
12911291
*/
12921292
void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
1293-
const int nNewHeight = pindexNew->nHeight;
1294-
m_connman.SetBestHeight(nNewHeight);
1295-
1293+
m_connman.SetBestHeight(pindexNew->nHeight);
12961294
SetServiceFlagsIBDCache(!fInitialDownload);
1297-
if (!fInitialDownload) {
1298-
// Find the hashes of all blocks that weren't previously in the best chain.
1299-
std::vector<uint256> vHashes;
1300-
const CBlockIndex *pindexToAnnounce = pindexNew;
1301-
while (pindexToAnnounce != pindexFork) {
1302-
vHashes.push_back(pindexToAnnounce->GetBlockHash());
1303-
pindexToAnnounce = pindexToAnnounce->pprev;
1304-
if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
1305-
// Limit announcements in case of a huge reorganization.
1306-
// Rely on the peer's synchronization mechanism in that case.
1307-
break;
1308-
}
1295+
1296+
// Don't relay inventory during initial block download.
1297+
if (fInitialDownload) return;
1298+
1299+
// Find the hashes of all blocks that weren't previously in the best chain.
1300+
std::vector<uint256> vHashes;
1301+
const CBlockIndex *pindexToAnnounce = pindexNew;
1302+
while (pindexToAnnounce != pindexFork) {
1303+
vHashes.push_back(pindexToAnnounce->GetBlockHash());
1304+
pindexToAnnounce = pindexToAnnounce->pprev;
1305+
if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
1306+
// Limit announcements in case of a huge reorganization.
1307+
// Rely on the peer's synchronization mechanism in that case.
1308+
break;
13091309
}
1310-
// Relay inventory, but don't relay old inventory during initial block download.
1311-
m_connman.ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
1312-
LOCK(pnode->cs_inventory);
1313-
if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
1314-
for (const uint256& hash : reverse_iterate(vHashes)) {
1315-
pnode->vBlockHashesToAnnounce.push_back(hash);
1316-
}
1317-
}
1318-
});
1319-
m_connman.WakeMessageHandler();
13201310
}
1311+
1312+
// Relay to all peers
1313+
m_connman.ForEachNode([&vHashes](CNode* pnode) {
1314+
LOCK(pnode->cs_inventory);
1315+
for (const uint256& hash : reverse_iterate(vHashes)) {
1316+
pnode->vBlockHashesToAnnounce.push_back(hash);
1317+
}
1318+
});
1319+
m_connman.WakeMessageHandler();
13211320
}
13221321

13231322
/**

0 commit comments

Comments
 (0)