Skip to content

Commit 9492e93

Browse files
committed
Add helper function for checking header continuity
1 parent 7f24508 commit 9492e93

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/net_processing.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ class PeerManagerImpl final : public PeerManager
565565
* occasional non-connecting header (this can happen due to BIP 130 headers
566566
* announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
567567
void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers);
568+
/** Return true if the headers connect to each other, false otherwise */
569+
bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
568570

569571
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
570572

@@ -2241,6 +2243,18 @@ void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
22412243
}
22422244
}
22432245

2246+
bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const
2247+
{
2248+
uint256 hashLastBlock;
2249+
for (const CBlockHeader& header : headers) {
2250+
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2251+
return false;
2252+
}
2253+
hashLastBlock = header.GetHash();
2254+
}
2255+
return true;
2256+
}
2257+
22442258
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
22452259
const std::vector<CBlockHeader>& headers,
22462260
bool via_compact_block)
@@ -2271,21 +2285,18 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
22712285
return;
22722286
}
22732287

2288+
// At this point, the headers connect to something in our block index.
2289+
if (!CheckHeadersAreContinuous(headers)) {
2290+
Misbehaving(peer, 20, "non-continuous headers sequence");
2291+
return;
2292+
}
2293+
22742294
{
22752295
LOCK(cs_main);
22762296

2277-
uint256 hashLastBlock;
2278-
for (const CBlockHeader& header : headers) {
2279-
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
2280-
Misbehaving(peer, 20, "non-continuous headers sequence");
2281-
return;
2282-
}
2283-
hashLastBlock = header.GetHash();
2284-
}
2285-
22862297
// If we don't have the last header, then they'll have given us
22872298
// something new (if these headers are valid).
2288-
if (!m_chainman.m_blockman.LookupBlockIndex(hashLastBlock)) {
2299+
if (!m_chainman.m_blockman.LookupBlockIndex(headers.back().GetHash())) {
22892300
received_new_header = true;
22902301
}
22912302
}

0 commit comments

Comments
 (0)