mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-12 11:19:08 -05:00
Add helper function for checking header continuity
This commit is contained in:
parent
7f2450871b
commit
9492e93bf9
1 changed files with 21 additions and 10 deletions
|
@ -565,6 +565,8 @@ private:
|
||||||
* occasional non-connecting header (this can happen due to BIP 130 headers
|
* occasional non-connecting header (this can happen due to BIP 130 headers
|
||||||
* announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
|
* announcements for blocks interacting with the 2hr (MAX_FUTURE_BLOCK_TIME) rule). */
|
||||||
void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers);
|
void HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer, const std::vector<CBlockHeader>& headers);
|
||||||
|
/** Return true if the headers connect to each other, false otherwise */
|
||||||
|
bool CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const;
|
||||||
|
|
||||||
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
||||||
|
|
||||||
|
@ -2241,6 +2243,18 @@ void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PeerManagerImpl::CheckHeadersAreContinuous(const std::vector<CBlockHeader>& headers) const
|
||||||
|
{
|
||||||
|
uint256 hashLastBlock;
|
||||||
|
for (const CBlockHeader& header : headers) {
|
||||||
|
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
hashLastBlock = header.GetHash();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
||||||
const std::vector<CBlockHeader>& headers,
|
const std::vector<CBlockHeader>& headers,
|
||||||
bool via_compact_block)
|
bool via_compact_block)
|
||||||
|
@ -2271,21 +2285,18 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// At this point, the headers connect to something in our block index.
|
||||||
|
if (!CheckHeadersAreContinuous(headers)) {
|
||||||
|
Misbehaving(peer, 20, "non-continuous headers sequence");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
|
|
||||||
uint256 hashLastBlock;
|
|
||||||
for (const CBlockHeader& header : headers) {
|
|
||||||
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
|
|
||||||
Misbehaving(peer, 20, "non-continuous headers sequence");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
hashLastBlock = header.GetHash();
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we don't have the last header, then they'll have given us
|
// If we don't have the last header, then they'll have given us
|
||||||
// something new (if these headers are valid).
|
// something new (if these headers are valid).
|
||||||
if (!m_chainman.m_blockman.LookupBlockIndex(hashLastBlock)) {
|
if (!m_chainman.m_blockman.LookupBlockIndex(headers.back().GetHash())) {
|
||||||
received_new_header = true;
|
received_new_header = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue