From 8b57013473c44fc87c9f1c4611224a89187c289a Mon Sep 17 00:00:00 2001 From: John Newbery Date: Fri, 11 Dec 2020 10:08:37 +0000 Subject: [PATCH 1/3] [net processing] Remove nStartingHeight check from block relay nStartingHeight was introduced in commit 7a47324c7 (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 eae82d8e. In commit 202e0194, all block relay was gated on being out of Initial Block Download. In commit 0278fb5f, 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. --- src/net_processing.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 200c286f5d..6e9d8119ab 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1295,8 +1295,9 @@ void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) { const int nNewHeight = pindexNew->nHeight; m_connman.SetBestHeight(nNewHeight); - SetServiceFlagsIBDCache(!fInitialDownload); + + // Relay inventory, but don't relay old inventory during initial block download. if (!fInitialDownload) { // Find the hashes of all blocks that weren't previously in the best chain. std::vector vHashes; @@ -1310,13 +1311,10 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInde break; } } - // Relay inventory, but don't relay old inventory during initial block download. m_connman.ForEachNode([nNewHeight, &vHashes](CNode* pnode) { LOCK(pnode->cs_inventory); - if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) { - for (const uint256& hash : reverse_iterate(vHashes)) { - pnode->vBlockHashesToAnnounce.push_back(hash); - } + for (const uint256& hash : reverse_iterate(vHashes)) { + pnode->vBlockHashesToAnnounce.push_back(hash); } }); m_connman.WakeMessageHandler(); From 94d2cc35be98d3b20db88b2a3745322e5b0aa9d4 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Fri, 11 Dec 2020 10:35:32 +0000 Subject: [PATCH 2/3] [net processing] Remove unnecesary nNewHeight variable in UpdatedBlockTip() --- src/net_processing.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 6e9d8119ab..acacb7c592 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1293,8 +1293,7 @@ void PeerManager::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ * in ::ChainActive() to our peers. */ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) { - const int nNewHeight = pindexNew->nHeight; - m_connman.SetBestHeight(nNewHeight); + m_connman.SetBestHeight(pindexNew->nHeight); SetServiceFlagsIBDCache(!fInitialDownload); // Relay inventory, but don't relay old inventory during initial block download. @@ -1311,7 +1310,7 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInde break; } } - m_connman.ForEachNode([nNewHeight, &vHashes](CNode* pnode) { + m_connman.ForEachNode([&vHashes](CNode* pnode) { LOCK(pnode->cs_inventory); for (const uint256& hash : reverse_iterate(vHashes)) { pnode->vBlockHashesToAnnounce.push_back(hash); From f6360088de8ca02f9d198da2f8cca4ea8d64c992 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Fri, 11 Dec 2020 10:40:19 +0000 Subject: [PATCH 3/3] [net processing] Clarify UpdatedBlockTip() --- src/net_processing.cpp | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index acacb7c592..7947470ba0 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1296,28 +1296,30 @@ void PeerManager::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockInde m_connman.SetBestHeight(pindexNew->nHeight); SetServiceFlagsIBDCache(!fInitialDownload); - // Relay inventory, but don't relay old inventory during initial block download. - if (!fInitialDownload) { - // Find the hashes of all blocks that weren't previously in the best chain. - std::vector vHashes; - const CBlockIndex *pindexToAnnounce = pindexNew; - while (pindexToAnnounce != pindexFork) { - vHashes.push_back(pindexToAnnounce->GetBlockHash()); - pindexToAnnounce = pindexToAnnounce->pprev; - if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) { - // Limit announcements in case of a huge reorganization. - // Rely on the peer's synchronization mechanism in that case. - break; - } + // Don't relay inventory during initial block download. + if (fInitialDownload) return; + + // Find the hashes of all blocks that weren't previously in the best chain. + std::vector vHashes; + const CBlockIndex *pindexToAnnounce = pindexNew; + while (pindexToAnnounce != pindexFork) { + vHashes.push_back(pindexToAnnounce->GetBlockHash()); + pindexToAnnounce = pindexToAnnounce->pprev; + if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) { + // Limit announcements in case of a huge reorganization. + // Rely on the peer's synchronization mechanism in that case. + break; } - m_connman.ForEachNode([&vHashes](CNode* pnode) { - LOCK(pnode->cs_inventory); - for (const uint256& hash : reverse_iterate(vHashes)) { - pnode->vBlockHashesToAnnounce.push_back(hash); - } - }); - m_connman.WakeMessageHandler(); } + + // 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); + } + }); + m_connman.WakeMessageHandler(); } /**