@@ -565,6 +565,8 @@ class PeerManagerImpl final : public PeerManager
565
565
* occasional non-connecting header (this can happen due to BIP 130 headers
566
566
* announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
567
567
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 ;
568
570
569
571
void SendBlockTransactions (CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
570
572
@@ -2241,6 +2243,18 @@ void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
2241
2243
}
2242
2244
}
2243
2245
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
+
2244
2258
void PeerManagerImpl::ProcessHeadersMessage (CNode& pfrom, Peer& peer,
2245
2259
const std::vector<CBlockHeader>& headers,
2246
2260
bool via_compact_block)
@@ -2271,21 +2285,18 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
2271
2285
return ;
2272
2286
}
2273
2287
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
+
2274
2294
{
2275
2295
LOCK (cs_main);
2276
2296
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
-
2286
2297
// If we don't have the last header, then they'll have given us
2287
2298
// 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 () )) {
2289
2300
received_new_header = true ;
2290
2301
}
2291
2302
}
0 commit comments